curses --- 字符单元显示的终端处理¶
源代码: Lib/curses
The curses module provides an interface to the curses library, the
de-facto standard for portable advanced terminal handling.
虽然 curses 在 Unix 环境中使用最为广泛,但也有适用于 Windows,DOS 以及其他可能的系统的版本。此扩展模块旨在匹配 ncurses 的 API,这是一个部署在 Linux 和 Unix 的 BSD 变体上的开源 curses 库。
适用范围: not Android, not iOS, not WASI.
此模块在 移动平台 或 WebAssembly 平台 上不受支持。
这是一个 optional module。 如果它在你的 CPython 副本中缺失,请查看你的发行方(也就是说,向你提供 Python 的人)的文档。 如果你就是发行方,请参阅 针对可选模块的要求。
适用范围: Unix.
备注
每当文档提到 字符 时,它可以被指定为一个整数,一个单字符 Unicode 字符串或者一个单字节的字节字符串。
每当此文档提到 字符串 时,它可以被指定为一个 Unicode 字符串或者一个字节字符串。
参见
- 模块
curses.ascii 在 ASCII 字符上工作的工具,无论你的区域设置是什么。
- 模块
curses.panel 为 curses 窗口添加深度的面板栈扩展。
- 模块
curses.textpad 用于使 curses 支持 Emacs 式绑定的可编辑文本部件。
- 用 Python 进行 Curses 编程
关于配合 Python 使用 curses 的教学材料,由 Andrew Kuchling 和 Eric Raymond 撰写。
函数¶
The module curses defines the following exception:
- exception curses.error¶
当 curses 库中函数返回一个错误时引发的异常。
备注
只要一个函数或方法的 x 或 y 参数是可选项,它们会默认为当前光标位置。 而当 attr 是可选项时,它会默认为 A_NORMAL。
The module curses defines the following functions:
- curses.assume_default_colors(fg, bg, /)¶
允许在支持此特性的终端上使用颜色的默认值。使用此功能可在您的应用程序中支持透明度。
将终端默认的前景色/背景色分配为颜色编号
-1。 因此,init_pair(x, COLOR_RED, -1)将初始化颜色对 x 为红色前景色和默认背景色,而init_pair(x, -1, COLOR_BLUE)将初始化颜色对 x 为默认前景色和蓝色背景色。将颜色对
0的定义更改为(fg, bg)。
This is an ncurses extension.
Added in version 3.14.
- curses.baudrate()¶
以每秒比特数为单位返回终端输出速度。 在软件终端模拟器上它将具有一个固定的最高值。 此函数出于历史原因被包括;在以前,它被用于写输出循环以提供时间延迟,并偶尔根据线路速度来改变接口。
- curses.beep()¶
发出短促的提醒声音。
- curses.can_change_color()¶
根据程序员能否改变终端显示的颜色返回
True或False。
- curses.cbreak()¶
进入 cbreak 模式。 在 cbreak 模式(有时也称为“稀有”模式)通常的 tty 行缓冲会被关闭并且字符可以被一个一个地读取。 但是,与原始模式不同,特殊字符(中断、退出、挂起和流程控制)会在 tty 驱动和调用程序上保留其效果。 首先调用
raw()然后调用cbreak()会将终端置于 cbreak 模式。
- curses.color_content(color_number)¶
Return the intensity of the red, green, and blue (RGB) components in the color color_number, which must be between
0andCOLORS - 1. Return a 3-tuple, containing the R,G,B values for the given color, which will be between0(no component) and1000(maximum amount of component). Raise an exception if the color is not supported.
- curses.color_pair(pair_number)¶
返回用于以指定颜色对显示文本的属性值。 仅支持前 256 个颜色对。 该属性值可与
A_STANDOUT,A_REVERSE以及其他A_*属性组合使用。pair_number()是此函数的对应操作。
- curses.curs_set(visibility)¶
设置光标状态。 visibility 可设为
0,1或2表示不可见、正常与高度可见。 如果终端支持所请求的可见性,则返回之前的光标状态;否则会引发异常。 在许多终端上,“正常可见”模式为下划线光标而“高度可见”模式为方块形光标。
- curses.def_prog_mode()¶
将当前终端模式保存为 "program" 模式,即正在运行的程序使用 curses 的模式。 (与其相对的是 "shell" 模式,即程序不使用 curses。) 对
reset_prog_mode()的后续调用将恢复此模式。
- curses.def_shell_mode()¶
将当前终端模式保存为 "shell" 模式,即正在运行的程序不使用 curses 的模式。 (与其相对的是 "program" 模式,即程序使用 功能。) 对
reset_shell_mode()的后续调用将恢复此模式。
- curses.delay_output(ms)¶
在输出中插入 ms 毫秒的暂停。
- curses.doupdate()¶
Update the physical screen. The curses library keeps two data structures, one representing the current physical screen contents and a virtual screen representing the desired next state. The
doupdate()function updates the physical screen to match the virtual screen.虚拟屏幕可以通过在写入操作例如在一个窗口上执行
addstr()之后调用noutrefresh()来刷新。 普通的refresh()调用只是简单的noutrefresh()加doupdate();如果你需要更新多个窗口,你可以通过在所有窗口上发出noutrefresh()调用再加单次doupdate()来提升性能并可减少屏幕闪烁。
- curses.echo()¶
进入 echo 模式。 在 echo 模式下,输入的每个字符都会在输入后回显到屏幕上。
- curses.endwin()¶
撤销库的初始化,使终端返回正常状态。
- curses.erasechar()¶
将用户的当前擦除字符以单字节字节串对象的形式返回。 在 Unix 操作系统下这是 curses 程序用来控制 tty 的属性,而不是由 curses 库本身来设置的。
- curses.filter()¶
The
filter()routine, if used, must be called beforeinitscr()is called. The effect is that, during the initialization,LINESis set to1; the capabilitiesclear,cup,cud,cud1,cuu1,cuu,vpaare disabled; and thehomestring is set to the value ofcr. The effect is that the cursor is confined to the current line, and so are screen updates. This may be used for enabling character-at-a-time line editing without touching the rest of the screen.
- curses.flushinp()¶
刷新所有输入缓冲区。 这会丢弃任何已被用户输入但尚未被程序处理的预输入内容。
- curses.getmouse()¶
在
getch()返回KEY_MOUSE以发出鼠标事件信号之后,应当调用此方法来获取加入队列的鼠标事件,事件以一个 5 元组(id, x, y, z, bstate)来表示。 其中 id 为用于区分多个设备的 ID 值,而 x, y, z 为事件的坐标。 (z 目前未被使用。) bstate 为一个整数值,其各个比特位将被设置用来表示事件的类型,并将为下列常量中的一个或多个按位 OR 的结果,其中 n 是以 1 到 5 表示的键号:BUTTONn_PRESSED,BUTTONn_RELEASED,BUTTONn_CLICKED,BUTTONn_DOUBLE_CLICKED,BUTTONn_TRIPLE_CLICKED,BUTTON_SHIFT,BUTTON_CTRL,BUTTON_ALT。在 3.10 版本发生变更: 现在
BUTTON5_*常量如果是由下层 curses 库提供的则会对外公开。
- curses.getwin(file)¶
Read window-related data stored in the file by an earlier
window.putwin()call. The routine then creates and initializes a new window using that data, returning the new window object. The file argument must be a file object opened for reading in binary mode.
- curses.has_colors()¶
如果终端能显示彩色则返回
True;否则返回False。
- curses.has_extended_color_support()¶
Return
Trueif the module supports extended colors; otherwise, returnFalse. Extended color support allows more than 256 color pairs for terminals that support more than 16 colors (for example, xterm-256color).扩展颜色支持要求 ncurses 版本为 6.1 或更新。
Added in version 3.10.
- curses.has_ic()¶
如果终端具有插入和删除字符的功能则返回
True。 此函数仅是出于历史原因而被包括的,因为所有现代软件终端模拟器都具有这些功能。
- curses.has_il()¶
如果终端具有插入和删除行功能,或者能够使用滚动区域来模拟这些功能则返回
True。 此函数仅是出于历史原因而被包括的,因为所有现代软件终端模拟器都具有这些功能。
- curses.has_key(ch)¶
接受一个键值 ch,并在当前终端类型能识别出具有该值的键时返回
True。
- curses.halfdelay(tenths)¶
用于半延迟模式,与 cbreak 模式的类似之处是用户所键入的字符会立即对程序可用。 但是,在阻塞 tenths 个十分之一秒之后,如果还未输入任何内容则将引发异常。 tenths 值必须为
1和255之间的数字。 使用nocbreak()可退出半延迟模式。
- curses.init_color(color_number, r, g, b)¶
更改某个颜色的定义,接受要更改的颜色编号以及三个 RGB 值(表示红绿蓝三个分量的强度)。 color_number 的值必须为
0和COLORS - 1之间的数字。 每个 r, g, b 值必须为0和1000之间的数字。 当使用init_color()时,出现在屏幕上的对应颜色会立即按照新定义来更改。 此函数在大多数终端上都是无操作的;它仅会在can_change_color()返回True时生效。
- curses.init_pair(pair_number, fg, bg)¶
更改颜色对的定义。 它需要三个参数:要更改的颜色对的编号、前景色编号和背景色编号。 pair_number 的值必须介于
1和COLOR_PAIRS - 1之间 (0颜色对只能由use_default_colors()和assume_default_colors()更改)。 fg 和 bg 参数的值必须介于0和COLORS - 1之间,或者在调用use_default_colors()或assume_default_colors()时使用-1。 如果该颜色对之前已被初始化,则屏幕将被刷新,该颜色对的所有出现都将更改为新定义。
- curses.intrflush(flag)¶
If flag is
True, pressing an interrupt key (interrupt, break, or quit) will flush all output in the terminal driver queue. If flag isFalse, no flushing is done.
- curses.is_term_resized(nlines, ncols)¶
如果
resize_term()会修改窗口结构则返回True,否则返回False。
- curses.keyname(k)¶
将编号为 k 的键名称作为字节串对象返回。 生成可打印 ASCII 字符的键名称就是键所对应的字符。 Ctrl-键组合的键名称则是一个两字节的字节串对象,它由插入符 (
b'^') 加对应的可打印 ASCII 字符组成。 Alt-键组合 (128--255) 的键名称则是由前缀b'M-'加对应的可打印 ASCII 字符组成的字节串对象。Raise a
ValueErrorif k is negative.
- curses.killchar()¶
将用户的当前行删除字符以单字节字节串对象的形式返回。 在 Unix 操作系统下这是 curses 程序用来控制 tty 的属性,而不是由 curses 库本身来设置的。
- curses.longname()¶
返回一个字节串对象,其中包含描述当前终端的 terminfo 长名称字段。 详细描述的最大长度为 128 个字符。 它仅在调用
initscr()之后才会被定义。
- curses.meta(flag)¶
如果 flag 为
True,则允许输入 8 比特位的字符。 如果 flag 为False,则只允许 7 比特位的字符。
- curses.mouseinterval(interval)¶
Set the maximum time in milliseconds that can elapse between press and release events in order for them to be recognized as a click, and return the previous interval value. The default value is 166 milliseconds, or one sixth of a second. Use a negative interval to obtain the interval value without changing it.
- curses.mousemask(mousemask)¶
Set the mouse events to be reported, and return a tuple
(availmask, oldmask). availmask indicates which of the specified mouse events can be reported; on complete failure it returns0. oldmask is the previous value of the mouse event mask. If this function is never called, no mouse events are ever reported.
- curses.napms(ms)¶
休眠 ms 毫秒。
- curses.newpad(nlines, ncols)¶
创建并返回一个指向具有给定行数和列数新的面板数据结构的指针。 将面板作为窗口对象返回。
A pad is like a window, except that it is not restricted by the screen size, and is not necessarily associated with a particular part of the screen. Pads can be used when a large window is needed, and only a part of the window will be on the screen at one time. Automatic refreshes of pads (such as from scrolling or echoing of input) do not occur. The
refresh()andnoutrefresh()methods of a pad require 6 arguments to specify the part of the pad to be displayed and the location on the screen to be used for the display. The arguments are pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol; the p arguments refer to the upper-left corner of the pad region to be displayed and the s arguments define a clipping box on the screen within which the pad region is to be displayed.
- curses.newwin(nlines, ncols)¶
- curses.newwin(nlines, ncols, begin_y, begin_x)
返回一个新的 窗口,其左上角位于
(begin_y, begin_x),并且其高度/宽度为 nlines/ncols。默认情况下,窗口将从指定位置扩展到屏幕的右下角。
- curses.nl(flag=True)¶
进入 newline 模式。 此模式会在输入时将回车转换为换行符,并在输出时将换行符转换为回车加换行。 newline 模式会在初始时启用。
If flag is
False, the effect is the same as callingnonl().
- curses.nocbreak()¶
退出 cbreak 模式。 返回具有行缓冲的正常 "cooked" 模式。
- curses.noecho()¶
退出 echo 模式。 关闭输入字符的回显。
- curses.nonl()¶
退出 newline 模式。 停止在输入时将回车转换为换行,并停止在输出时从换行到换行/回车的底层转换(但这不会改变
addch('\n')的行为,此行为总是在虚拟屏幕上执行相当于回车加换行的操作)。 当停止转换时,curses 有时能使纵向移动加快一些;并且,它将能够在输入时检测回车键。
- curses.noqiflush()¶
当使用
noqiflush()例程时,与INTR,QUIT和SUSP字符相关联的输入和输出队列的正常刷新将不会被执行。 如果你希望在处理程序退出后还能继续输出,就像没有发生过中断一样,你可能会想要在信号处理程序中调用noqiflush()。
- curses.noraw()¶
退出 raw 模式。 返回具有行缓冲的正常 "cooked" 模式。
- curses.pair_content(pair_number)¶
返回包含对应于所请求颜色对的元组
(fg, bg)。 pair_number 的值必须在0和COLOR_PAIRS - 1之间。
- curses.pair_number(attr)¶
返回通过属性值 attr 所设置的颜色对的编号。
color_pair()是此函数的对应操作。
- curses.putp(str)¶
等价于
tputs(str, 1, putchar);为当前终端发出指定 terminfo 功能的值。 请注意putp()的输出总是前往标准输出。setupterm()(orinitscr()) must be called first.
- curses.qiflush([flag])¶
如果 flag 为
False,则效果与调用noqiflush()相同。 如果 flag 为True或未提供参数,则在读取这些控制字符时队列将被刷新。
- curses.raw()¶
进入 raw 模式。 在 raw 模式下,正常的行缓冲和对中断、退出、挂起和流程控制键的处理会被关闭;字符会被逐个地提交给 curses 输入函数。
- curses.reset_prog_mode()¶
将终端恢复到 "program" 模式,如之前由
def_prog_mode()所保存的一样。
- curses.reset_shell_mode()¶
将终端恢复到 "shell" 模式,如之前由
def_shell_mode()所保存的一样。
- curses.resize_term(nlines, ncols)¶
由
resizeterm()用来执行大部分工作的后端函数;当调整窗口大小时,resize_term()会以空白填充扩展区域。 调用方应用程序应当以适当的数据填充这些区域。resize_term()函数会尝试调整所有窗口的大小。 但是,由于面板的调用约定,在不与应用程序进行额外交互的情况下是无法调整其大小的。
- curses.resizeterm(nlines, ncols)¶
将标准窗口和当前窗口的大小调整为指定的尺寸,并调整由 curses 库所使用的记录窗口尺寸的其他记录数据(特别是 SIGWINCH 处理程序)。
- curses.get_escdelay()¶
提取通过
set_escdelay()设置的值。Added in version 3.9.
- curses.set_escdelay(ms)¶
设置读取一个转义字符后要等待的毫秒数,以区分在键盘上输入的单个转义字符与通过光标和功能键发送的转义序列。
Added in version 3.9.
- curses.get_tabsize()¶
提取通过
set_tabsize()设置的值。Added in version 3.9.
- curses.set_tabsize(size)¶
设置 curses 库在将制表符添加到窗口时将制表符转换为空格所使用的列数。
Added in version 3.9.
- curses.setupterm(term=None, fd=-1)¶
初始化终端。 term 为给出终端名称的字符串或为
None;如果省略或为None,则将使用TERM环境变量的值。 fd 是任何初始化序列将被发送到的文件描述符;如未指定或为-1,则将使用sys.stdout的文件描述符。Raise a
curses.errorif the terminal could not be found or its terminfo database entry could not be read. If the terminal has already been initialized, this function has no effect.
- curses.start_color()¶
如果程序员想要使用颜色,则必须在任何其他颜色操作例程被调用之前调用它。 在
initscr()之后立即调用此例程是一个很好的做法。start_color()initializes eight basic colors (black, red, green, yellow, blue, magenta, cyan, and white), and two global variables in thecursesmodule,COLORSandCOLOR_PAIRS, containing the maximum number of colors and color-pairs the terminal can support. It also restores the colors on the terminal to the values they had when the terminal was just turned on.
- curses.termattrs()¶
返回终端所支持的所有视频属性逻辑 OR 的值。 此信息适用于当 curses 程序需要对屏幕外观进行完全控制的情况。
- curses.termname()¶
将环境变量
TERM的值截短至 14 个字节,作为字节串对象返回。
- curses.tigetflag(capname)¶
将与 terminfo 功能名称 capname 相对应的布尔功能值以整数形式返回。 如果 capname 不是一个布尔功能则返回
-1,如果其被取消或不存在于终端描述中则返回0。setupterm()(orinitscr()) must be called first.
- curses.tigetnum(capname)¶
将与 terminfo 功能名称 capname 相对应的数字功能值以整数形式返回。 如果 capname 不是一个数字功能则返回
-2,如果其被取消或不存在于终端描述中则返回-1。setupterm()(orinitscr()) must be called first.
- curses.tigetstr(capname)¶
将与 terminfo 功能名称 capname 相对应的字符串功能值以字节串对象形式返回。 如果 capname 不是一个 terminfo "字符串功能" 或者如果其被取消或不存在于终端描述中则返回
None。setupterm()(orinitscr()) must be called first.
- curses.tparm(str[, ...])¶
Instantiate the bytes object str with the supplied parameters, where str should be a parameterized string obtained from the terminfo database. For example,
tparm(tigetstr("cup"), 5, 3)could result inb'\033[6;4H', the exact result depending on terminal type. Up to nine integer parameters may be supplied.setupterm()(orinitscr()) must be called first.
- curses.typeahead(fd)¶
指定将被用于预输入检查的文件描述符 fd。 如果 fd 为
-1,则不执行预输入检查。curses 库会在更新屏幕时通过定期查找预输入来执行 "断行优化"。 如果找到了输入,并且输入是来自于 tty,则会将当前更新推迟至 refresh 或 doupdate 再次被调用的时候,以便允许更快地响应预先输入的命令。 此函数允许为预输入检查指定其他的文件描述符。
- curses.unctrl(ch)¶
返回一个字节串对象作为字符 ch 的可打印表示形式。 控制字符会表示为一个插入符加相应的字符,例如
b'^C'。 可打印字符则会保持原样。
- curses.unget_wch(ch)¶
推送 ch 以便让下一个
get_wch()返回该字符。备注
在
get_wch()被调用之前只能推送一个 ch。Added in version 3.3.
- curses.use_env(flag)¶
如果使用此函数,则应当在调用
initscr()或 newterm 之前调用它。 当 flag 为False时,将会使用在 terminfo 数据库中指定的行和列的值,即使设置了环境变量LINES和COLUMNS(默认使用),或者如果 curses 是在窗口中运行(在此情况下如果未设置LINES和COLUMNS则默认行为将是使用窗口大小)。
- curses.use_default_colors()¶
相当于
assume_default_colors(-1, -1)。
- curses.wrapper(func, /, *args, **kwargs)¶
初始化 curses 并调用另一个可调用对象 func,该对象应当为你的使用 curses 的应用程序的其余部分。 如果应用程序引发了异常,此函数将在重新引发异常并生成回溯信息之前将终端恢复到正常状态。 随后可调用对象 func 会被传入主窗口 'stdscr' 作为其第一个参数,再带上其他所有传给
wrapper()的参数。 在调用 func 之前,wrapper()会启用 cbreak 模式,关闭回显,启用终端键盘,并在终端具有颜色支持的情况下初始化颜色。 在退出时(无论是正常退出还是异常退出)它会恢复 cooked 模式,打开回显,并禁用终端键盘。
Window objects¶
- window.addch(ch[, attr])¶
- window.addch(y, x, ch[, attr])
将带有属性 attr 的字符 ch 绘制到
(y, x),覆盖之前在该位置上绘制的任何字符。 默认情况下,字符的位置和属性均为窗口对象的当前设置。备注
Writing outside the window, subwindow, or pad raises a
curses.error. Attempting to write to the lower-right corner of a window, subwindow, or pad will cause an exception to be raised after the character is printed.
- window.addnstr(str, n[, attr])¶
- window.addnstr(y, x, str, n[, attr])
将带有属性 attr 的字符串 str 中的至多 n 个字符绘制到
(y, x),覆盖之前在屏幕上的任何内容。
- window.addstr(str[, attr])¶
- window.addstr(y, x, str[, attr])
将带有属性 attr 的字符串 str 绘制到
(y, x),覆盖之前在屏幕上的任何内容。备注
Writing outside the window, subwindow, or pad raises
curses.error. Attempting to write to the lower-right corner of a window, subwindow, or pad will cause an exception to be raised after the string is printed.A bug in ncurses, the backend for this Python module, could cause segfaults when resizing windows. This was fixed in ncurses-6.1-20190511. If you are stuck with an earlier ncurses, you can avoid triggering it by not calling
addstr()with a str that has embedded newlines; instead, calladdstr()separately for each line.
- window.attroff(attr)¶
从应用于写入到当前窗口的 "background" 集中移除属性 attr。
- window.attron(attr)¶
将属性 attr 添加到应用于对当前窗口的所有写入的 "background" 集合。
- window.attrset(attr)¶
将 "background" 属性集设为 attr。 该集合初始时为
0(无属性)。
- window.bkgd(ch[, attr])¶
将窗口 background 特征属性设为带有属性 attr 的字符 ch。 随后此修改将应用于放置到该窗口中的每个字符。
窗口中每个字符的属性会被修改为新的 background 属性。
不论之前的 background 字符出现在哪里,它都会被修改为新的 background 字符。
- window.bkgdset(ch[, attr])¶
设置窗口的背景。 窗口的背景由字符和属性的任意组合构成。 背景的属性部分会与写入窗口的所有非空白字符合并(即 OR 运算)。 背景的字符和属性部分均会与空白字符合并。 背景将成为字符的特征属性并在任何滚动与插入/删除行/字符操作中与字符一起移动。
- window.border([ls[, rs[, ts[, bs[, tl[, tr[, bl[, br]]]]]]]])¶
在窗口边缘绘制边框。每个参数指定用于边界特定部分的字符;请参阅下表了解更多详情。
备注
任何形参的值为
0都将导致该形参使用默认字符。 关键字形参 不可 被使用。 默认字符在下表中列出:参数
描述
默认值
ls
左侧
rs
右侧
ts
顶部
bs
底部
tl
左上角
tr
右上角
bl
左下角
br
右下角
- window.chgat(attr)¶
- window.chgat(num, attr)
- window.chgat(y, x, attr)
- window.chgat(y, x, num, attr)
在当前光标位置或是在所提供的位置
(y, x)设置 num 个字符的属性。 如果 num 未给出或为-1,则将属性设置到所有字符上直至行尾。 如果提供了位置(y, x)则此函数会将光标移至该位置。 修改过的行将使用touchline()方法处理以便下次窗口刷新时内容会重新显示。
- window.clrtobot()¶
从光标位置开始擦除直至窗口末端:光标以下的所有行都会被删除,然后会执行
clrtoeol()的等效操作。
- window.clrtoeol()¶
从光标位置开始擦除直至行尾。
- window.cursyncup()¶
更新窗口所有上级窗口的当前光标位置以反映窗口的当前光标位置。
- window.delch([y, x])¶
Delete the character under the cursor, or at
(y, x)if specified. All characters to the right on the same line are shifted one position left.
- window.deleteln()¶
删除在光标之下的行。 所有后续的行都会上移一行。
- window.derwin(begin_y, begin_x)¶
- window.derwin(nlines, ncols, begin_y, begin_x)
"derive window" 的缩写,
derwin()与调用subwin()等效,不同之处在于 begin_y 和 begin_x 是相对于窗口的初始位置,而不是相对于整个屏幕。 返回代表所派生窗口的窗口对象。
- window.enclose(y, x)¶
检测给定的相对屏幕的字符-单元格坐标是否被给定的窗口所包围,返回
True或False。 它适用于确定是哪个屏幕窗口子集包围着某个鼠标事件的位置。在 3.10 版本发生变更: 在之前版本中它会返回
1或0而不是True或False。
- window.encoding¶
用于编码方法参数(Unicode 字符串和字符)的编码格式。 encoding 属性是在创建子窗口时从父窗口继承的,例如通过
window.subwin()。 在默认情况下,会使用当前语言区域的编码格式(参见locale.getencoding()文档)。Added in version 3.3.
- window.erase()¶
清空窗口。
- window.getbegyx()¶
返回以左上角为原点的坐标元组
(y, x)。
- window.getbkgd()¶
返回给定窗口的当前背景字符/属性对。
- window.getch([y, x])¶
获取一个字符。 请注意所返回的整数 不一定 要在 ASCII 范围以内:功能键、小键盘键等等是由大于 255 的数字表示的。 在无延迟模式下,如果没有输入则返回
-1,在其他情况下都会等待直至有键被按下。
- window.get_wch([y, x])¶
获取一个宽字符。 对于大多数键都是返回一个字符,对于功能键、小键盘键和其他特殊键则是返回一个整数。 在无延迟模式下,如果没有输入则引发一个异常。
Added in version 3.3.
- window.getkey([y, x])¶
获取一个字符,返回一个字符串而不是像
getch()那样返回一个整数。 功能键、小键盘键和其他特殊键则是返回一个包含键名的多字节字符串。 在无延迟模式下,如果没有输入则引发一个异常。
- window.getmaxyx()¶
返回窗口高度和宽度的元组
(y, x)。
- window.getparyx()¶
将此窗口相对于父窗口的起始坐标作为元组
(y, x)返回。 如果此窗口没有父窗口则返回(-1, -1)。
- window.getstr()¶
- window.getstr(n)
- window.getstr(y, x)
- window.getstr(y, x, n)
Read a bytes object from the user, with primitive line editing capacity. At most n characters are read; n defaults to and cannot exceed 2047.
在 3.14 版本发生变更: The maximum value for n was increased from 1023 to 2047.
- window.getyx()¶
返回当前光标相对于窗口左上角的位置的元组
(y, x)。
- window.hline(ch, n[, attr])¶
- window.hline(y, x, ch, n[, attr])
Display a horizontal line starting at
(y, x)with length n consisting of the character ch with attributes attr. The line stops at the right edge of the window if fewer than n cells are available.
- window.idcok(flag)¶
如果 flag 为
False,curses 将不再考虑使用终端的硬件插入/删除字符功能;如果 flag 为True,则会启用字符插入和删除。 当 curses 首次初始化时,默认会启用字符插入/删除。
- window.idlok(flag)¶
If flag is
True,curseswill try to use hardware line editing facilities. Otherwise, curses will not use them.
- window.immedok(flag)¶
如果 flag 为
True,窗口图像中的任何改变都会自动导致窗口被刷新;你不必再自己调用refresh()。 但是,这可能会由于重复调用 wrefresh 而显著降低性能。 此选项默认被禁用。
- window.inch([y, x])¶
返回窗口中给定位置上的字符。 下面的 8 个比特位是字符本身,上面的比特位则为属性。
- window.insch(ch[, attr])¶
- window.insch(y, x, ch[, attr])
Insert character ch with attributes attr before the character under the cursor, or at
(y, x)if specified. All characters to the right of the cursor are shifted one position right, with the rightmost character on the line being lost. The cursor position does not change.
- window.insdelln(nlines)¶
在指定窗口的当前行上方插入 nlines 行。 下面的 nlines 行将丢失。 对于 nlines 为负值的情况,则从光标下方的行开始删除 nlines 行,并将其余的行向上移动。 下面的 nlines 行会被清空。 当前光标位置将保持不变。
- window.insertln()¶
在光标下方插入一个空行。 所有后续的行都会下移一行。
- window.insnstr(str, n[, attr])¶
- window.insnstr(y, x, str, n[, attr])
在光标下方的字符之前插入一个至多为 n 个字符的字符串(字符数量将与该行相匹配)。 如果 n 为零或负数,则插入整个字符串。 光标右边的所有字符将被右移,该行右端的字符将丢失。 光标位置将保持不变(在移到可能指定的 y, x 之后)。
- window.insstr(str[, attr])¶
- window.insstr(y, x, str[, attr])
在光标下方的字符之前插入一个字符串(字符数量将与该行相匹配)。 光标右边的所有字符将被右移,该行右端的字符将丢失。 光标位置将保持不变(在移到可能指定的 y, x 之后)。
- window.instr([n])¶
- window.instr(y, x[, n])
Return a bytes object of characters, extracted from the window starting at the current cursor position, or at y, x if specified, and stopping at the end of the line. Attributes and color information are stripped from the characters. If n is specified,
instr()returns a string at most n characters long (exclusive of the trailing NUL). The maximum value for n is 2047.在 3.14 版本发生变更: The maximum value for n was increased from 1023 to 2047.
- window.is_linetouched(line)¶
如果指定的行自上次调用
refresh()后发生了改变则返回True;否则返回False。 如果 line 对于给定的窗口不可用则会引发curses.error异常。
- window.keypad(flag)¶
If flag is
True, escape sequences generated by some keys (keypad, function keys) will be interpreted bycurses. If flag isFalse, escape sequences will be left as is in the input stream.
- window.leaveok(flag)¶
If flag is
True, cursor is left where it is on update, instead of being at "cursor position." This reduces cursor movement where possible.如果 flag 为
False,光标在更新后将总是位于“光标位置”。
- window.move(new_y, new_x)¶
将光标移至
(new_y, new_x)。
- window.mvderwin(y, x)¶
让窗口在其父窗口内移动。 窗口相对于屏幕的参数不会被更改。 此例程用于在屏幕的相同物理位置显示父窗口的不同部分。
- window.mvwin(new_y, new_x)¶
移动窗口以使其左上角位于
(new_y, new_x)。Moving the window so that any part of it would be off the screen is an error: the window is not moved and
curses.erroris raised.
- window.notimeout(flag)¶
如果 flag 为
True,则转义序列将不会发生超时。如果 flag 为
False,则在几毫秒之后,转义序列将不会被解析,并将保持在输入流中的原样。
- window.noutrefresh()¶
- window.noutrefresh(pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol)
标记为刷新但保持等待。 此函数会更新代表预期窗口状态的数据结构,但并不强制更新物理屏幕。 要完成后者,请调用
doupdate()。The 6 arguments can only be specified, and are then required, when the window is a pad created with
newpad(); they have the same meaning as forrefresh().
- window.overlay(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])¶
将窗口覆盖在 destwin 上方。 窗口的大小不必相同,只有重叠的区域会被复制。 此复制是非破坏性的,这意味着当前背景字符不会覆盖掉 destwin 的旧内容。
为了获得对被复制区域的细粒度控制,可以使用
overlay()的第二种形式。 sminrow 和 smincol 是源窗口的左上角坐标,而其他变量则在目标窗口中标记出一个矩形。
- window.overwrite(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])¶
将窗口覆盖在 destwin 上方。 窗口的大小不必相同,此时只有重叠的区域会被复制。 此复制是破坏性的,这意味着当前背景字符会覆盖掉 destwin 的旧内容。
为了获得对被复制区域的细粒度控制,可以使用
overwrite()的第二种形式。 sminrow 和 smincol 是源窗口的左上角坐标,而其他变量则在目标窗口中标记出一个矩形。
- window.refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])¶
立即更新显示(将实际屏幕与之前的绘制/删除方法进行同步)。
The 6 arguments can only be specified, and are then required, when the window is a pad created with
newpad(). The additional parameters are needed to indicate what part of the pad and screen are involved. pminrow and pmincol specify the upper-left corner of the rectangle to be displayed in the pad. sminrow, smincol, smaxrow, and smaxcol specify the edges of the rectangle to be displayed on the screen. The lower-right corner of the rectangle to be displayed in the pad is calculated from the screen coordinates, since the rectangles must be the same size. Both rectangles must be entirely contained within their respective structures. Negative values of pminrow, pmincol, sminrow, or smincol are treated as if they were zero.
- window.resize(nlines, ncols)¶
为 curses 窗口重新分配存储空间以将其尺寸调整为指定的值。 如果任一维度的尺寸大于当前值,则窗口的数据将以具有合并了当前背景渲染(由
bkgdset()设置)的空白来填充。
- window.scroll([lines=1])¶
Scroll the screen or scrolling region. Scroll upward by lines lines if lines is positive, or downward if it is negative. Scrolling has no effect unless it has been enabled for the window with
scrollok().
- window.scrollok(flag)¶
控制当一个窗口的光标移出窗口或滚动区域边缘时会发生什么,这可能是在底端行执行换行操作,或者在最后一行输入最后一个字符导致的结果。 如果 flag 为
False,光标会留在底端行。 如果 flag 为True,窗口会向上滚动一行。 请注意为了在终端上获得实际的滚动效果,还需要调用idlok()。
- window.setscrreg(top, bottom)¶
设置从 top 行至 bottom 行的滚动区域。 所有滚动操作将在此区域中进行。
- window.standend()¶
关闭 standout 属性。 在某些终端上此操作会有关闭所有属性的副作用。
- window.standout()¶
启用属性 A_STANDOUT。
- window.subpad(begin_y, begin_x)¶
- window.subpad(nlines, ncols, begin_y, begin_x)
Return a sub-pad, whose upper-left corner is at
(begin_y, begin_x), and whose width/height is ncols/nlines. The coordinates are relative to the parent pad (unlikesubwin(), which uses screen coordinates). This method is only available for pads created withnewpad().
- window.subwin(begin_y, begin_x)¶
- window.subwin(nlines, ncols, begin_y, begin_x)
Return a sub-window, whose upper-left corner is at the screen-relative coordinates
(begin_y, begin_x), and whose width/height is ncols/nlines.默认情况下,子窗口将从指定位置扩展到窗口的右下角。
- window.syncup()¶
触碰已在窗口中被改变的此窗口的各个上级窗口中的所有位置。
- window.timeout(delay)¶
为窗口设置阻塞或非阻塞读取行为。 如果 delay 为负值,则会使用阻塞读取(这将无限期地等待输入)。 如果 delay 为零,则会使用非阻塞读取,并且当没有输入在等待时
getch()将返回-1。 如果 delay 为正值,则getch()将阻塞 delay 毫秒,并且当此延时结束时仍无输入将返回-1。
- window.touchline(start, count[, changed])¶
假定从行 start 开始的 count 行已被更改。 如果提供了 changed,它将指明是将受影响的行标记为已更改 (changed
=True) 还是未更改 (changed=False)。
- window.touchwin()¶
假定整个窗口已被更改,其目的是用于绘制优化。
- window.vline(ch, n[, attr])¶
- window.vline(y, x, ch, n[, attr])
显示一行起始于
(y, x)长度为 n 的由具有属性 attr 的字符 ch 组成的垂直线。
常量¶
The curses module defines the following data members:
- curses.version¶
一个代表模块当前版本的字节串对象。
- curses.ncurses_version¶
一个具名元组,它包含构成 ncurses 库版本号的三个数字: major, minor 和 patch。 三个值均为整数。 三个值也可通过名称来访问,因此
curses.ncurses_version[0]等价于curses.ncurses_version.major,依此类推。可用性:如果使用了 ncurses 库。
Added in version 3.8.
- curses.COLORS¶
终端可支持的最大颜色数。 它只有在调用
start_color()之后才会被定义。
- curses.COLOR_PAIRS¶
终端可支持的最大颜色对数。 它只有在调用
start_color()之后才会被定义。
- curses.COLS¶
The width of the screen, that is, the number of columns. It is defined only after the call to
initscr(). Updated byupdate_lines_cols(),resizeterm()andresize_term().
- curses.LINES¶
The height of the screen, that is, the number of lines. It is defined only after the call to
initscr(). Updated byupdate_lines_cols(),resizeterm()andresize_term().
有些常量可用于指定字符单元属性。 实际可用的常量取决于具体的系统。
属性 |
含意 |
|---|---|
|
备用字符集模式 |
|
闪烁模式 |
|
粗体模式 |
|
暗淡模式 |
|
不可见或空白模式 |
|
斜体模式 |
|
正常属性 |
|
保护模式 |
|
反转背景色和前景色 |
|
突出模式 |
|
下划线模式 |
|
水平突出显示 |
|
左高亮 |
|
底部高亮 |
|
右高亮 |
|
顶部高亮 |
|
垂直突出显示 |
Added in version 3.7: 新增 A_ITALIC。
有几个常量可用于提取某些方法返回的相应属性。
位掩码 |
含意 |
|---|---|
|
用于提取属性的位掩码 |
|
用于提取字符的位掩码 |
|
用于提取颜色对字段信息的位掩码 |
键由名称以 KEY_ 开头的整数常量引用。确切的可用键取决于系统。
键常量 |
键 |
|---|---|
|
最小键值 |
|
中断键(不可靠) |
|
向下箭头 |
|
向上箭头 |
|
向左箭头 |
|
向右箭头 |
|
Home 键 (上+左箭头) |
|
退格(不可靠) |
|
功能键。 支持至多 64 个功能键。 |
|
功能键 n 的值 |
|
删除行 |
|
插入行 |
|
删除字符 |
|
插入字符或进入插入模式 |
|
退出插入字符模式 |
|
清空屏幕 |
|
清空至屏幕底部 |
|
清空至行尾 |
|
向前滚动 1 行 |
|
向后滚动 1 行 (反转) |
|
下一页 |
|
上一页 |
|
设置制表符 |
|
清除制表符 |
|
清除所有制表符 |
|
回车或发送 (不可靠) |
|
软 (部分) 重置 (不可靠) |
|
重置或硬重置 (不可靠) |
|
打印 |
|
Home 向下或到底 (左下) |
|
键盘的左上角 |
|
键盘的右上角 |
|
键盘的中心 |
|
键盘左下方 |
|
键盘右下方 |
|
回退制表符 |
|
Beg (开始) |
|
取消 |
|
关闭 |
|
Cmd (命令行) |
|
复制 |
|
创建 |
|
End |
|
退出 |
|
查找 |
|
帮助 |
|
标记 |
|
消息 |
|
移动 |
|
下一个 |
|
打开 |
|
选项 |
|
Prev (上一个) |
|
重做 |
|
Ref (引用) |
|
刷新 |
|
替换 |
|
重启 |
|
恢复 |
|
保存 |
|
Shift + Beg (开始) |
|
Shift + Cancel |
|
Shift + Command |
|
Shift + Copy |
|
Shift + Create |
|
Shift + 删除字符 |
|
Shift + 删除行 |
|
选择 |
|
Shift + End |
|
Shift + 清空行 |
|
Shift + 退出 |
|
Shift + 查找 |
|
Shift + 帮助 |
|
Shift + Home |
|
Shift + 输入 |
|
Shift + 向左箭头 |
|
Shift + 消息 |
|
Shift + 移动 |
|
Shift + 下一个 |
|
Shift + 选项 |
|
Shift + 上一个 |
|
Shift + 打印 |
|
Shift + 重做 |
|
Shift + 替换 |
|
Shift + 向右箭头 |
|
Shift + 恢复 |
|
Shift + 保存 |
|
Shift + 挂起 |
|
Shift + 撤销 |
|
挂起 |
|
撤销操作 |
|
鼠标事件已发生 |
|
终端大小改变事件 |
|
最大键值 |
在 VT100s 及其软件模拟器,如 X 终端模拟器上,通常至少有四个功能键 (KEY_F1, KEY_F2, KEY_F3, KEY_F4) 可用,并且方向键将明确地映射到 KEY_UP, KEY_DOWN, KEY_LEFT 和 KEY_RIGHT。 如果你的机器有一个 PC 键盘,则保证能使用方向键和十二个功能键 (老式的 PC 键盘可能只有十个功能键);此外,还有以下的标准小键盘映射:
键帽 |
常量 |
|---|---|
Insert |
KEY_IC |
Delete |
KEY_DC |
Home |
KEY_HOME |
End |
KEY_END |
Page Up |
KEY_PPAGE |
Page Down |
KEY_NPAGE |
下表列出了替代字符集中的字符。 这些字符继承自 VT100 终端,在 X 终端等软件模拟器上通常均为可用。 当没有可用的图形时,curses 会回退为粗糙的可打印 ASCII 近似符号。
备注
只有在调用 initscr() 之后才能使用它们
ACS代码 |
含意 |
|---|---|
|
alternate name for upper-right corner |
|
实心方块 |
|
正方形 |
|
水平线的别名 |
|
alternate name for upper-left corner |
|
顶部 T 型的别名 |
|
底部 T 型 |
|
正方形 |
|
棋盘(点刻) |
|
向下箭头 |
|
度数符号 |
|
菱形 |
|
大于或等于 |
|
水平线 |
|
灯形符号 |
|
向左箭头 |
|
小于或等于 |
|
lower-left corner |
|
lower-right corner |
|
左侧 T 型 |
|
不等号 |
|
字母π |
|
正负号 |
|
加号 |
|
向右箭头 |
|
右侧 T 型 |
|
扫描线 1 |
|
扫描线3 |
|
扫描线7 |
|
扫描线 9 |
|
alternate name for lower-right corner |
|
垂直线的别名 |
|
右侧 T 型的别名 |
|
alternate name for lower-left corner |
|
底部 T 型的别名 |
|
左侧 T 型的别名 |
|
交叉或大加号的替代名称 |
|
英镑 |
|
顶部 T 型 |
|
向上箭头 |
|
upper-left corner |
|
upper-right corner |
|
垂线 |
下面列出了 getmouse() 所使用的鼠标按键常量:
鼠标按键常量 |
含意 |
|---|---|
|
鼠标按键 n 被按下 |
|
鼠标按键 n 被释放 |
|
鼠标按键 n 被点击 |
|
鼠标按键 n 被双击 |
|
鼠标按键 n 被三击 |
|
当按键状态改变时 Shift 被按下 |
|
当按键状态改变时 Control 被按下 |
|
Alt was down during button state change |
在 3.10 版本发生变更: 现在 BUTTON5_* 常量如果是由下层 curses 库提供的则会对外公开。
下表列出了预定义的颜色:
常量 |
颜色 |
|---|---|
|
黑色 |
|
蓝色 |
|
青色(浅绿蓝色) |
|
绿色 |
|
洋红色(紫红色) |
|
红色 |
|
白色 |
|
黄色 |
curses.textpad --- Text input widget for curses programs¶
The curses.textpad module provides a Textbox class that handles
elementary text editing in a curses window, supporting a set of keybindings
resembling those of Emacs (thus, also of Netscape Navigator, BBedit 6.x,
FrameMaker, and many other programs). The module also provides a
rectangle-drawing function useful for framing text boxes or for other purposes.
The module curses.textpad defines the following function:
- curses.textpad.rectangle(win, uly, ulx, lry, lrx)¶
Draw a rectangle. The first argument must be a window object; the remaining arguments are coordinates relative to that window. The second and third arguments are the y and x coordinates of the upper-left corner of the rectangle to be drawn; the fourth and fifth arguments are the y and x coordinates of the lower-right corner. The rectangle will be drawn using VT100/IBM PC forms characters on terminals that make this possible (including xterm and most other software terminal emulators). Otherwise it will be drawn with ASCII dashes, vertical bars, and plus signs.
文本框对象¶
你可以通过如下方式实例化一个 Textbox:
- class curses.textpad.Textbox(win, insert_mode=False)¶
Return a textbox widget object. The win argument should be a curses window object in which the textbox is to be contained. If insert_mode is true, the textbox inserts typed characters, shifting existing text to the right, rather than overwriting it. The edit cursor of the textbox is initially located at the upper-left corner of the containing window, with coordinates
(0, 0). The instance'sstripspacesflag is initially on.Textbox对象具有以下方法:- edit(validate=None)¶
This is the entry point you will normally use. It accepts editing keystrokes until one of the termination keystrokes is entered. If validate is supplied, it must be a function. It will be called for each keystroke entered with the keystroke as a parameter; command dispatch is done on the result. If it returns a false value, the keystroke is ignored. This method returns the window contents as a string; whether blanks in the window are included is affected by the
stripspacesattribute.
- do_command(ch)¶
Process a single command keystroke. Returns
1to continue editing, or0if a termination keystroke was processed. Here are the supported special keystrokes:按键
动作
Control-A
转到窗口的左边缘。
Control-B
光标向左,如果可能,包含前一行。
Control-D
删除光标下的字符。
Control-E
前往右边缘(stripspaces 关闭时)或者行尾(stripspaces 启用时)。
Control-F
向右移动光标,适当时换行到下一行。
Control-G
终止,返回窗口内容。
Control-H
向后删除字符。
Control-J
Terminate if the window is 1 line, otherwise move to the start of the next line.
Control-K
如果行为空,则删除它,否则清除到行尾。
Control-L
刷新屏幕。
Control-N
光标向下;向下移动一行。
Control-O
在光标位置插入一个空行。
Control-P
光标向上;向上移动一行。
如果光标位于无法移动的边缘,则移动操作不执行任何操作。在可能的情况下,支持以下同义词:
常量
按键
Control-B
Control-F
Control-P
Control-N
Control-h
所有其他按键将被视为插入给定字符并右移的命令(带有自动折行)。
- gather()¶
以字符串形式返回窗口内容;是否包括窗口中的空白将受到
stripspaces成员的影响。
- stripspaces¶
此属性是控制窗口中空白解读方式的旗标。 当启用时,每一行的末尾空白会被忽略;任何将光标定位至末尾空白的光标动作都将改为前往该行末尾,并且在收集窗口内容时将去除末尾空白。