curses --- 字符单元显示的终端处理¶
源代码: Lib/curses
curses 模块提供了 curses 库的接口,这是可移植高级终端处理的事实标准。
虽然 curses 在 Unix 环境中使用最为广泛,但也有适用于 Windows,DOS 以及其他可能的系统的版本。此扩展模块旨在匹配 ncurses 的 API,这是一个部署在 Linux 和 Unix 的 BSD 变体上的开源 curses 库。
适用范围: not Android, not iOS, not WASI.
此模块在 移动平台 或 WebAssembly 平台 上不受支持。
这是一个 optional module。 如果它在你的 CPython 副本中缺失,请查看你的发行方(也就是说,向你提供 Python 的人)的文档。 如果你就是发行方,请参阅 针对可选模块的要求。
适用范围: Unix.
备注
Whenever the documentation mentions a character it can be specified
as an integer, a one-character Unicode string or a one-byte byte string.
An integer is the code of a single encoded byte, optionally combined with
attributes and a color pair, as returned by window.inch().
Methods that write to a window accept also a character cell: a Unicode
string of a spacing character followed by combining characters, or a
complexchar.
Whenever the documentation mentions a character string it can be specified
as a Unicode string or a byte string.
Methods that write to a window accept also a complexstr.
备注
Whether curses may be used from several threads
depends on the underlying library and how it was built.
In many implementations, including the default build of ncurses,
the screen state is shared and not thread-safe;
since the blocking and refresh methods
(such as getch() and refresh())
release the GIL,
unsynchronized use from several threads can then crash the interpreter.
Serialize the calls,
or wrap them in window.use() and screen.use().
参见
- 模块
curses.ascii 在 ASCII 字符上工作的工具,无论你的区域设置是什么。
- 模块
curses.panel 为 curses 窗口添加深度的面板栈扩展。
- 模块
curses.textpad 用于使 curses 支持 Emacs 式绑定的可编辑文本部件。
- 用 Python 进行 Curses 编程
关于配合 Python 使用 curses 的教学材料,由 Andrew Kuchling 和 Eric Raymond 撰写。
函数¶
curses 模块定义了以下异常:
- exception curses.error¶
当 curses 库中函数返回一个错误时引发的异常。
备注
只要一个函数或方法的 x 或 y 参数是可选项,它们会默认为当前光标位置。 而当 attr 是可选项时,它会默认为 A_NORMAL。
curses 模块定义了以下函数:
Initialization and termination¶
- curses.initscr()¶
初始化库。 返回代表整个屏幕的 窗口 对象。
请参阅
setupterm()了解关于在此函数之前调用它的注意事项。备注
如果打开终端时发生错误,则下层的 curses 库可能会导致解释器退出。
- curses.endwin()¶
撤销库的初始化,使终端返回正常状态。
- curses.newterm(type=None, fd=None, infd=None, /)¶
Initialize a new terminal in addition to the one initialized by
initscr(), and return a screen for it. This allows a program to drive more than one terminal.type is the terminal name, as in
setupterm(); ifNone, the value of theTERMenvironment variable is used. fd and infd are the output and input files for the terminal: either a file object or a file descriptor. They default tosys.stdoutandsys.stdin.The new screen becomes the current one. Use
set_term()to switch between screens.请参阅
setupterm()了解关于在此函数之前调用它的注意事项。Added in version 3.16.
- curses.new_prescr()¶
Return a new screen that can be used to call functions that affect global state before
initscr()ornewterm()is called.Availability: if the underlying curses library provides
new_prescr().Added in version 3.16.
- curses.set_term(screen, /)¶
Make screen, a screen returned by
newterm(), the current terminal, and return the previously current screen. ReturnsNoneif the previous screen was the one created byinitscr(). Raiseserrorif screen has no terminal, as is the case for a screen returned bynew_prescr().Added in version 3.16.
- curses.wrapper(func, /, *args, **kwargs)¶
初始化 curses 并调用另一个可调用对象 func,该对象应当为你的使用 curses 的应用程序的其余部分。 如果应用程序引发了异常,此函数将在重新引发异常并生成回溯信息之前将终端恢复到正常状态。 随后可调用对象 func 会被传入主窗口 'stdscr' 作为其第一个参数,再带上其他所有传给
wrapper()的参数。 在调用 func 之前,wrapper()会启用 cbreak 模式,关闭回显,启用终端键盘,并在终端具有颜色支持的情况下初始化颜色。 在退出时(无论是正常退出还是异常退出)它会恢复 cooked 模式,打开回显,并禁用终端键盘。
Terminal mode control¶
- curses.def_prog_mode()¶
将当前终端模式保存为 "program" 模式,即正在运行的程序使用 curses 的模式。 (与其相对的是 "shell" 模式,即程序不使用 curses。) 对
reset_prog_mode()的后续调用将恢复此模式。
- curses.def_shell_mode()¶
将当前终端模式保存为 "shell" 模式,即正在运行的程序不使用 curses 的模式。 (与其相对的是 "program" 模式,即程序使用 功能。) 对
reset_shell_mode()的后续调用将恢复此模式。
- curses.reset_prog_mode()¶
将终端恢复到 "program" 模式,如之前由
def_prog_mode()所保存的一样。
- curses.reset_shell_mode()¶
将终端恢复到 "shell" 模式,如之前由
def_shell_mode()所保存的一样。
- curses.curs_set(visibility)¶
设置光标状态。 visibility 可设为
0,1或2表示不可见、正常与高度可见。 如果终端支持所请求的可见性,则返回之前的光标状态;否则会引发异常。 在许多终端上,“正常可见”模式为下划线光标而“高度可见”模式为方块形光标。
- curses.doupdate()¶
更新物理屏幕。 curses 库会保留两个数据结构,一个代表当前物理屏幕的内容,另一个代表期望的后续状态的虚拟屏幕。
doupdate()函数更新物理屏幕以匹配虚拟屏幕。虚拟屏幕可以通过在写入操作例如在一个窗口上执行
addstr()之后调用noutrefresh()来刷新。 普通的refresh()调用只是简单的noutrefresh()加doupdate();如果你需要更新多个窗口,你可以通过在所有窗口上发出noutrefresh()调用再加单次doupdate()来提升性能并可减少屏幕闪烁。
Input options¶
- curses.cbreak()¶
进入 cbreak 模式。 在 cbreak 模式(有时也称为“稀有”模式)通常的 tty 行缓冲会被关闭并且字符可以被一个一个地读取。 但是,与原始模式不同,特殊字符(中断、退出、挂起和流程控制)会在 tty 驱动和调用程序上保留其效果。 首先调用
raw()然后调用cbreak()会将终端置于 cbreak 模式。
- curses.nocbreak()¶
退出 cbreak 模式。 返回具有行缓冲的正常 "cooked" 模式。
- curses.echo()¶
进入 echo 模式。 在 echo 模式下,输入的每个字符都会在输入后回显到屏幕上。
- curses.noecho()¶
退出 echo 模式。 关闭输入字符的回显。
- curses.raw()¶
进入 raw 模式。 在 raw 模式下,正常的行缓冲和对中断、退出、挂起和流程控制键的处理会被关闭;字符会被逐个地提交给 curses 输入函数。
- curses.noraw()¶
退出 raw 模式。 返回具有行缓冲的正常 "cooked" 模式。
- curses.halfdelay(tenths)¶
Used for half-delay mode, which is similar to cbreak mode in that characters typed by the user are immediately available to the program. However, after blocking for tenths tenths of seconds, if nothing has been typed,
getch()returns-1, whilegetkey()andget_wch()raisecurses.error. The value of tenths must be a number between1and255. Usenocbreak()to leave half-delay mode.
- curses.meta(flag)¶
如果 flag 为
True,则允许输入 8 比特位的字符。 如果 flag 为False,则只允许 7 比特位的字符。
- curses.nl(flag=True)¶
进入 newline 模式。 此模式会在输入时将回车转换为换行符,并在输出时将换行符转换为回车加换行。 newline 模式会在初始时启用。
如果 flag 为
False,则效果与调用nonl()相同。
- curses.nonl()¶
退出 newline 模式。 停止在输入时将回车转换为换行,并停止在输出时从换行到换行/回车的底层转换(但这不会改变
addch('\n')的行为,此行为总是在虚拟屏幕上执行相当于回车加换行的操作)。 当停止转换时,curses 有时能使纵向移动加快一些;并且,它将能够在输入时检测回车键。
- curses.intrflush(flag)¶
如果 flag 为
True,则按下中断键(interrupt、break 或 quit)将刷新终端驱动程序队列中的所有输出。 如果 flag 为False,则不执行刷新。
- curses.qiflush([flag])¶
如果 flag 为
False,则效果与调用noqiflush()相同。 如果 flag 为True或未提供参数,则在读取这些控制字符时队列将被刷新。
- curses.noqiflush()¶
当使用
noqiflush()例程时,与INTR,QUIT和SUSP字符相关联的输入和输出队列的正常刷新将不会被执行。 如果你希望在处理程序退出后还能继续输出,就像没有发生过中断一样,你可能会想要在信号处理程序中调用noqiflush()。
- curses.typeahead(fd)¶
指定将被用于预输入检查的文件描述符 fd。 如果 fd 为
-1,则不执行预输入检查。The curses library does "line-breakout optimization" by looking for typeahead periodically while updating the screen. If input is found, and it is coming from a tty, the current update is postponed until
refresh()ordoupdate()is called again, allowing faster response to commands typed in advance. This function allows specifying a different file descriptor for typeahead checking.
- curses.is_cbreak()¶
Return
Trueif cbreak mode (seecbreak()) is enabled,Falseotherwise. Availability: ncurses 6.5 or later.Added in version 3.16.
- curses.is_echo()¶
Return
Trueif echo mode (seeecho()) is enabled,Falseotherwise. Availability: ncurses 6.5 or later.Added in version 3.16.
Keyboard input¶
- curses.ungetch(ch)¶
Push ch so the next
getch()orget_wch()will return it.ch may be an integer (a key code or the code of an encoded byte), a byte, or a string of length 1. A one-character string is pushed like
unget_wch(); on a narrow build it must encode to a single byte.备注
在
getch()被调用之前只能推送一个 ch。在 3.16 版本发生变更: A one-character string argument is no longer required to encode to a single byte, except on a narrow build.
- curses.unget_wch(ch)¶
推送 ch 以便让下一个
get_wch()返回该字符。ch 可以是一个整数(字符代码,不是键代码)或一个长度为 1 的字符串。
备注
在
get_wch()被调用之前只能推送一个 ch。Added in version 3.3.
在 3.16 版本发生变更: Also available on a narrow build, where ch must encode to a single byte (an 8-bit locale).
- curses.flushinp()¶
刷新所有输入缓冲区。 这会丢弃任何已被用户输入但尚未被程序处理的预输入内容。
- curses.has_key(ch)¶
接受一个键值 ch,并在当前终端类型能识别出具有该值的键时返回
True。
- curses.keyname(k)¶
将编号为 k 的键名称作为字节串对象返回。 生成可打印 ASCII 字符的键名称就是键所对应的字符。 Ctrl-键组合的键名称则是一个两字节的字节串对象,它由插入符 (
b'^') 加对应的可打印 ASCII 字符组成。 Alt-键组合 (128--255) 的键名称则是由前缀b'M-'加对应的可打印 ASCII 字符组成的字节串对象。如果 k 为负值则会引发
ValueError。
- curses.define_key(definition, keycode)¶
Define an escape sequence definition, a string, as a key that generates the key code keycode, so that
cursesinterprets it like one of the keys predefined in the terminal database.If definition is
None, any existing binding for keycode is removed. If keycode is zero or negative, any existing binding for definition is removed.Added in version 3.16.
- curses.key_defined(definition)¶
Return the key code bound to the escape sequence definition, a string,
0if no key code is bound to it, or-1if definition is a prefix of a longer bound sequence (and so is ambiguous).Added in version 3.16.
- curses.keyok(keycode, enable)¶
Enable (if enable is true) or disable (otherwise) interpretation of the key code keycode. Unlike
window.keypad(), this affects a single key code rather than all of them.Added in version 3.16.
Mouse¶
- 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.mousemask(mousemask)¶
设置要报告的鼠标事件,并返回一个元组
(availmask, oldmask)。 availmask 表明指定的鼠标事件中哪些可以被报告;当完全失败时将返回0。 oldmask 是给定窗口鼠标事件掩码的先前值。 如果从未调用此函数,则不会报告任何鼠标事件。
- curses.mouseinterval(interval)¶
设置按下和释放事件之间可以经过的最长时间(以毫秒为单位),以便将它们识别为一次点击,并返回先前的间隔值。 默认值为 166 毫秒,即六分之一秒。 使用负数 interval 可在不更改间隔值的情况下获取它。
- curses.has_mouse()¶
Return
Trueif the mouse driver has been successfully initialized.Availability: if the underlying curses library provides
has_mouse().Added in version 3.16.
颜色¶
- 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.has_colors()¶
如果终端能显示彩色则返回
True;否则返回False。
- curses.has_extended_color_support()¶
如果模块支持扩展颜色,则返回
True;否则返回False。 扩展颜色支持允许支持超过 16 种颜色的终端(例如 xterm-256color)使用超过 256 个颜色对。扩展颜色支持要求 ncurses 版本为 6.1 或更新。
Added in version 3.10.
- curses.can_change_color()¶
根据程序员能否改变终端显示的颜色返回
True或False。
- 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.color_content(color_number)¶
返回颜色 color_number 中红、绿和蓝(RGB)分量的强度,此强度值必须介于
0和COLORS - 1之间。 返回一个 3 元组,其中包含给定颜色的 R,G,B 值,它们将介于0(无分量)和1000(最大分量)之间。 如果该颜色不受支持,则引发异常。
- 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.pair_content(pair_number)¶
返回包含对应于所请求颜色对的元组
(fg, bg)。 pair_number 的值必须在0和COLOR_PAIRS - 1之间。
- curses.color_pair(pair_number)¶
Return the attribute value for displaying text in the specified color pair. Only color pairs that fit in the color-pair field of the returned value can be represented (usually the first 256); a larger pair_number raises
OverflowErrorrather than being silently masked to a different pair. Usecolor_set()orattr_set()to display higher pairs. This attribute value can be combined withA_STANDOUT,A_REVERSE, and the otherA_*attributes.pair_number()is the counterpart to this function.
- curses.pair_number(attr)¶
返回通过属性值 attr 所设置的颜色对的编号。
color_pair()是此函数的对应操作。
- curses.use_default_colors()¶
相当于
assume_default_colors(-1, -1)。
- curses.assume_default_colors(fg, bg, /)¶
允许在支持此特性的终端上使用颜色的默认值。使用此功能可在您的应用程序中支持透明度。
将终端默认的前景色/背景色分配为颜色编号
-1。 因此,init_pair(x, COLOR_RED, -1)将初始化颜色对 x 为红色前景色和默认背景色,而init_pair(x, -1, COLOR_BLUE)将初始化颜色对 x 为默认前景色和蓝色背景色。将颜色对
0的定义更改为(fg, bg)。
这是一个 ncurses 扩展。
Added in version 3.14.
- curses.alloc_pair(fg, bg)¶
Allocate a color pair for foreground color fg and background color bg, and return its number. If a color pair for the same combination of colors already exists, return its number. Otherwise allocate a new color pair and return its number.
This function is only available if Python was built against a wide-character version of the underlying curses library with extended-color support (see
has_extended_color_support()).Added in version 3.16.
- curses.free_pair(pair_number)¶
Free the color pair pair_number, which must have been allocated by
alloc_pair(). The pair must not be in use.This function is only available if Python was built against a wide-character version of the underlying curses library with extended-color support (see
has_extended_color_support()).Added in version 3.16.
- curses.find_pair(fg, bg)¶
Return the number of a color pair for foreground color fg and background color bg, or
-1if no color pair for this combination of colors has been allocated.This function is only available if Python was built against a wide-character version of the underlying curses library with extended-color support (see
has_extended_color_support()).Added in version 3.16.
- curses.reset_color_pairs()¶
Discard all color-pair definitions, releasing the color pairs allocated by
init_pair()andalloc_pair().This function is only available if Python was built against a wide-character version of the underlying curses library with extended-color support (see
has_extended_color_support()).Added in version 3.16.
Windows and pads¶
- curses.newwin(nlines, ncols)¶
- curses.newwin(nlines, ncols, begin_y, begin_x)
返回一个新的 窗口,其左上角位于
(begin_y, begin_x),并且其高度/宽度为 nlines/ncols。默认情况下,窗口将从指定位置扩展到屏幕的右下角。
- curses.newpad(nlines, ncols)¶
创建并返回一个指向具有给定行数和列数新的面板数据结构的指针。 将面板作为窗口对象返回。
面板类似于窗口,区别在于它不受屏幕大小的限制,并且不必与屏幕的特定部分相关联。 面板可以在需要使用大窗口时使用,并且每次只需将窗口的一部分放在屏幕上。 面板不会发生自动刷新(例如由于滚动或输入回显)。 面板的
refresh()和noutrefresh()方法需要 6 个参数来指定面板要显示的部分以及要用于显示的屏幕位置。 这些参数是 pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol; p 参数表示要显示的面板区域的左上角,而 s 参数定义了要显示的面板区域在屏幕上的剪裁框。
Soft labels¶
The following functions manage soft-label keys, a row of labels displayed
along the bottom line of the screen, typically used to label a row of function
keys. slk_init() must be called before initscr() or
newterm(); it takes one screen line away from the standard window for the
labels.
- curses.slk_init(fmt=0)¶
Reserve a screen line for the soft labels and choose their layout. fmt selects the arrangement:
0for 3-2-3 (eight labels),1for 4-4 (eight labels). Where the underlying curses library supports them,2gives 4-4-4 (twelve labels) and3gives 4-4-4 with an index line.Must be called before
initscr()ornewterm(), and affects only the screen created next.Added in version 3.16.
- curses.slk_set(labnum, label, justify)¶
Set the text of soft label number labnum, in the range
1through8(or12in a twelve-label layout). justify controls how label is placed within the label:0for left,1for centered,2for right.Added in version 3.16.
- curses.slk_label(labnum)¶
Return the current text of soft label number labnum, justified as it was set, or an empty string if it has no label.
Added in version 3.16.
- curses.slk_refresh()¶
Update the soft labels on the physical screen, like
refresh()for a window.Added in version 3.16.
- curses.slk_noutrefresh()¶
Update the soft labels on the virtual screen, like
window.noutrefresh(). Use it together withdoupdate()to batch screen updates.Added in version 3.16.
- curses.slk_clear()¶
Remove the soft labels from the screen.
Added in version 3.16.
- curses.slk_restore()¶
Restore the soft labels to the screen after a
slk_clear().Added in version 3.16.
- curses.slk_touch()¶
Force all the soft labels to be redrawn by the next
slk_refresh()orslk_noutrefresh().Added in version 3.16.
- curses.slk_attron(attr)¶
- curses.slk_attroff(attr)¶
- curses.slk_attrset(attr)¶
Add, remove, or set the attributes used to display the soft labels, given as packed
A_*attributes.Added in version 3.16.
- curses.slk_attr()¶
Return the current attributes of the soft labels as packed
A_*attributes. Availability depends on the underlying curses library.Added in version 3.16.
- curses.slk_attr_on(attr)¶
- curses.slk_attr_off(attr)¶
Turn the given attributes on or off without affecting any others. Like the
attr_*window methods, these work with the WA_* attributes rather than packedA_*attributes.Added in version 3.16.
- curses.slk_attr_set(attr, pair=0)¶
Set the attributes and color pair of the soft labels. attr is given as WA_* attributes and pair as a color pair number.
Added in version 3.16.
- curses.slk_color(pair)¶
Set the color pair of the soft labels to color pair number pair.
Added in version 3.16.
Saving and restoring¶
- curses.getwin(file)¶
读取先前通过调用
window.putwin()存储在文件中的窗口相关数据。 然后例程使用该数据创建并初始化一个新窗口,并返回新的窗口对象。 file 参数必须是以二进制模式打开供读取的文件对象。
- curses.scr_dump(filename)¶
Write the current contents of the virtual screen to filename, which may be a string or a path-like object. The file can later be read by
scr_restore(),scr_init()orscr_set(). This is the whole-screen counterpart ofwindow.putwin().Added in version 3.16.
- curses.scr_restore(filename)¶
Set the virtual screen to the contents of filename, which must have been written by
scr_dump(). The next call todoupdate()orwindow.refresh()restores the screen to those contents.Added in version 3.16.
- curses.scr_init(filename)¶
Initialize the assumed contents of the terminal from filename, which must have been written by
scr_dump(). Use it when the terminal already displays those contents, for example after another program has drawn the screen, so that curses does not redraw what is already there.Added in version 3.16.
- curses.scr_set(filename)¶
Use filename, which must have been written by
scr_dump(), as both the virtual screen and the assumed terminal contents. This combines the effects ofscr_restore()andscr_init().Added in version 3.16.
Querying the terminal¶
- curses.baudrate()¶
以每秒比特数为单位返回终端输出速度。 在软件终端模拟器上它将具有一个固定的最高值。 此函数出于历史原因被包括;在以前,它被用于写输出循环以提供时间延迟,并偶尔根据线路速度来改变接口。
- curses.erasechar()¶
Return the user's current erase character as a raw byte, a
bytesobject of length 1. See alsoerasewchar().
- curses.erasewchar()¶
Return the user's current erase character as a one-character
str. Under Unix operating systems this is a property of the controlling tty of the curses program, and is not set by the curses library itself.Added in version 3.16.
- curses.killchar()¶
Return the user's current line kill character as a raw byte, a
bytesobject of length 1. See alsokillwchar().
- curses.killwchar()¶
Return the user's current line kill character as a one-character
str. Under Unix operating systems this is a property of the controlling tty of the curses program, and is not set by the curses library itself.Added in version 3.16.
- curses.termname()¶
Return the name of the terminal in use, as a bytes object. This is the name passed to
newterm()orsetupterm(), or the value of theTERMenvironment variable if none was given. Some curses libraries truncate it to 14 characters.
- curses.longname()¶
返回一个字节串对象,其中包含描述当前终端的 terminfo 长名称字段。 详细描述的最大长度为 128 个字符。 它仅在调用
initscr()之后才会被定义。
- curses.termattrs()¶
返回终端所支持的所有视频属性逻辑 OR 的值。 此信息适用于当 curses 程序需要对屏幕外观进行完全控制的情况。
- curses.term_attrs()¶
Like
termattrs(), but return the attributes as WA_* values rather thanA_*values.Added in version 3.16.
- curses.has_ic()¶
如果终端具有插入和删除字符的功能则返回
True。 此函数仅是出于历史原因而被包括的,因为所有现代软件终端模拟器都具有这些功能。
- curses.has_il()¶
如果终端具有插入和删除行功能,或者能够使用滚动区域来模拟这些功能则返回
True。 此函数仅是出于历史原因而被包括的,因为所有现代软件终端模拟器都具有这些功能。
Bell and screen flash¶
- curses.beep()¶
发出短促的提醒声音。
Terminal resizing¶
- curses.resizeterm(nlines, ncols)¶
将标准窗口和当前窗口的大小调整为指定的尺寸,并调整由 curses 库所使用的记录窗口尺寸的其他记录数据(特别是 SIGWINCH 处理程序)。
- curses.resize_term(nlines, ncols)¶
由
resizeterm()用来执行大部分工作的后端函数;当调整窗口大小时,resize_term()会以空白填充扩展区域。 调用方应用程序应当以适当的数据填充这些区域。resize_term()函数会尝试调整所有窗口的大小。 但是,由于面板的调用约定,在不与应用程序进行额外交互的情况下是无法调整其大小的。
- curses.is_term_resized(nlines, ncols)¶
如果
resize_term()会修改窗口结构则返回True,否则返回False。
Terminfo database¶
- curses.setupterm(term=None, fd=-1)¶
初始化终端。 term 为给出终端名称的字符串或为
None;如果省略或为None,则将使用TERM环境变量的值。 fd 是任何初始化序列将被发送到的文件描述符;如未指定或为-1,则将使用sys.stdout的文件描述符。如果找不到终端或无法读取其 terminfo 数据库条目,则引发
curses.error。 如果终端已初始化,此函数将没有效果。
- curses.tigetflag(capname)¶
将与 terminfo 功能名称 capname 相对应的布尔功能值以整数形式返回。 如果 capname 不是一个布尔功能则返回
-1,如果其被取消或不存在于终端描述中则返回0。setupterm()(或initscr()) 必须先被调用。
- curses.tigetnum(capname)¶
将与 terminfo 功能名称 capname 相对应的数字功能值以整数形式返回。 如果 capname 不是一个数字功能则返回
-2,如果其被取消或不存在于终端描述中则返回-1。setupterm()(或initscr()) 必须先被调用。
- curses.tigetstr(capname)¶
将与 terminfo 功能名称 capname 相对应的字符串功能值以字节串对象形式返回。 如果 capname 不是一个 terminfo "字符串功能" 或者如果其被取消或不存在于终端描述中则返回
None。setupterm()(或initscr()) 必须先被调用。
- curses.tparm(str[, ...])¶
使用提供的形参实例化字节串对象 str,其中 str 应当是从 terminfo 数据库获取的参数化字节串。 例如,
tparm(tigetstr("cup"), 5, 3)的结果可能为b'\033[6;4H',实际结果取决于终端类型。 最多可以提供九个整数形参。setupterm()(或initscr()) 必须先被调用。
- curses.putp(str)¶
等价于
tputs(str, 1, putchar);为当前终端发出指定 terminfo 功能特性的值(一个字节串对象)。 请注意putp()的输出总是前往标准输出。setupterm()(或initscr()) 必须先被调用。
Utilities¶
- curses.unctrl(ch)¶
Return a bytes object which is a printable representation of the character ch; any attributes and color pair are ignored. Control characters are represented as a caret followed by a character, for example as
b'^C'. Printing characters are left as they are. The representation of other characters is defined by the underlying curses library.ch must fit in a single byte; use
wunctrl()for other characters.
- curses.wunctrl(ch)¶
Return a string which is a printable representation of the character ch; any attributes and color pair are ignored. ASCII control characters are represented as a caret followed by a character, for example as
'^C'. Printing characters, including non-ASCII characters printable in the locale, are left as they are. The representation of other characters is defined by the underlying curses library.Added in version 3.16.
- curses.filter()¶
The
filter()routine, if used, must be called beforeinitscr()ornewterm()is called, and affects every screen created afterwards. 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.nofilter()¶
Undo the effect of a previous
filter()call. Likefilter(), it must be called beforeinitscr()(ornewterm()) so that the next initialization uses the full screen again.Availability: if the underlying curses library provides
nofilter().Added in version 3.16.
- curses.use_env(flag)¶
If used, this function should be called before
initscr()ornewterm()are called, and affects every screen created afterwards. When flag isFalse, the values of lines and columns specified in the terminfo database will be used, even if environment variablesLINESandCOLUMNS(used by default) are set, or if curses is running in a window (in which case default behavior would be to use the window size ifLINESandCOLUMNSare not set).
- curses.get_escdelay()¶
提取通过
set_escdelay()设置的值。Added in version 3.9.
- curses.set_escdelay(ms)¶
设置读取一个转义字符后要等待的毫秒数,以区分在键盘上输入的单个转义字符与通过光标和功能键发送的转义序列。
Depending on the curses library, the setting may apply to all screens, not only to the current one.
Added in version 3.9.
- curses.get_tabsize()¶
提取通过
set_tabsize()设置的值。Added in version 3.9.
- curses.set_tabsize(size)¶
设置 curses 库在将制表符添加到窗口时将制表符转换为空格所使用的列数。
Depending on the curses library, the setting may apply to all screens, not only to the current one, and creating a screen may reset it.
Added in version 3.9.
- curses.napms(ms)¶
休眠 ms 毫秒。
- curses.delay_output(ms)¶
在输出中插入 ms 毫秒的暂停。
窗口对象¶
Adding and inserting text¶
- window.addch(ch[, attr])¶
- window.addch(y, x, ch[, attr])
将带有属性 attr 的字符 ch 绘制到
(y, x),覆盖之前在该位置上绘制的任何字符。 默认情况下,字符的位置和属性均为窗口对象的当前设置。ch may be a single character, optionally followed by combining characters, that together occupy one character cell.
备注
在窗口、子窗口或面板之外写入会引发
curses.error。 尝试在窗口、子窗口或面板的右下角写入将在字符被打印之后引发异常。在 3.16 版本发生变更: A character may now be given as a string of a base character followed by combining characters, instead of only a single character, or as a
complexcharcell.
- window.addstr(str[, attr])¶
- window.addstr(y, x, str[, attr])
将带有属性 attr 的字符串 str 绘制到
(y, x),覆盖之前在屏幕上的任何内容。str may also be a
complexstr, in which case each cell carries its own attributes and color pair, so attr must not be given. Acomplexstrobtained fromin_wchstr()is written back unchanged.备注
在窗口、子窗口或面板之外写入会引发
curses.error。 尝试在窗口、子窗口或面板的右下角写入将在字符串被打印之后引发异常。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.
在 3.16 版本发生变更: str may now also be a
complexstr, as described above.
- window.addnstr(str, n[, attr])¶
- window.addnstr(y, x, str, n[, attr])
将带有属性 attr 的字符串 str 中的至多 n 个字符绘制到
(y, x),覆盖之前在屏幕上的任何内容。在 3.16 版本发生变更: str may now also be a
complexstr; seeaddstr().
- window.echochar(ch[, attr])¶
使用属性 attr 添加字符 ch,并立即在窗口上调用
refresh()。在 3.16 版本发生变更: Wide and combining characters, and
complexcharcells, are now accepted.
- 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 (after moving to y, x, if specified).在 3.16 版本发生变更: Wide and combining characters, and
complexcharcells, are now accepted.
- window.insstr(str[, attr])¶
- window.insstr(y, x, str[, attr])
在光标下方的字符之前插入一个字符串(字符数量将与该行相匹配)。 光标右边的所有字符将被右移,该行右端的字符将丢失。 光标位置将保持不变(在移到可能指定的 y, x 之后)。
str may also be a
complexstr, in which case each cell carries its own attributes and color pair, so attr must not be given.在 3.16 版本发生变更: str may now also be a
complexstr, as described above.
- window.insnstr(str, n[, attr])¶
- window.insnstr(y, x, str, n[, attr])
Insert a character string (as many characters as will fit on the line) before the character under the cursor, up to n characters. If n is negative, the entire string is inserted. All characters to the right of the cursor are shifted right, with the rightmost characters on the line being lost. The cursor position does not change (after moving to y, x, if specified).
在 3.16 版本发生变更: str may now also be a
complexstr; seeinsstr().
Deleting and inserting lines¶
- window.delch([y, x])¶
删除光标下或
(y, x)处的字符(如果指定)。 同一行右侧的所有字符都会向左移动一个位置。
- window.deleteln()¶
删除在光标之下的行。 所有后续的行都会上移一行。
- window.insertln()¶
在光标下方插入一个空行。 所有后续的行都会下移一行。
- window.insdelln(nlines)¶
在指定窗口的当前行上方插入 nlines 行。 下面的 nlines 行将丢失。 对于 nlines 为负值的情况,则从光标下方的行开始删除 nlines 行,并将其余的行向上移动。 下面的 nlines 行会被清空。 当前光标位置将保持不变。
Reading input¶
- window.getch([y, x])¶
在将光标移动到 y, x (如果已指定)后读取按键,并将其作为整数返回。 如果窗口不是面板并且自上次刷新以来已被修改,则窗口会首先刷新。 等待直到按下按键,或者如果读取是非阻塞的或超时,则返回
-1(参见nodelay()和timeout())。普通键返回为其在当前语言区域编码中的单字节代码,因此用多个字节编码的字符需要多次调用。 例如,在 UTF-8 语言区域中,
'é'会被读取为195,然后是169。 请使用get_wch()将其作为单个字符读取。在小键盘模式下(参见
keypad()),功能键和其他特殊键将作为 KEY_* 常量 之一返回,这不会被误认为是普通键。 否则,或者如果它们的转义序列未及时到达(参见notimeout()和set_escdelay()),它们的字节将一次返回一个。
- window.get_wch([y, x])¶
在将光标移动到 y, x (如果已指定)后读取按键,并将其作为单字符
str返回。 如果窗口不是面板并且自上次刷新以来已被修改,则窗口会首先刷新。 等待直到按下按键,或者如果读取是非阻塞的或超时,则引发error(参见nodelay()和timeout())。在小键盘模式下(参见
keypad()),功能键和其他特殊键将作为 KEY_* 常量 之一(一个整数)返回。 否则,或者如果它们的转义序列未及时到达(参见notimeout()和set_escdelay()),它们的字符将一次返回一个。在回显模式下(参见
echo()),该键将像通过addch()一样被添加到窗口中;特殊键不会回显。Added in version 3.3.
在 3.16 版本发生变更: Also available on a narrow build, where only a character representable as a single byte (an 8-bit locale) can be returned.
- window.getkey([y, x])¶
像
getch()那样读取按键,但将其作为str返回:普通键作为单字符字符串(该字节按 Latin-1 解码),特殊键作为其名称,例如'KEY_UP'(参见keyname())。 如果没有输入,则引发error而不是返回-1。
- window.getstr()¶
- window.getstr(n)
- window.getstr(y, x)
- window.getstr(y, x, n)
如果指定了 y, x 则在移动光标之后读取一行来自用户的输入,带有基本的行编辑功能。 将其作为字节串对象返回,采用当前语言区域的编辑格式且不带末尾换行符。 最多读取 n 个字节;n 默认值 2047 也是其上限值。
Use
get_wstr()to read the input as astr.在 3.14 版本发生变更: The maximum value for n was increased from 1023 to 2047.
- window.get_wstr()¶
- window.get_wstr(n)
- window.get_wstr(y, x)
- window.get_wstr(y, x, n)
Read a line of input from the user, with primitive line editing capacity, after moving the cursor to y, x if specified. Return it as a
str, without the terminating newline. At most n characters are read; n defaults to and cannot exceed 2047.This is the wide-character variant of
getstr().Added in version 3.16.
Reading window contents¶
- window.inch([y, x])¶
Return the character at the given position in the window. The bottom 8 bits are the character proper and the upper bits are the attributes; extract them with the
A_CHARTEXTandA_ATTRIBUTESbit-masks, and the color pair withpair_number(). The character byte is the locale-encoded byte of the cell's character, consistent withinstr(). It cannot represent a cell holding combining characters, a character that does not fit in a single byte, or a color pair outside thecolor_pair()range; usein_wch()for those, which returns it as acomplexchar.
- window.in_wch([y, x])¶
Return the complex character at the given position in the window as a
complexchar. Unlikeinch(), the returned object carries the cell's text (a spacing character optionally followed by combining characters) together with its attributes and color pair.Added in version 3.16.
- window.instr([n])¶
- window.instr(y, x[, n])
Read the text of the window from the current cursor position, or from y, x if specified, to the end of the line or at most n bytes if n is specified, and return it as a bytes object, in the encoding of the current locale. Attributes and color pairs are stripped; use
in_wchstr()to read them too. A character not representable in the encoding cannot be returned; usein_wstr()for those.在 3.14 版本发生变更: The maximum value for n was increased from 1023 to 2047.
在 3.16 版本发生变更: n is no longer limited to 2047.
- window.in_wstr([n])¶
- window.in_wstr(y, x[, n])
Read the text of the window from the current cursor position, or from y, x if specified, to the end of the line or at most n characters if n is specified, and return it as a
str. Attributes and color pairs are stripped; usein_wchstr()to read them too.This is the wide-character variant of
instr().Added in version 3.16.
- window.in_wchstr([n])¶
- window.in_wchstr(y, x[, n])
Read the styled cells of the window from the current cursor position, or from y, x if specified, to the end of the line or at most n cells if n is specified, and return them as a
complexstr. Unlikeinstr()andin_wstr(), each cell keeps its attributes and color pair, so the result can be written back unchanged withaddstr().Added in version 3.16.
Attributes¶
- window.attroff(attr)¶
从应用于写入到当前窗口的 "background" 集中移除属性 attr。
- window.attron(attr)¶
将属性 attr 添加到应用于对当前窗口的所有写入的 "background" 集合。
- window.attrset(attr)¶
将 "background" 属性集设为 attr。 该集合初始时为
0(无属性)。
- window.attr_get()¶
Return the window's current rendition as a
(attrs, pair)tuple, where attrs is the set of attributes and pair is the color pair number.Unlike
attron()and friends, which take packedA_*attributes, this method and the otherattr_*methods work with the WA_* attributes and keep the color pair as a separate number, which lets them use color pairs that do not fit alongside the attributes in a single value.Added in version 3.16.
- window.attr_set(attr, pair=0)¶
Set the window's rendition to the attributes attr and the color pair pair.
Added in version 3.16.
- window.attr_on(attr)¶
Turn on the attributes attr without affecting any others.
Added in version 3.16.
- window.attr_off(attr)¶
Turn off the attributes attr without affecting any others.
Added in version 3.16.
- window.color_set(pair)¶
Set the window's color pair to pair, leaving the other attributes unchanged.
Added in version 3.16.
- window.getattrs()¶
Return the window's current attributes.
Added in version 3.16.
- window.standout()¶
启用属性 A_STANDOUT。
- window.standend()¶
关闭 standout 属性。 在某些终端上此操作会有关闭所有属性的副作用。
- 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()方法处理以便下次窗口刷新时内容会重新显示。
Background¶
- window.bkgd(ch[, attr])¶
将窗口 background 特征属性设为带有属性 attr 的字符 ch。 随后此修改将应用于放置到该窗口中的每个字符。
窗口中每个字符的属性会被修改为新的 background 属性。
不论之前的 background 字符出现在哪里,它都会被修改为新的 background 字符。
在 3.16 版本发生变更: Wide and combining characters, and
complexcharcells, are now accepted.
- window.bkgdset(ch[, attr])¶
设置窗口的背景。 窗口的背景由字符和属性的任意组合构成。 背景的属性部分会与写入窗口的所有非空白字符合并(即 OR 运算)。 背景的字符和属性部分均会与空白字符合并。 背景将成为字符的特征属性并在任何滚动与插入/删除行/字符操作中与字符一起移动。
在 3.16 版本发生变更: Wide and combining characters, and
complexcharcells, are now accepted.
- window.getbkgd()¶
Return the given window's current background character/attribute pair. Its components can be extracted like those of
inch(). It cannot represent a background set with a wide character or with a color pair outside thecolor_pair()range; usegetbkgrnd()for those.
- window.getbkgrnd()¶
Return the given window's current background as a
complexchar. Unlikegetbkgd(), the returned object carries the background character together with its attributes and color pair, and the color pair is not limited to the value that fits in acolor_pair().Added in version 3.16.
Clearing and erasing¶
- window.erase()¶
清空窗口。
- window.clrtobot()¶
从光标位置开始擦除直至窗口末端:光标以下的所有行都会被删除,然后会执行
clrtoeol()的等效操作。
- window.clrtoeol()¶
从光标位置开始擦除直至行尾。
Borders and lines¶
- window.border([ls[, rs[, ts[, bs[, tl[, tr[, bl[, br]]]]]]]])¶
在窗口边缘绘制边框。每个参数指定用于边界特定部分的字符;请参阅下表了解更多详情。
备注
任何形参的值为
0都将导致该形参使用默认字符。 关键字形参 不可 被使用。 默认字符在下表中列出:参数
描述
默认值
Wide default value
ls
左侧
rs
右侧
ts
顶部
bs
底部
tl
左上角
tr
右上角
bl
左下角
br
右下角
The wide default value is used when the border is drawn from string characters or
complexcharcells.If any parameter is a byte character or an integer other than
0, the border is drawn from byte characters, and every string character must be encodable as a single byte.在 3.16 版本发生变更: Wide and combining characters, and
complexcharcells, are now accepted. A single call cannot mixcomplexcharcells with integer or byte characters.
- window.box([vertch, horch])¶
类似于
border(),但 ls 和 rs 均为 vertch 而 ts 和 bs 均为 horch。 此函数总是会使用默认的转角字符。在 3.16 版本发生变更: Wide and combining characters, and
complexcharcells, are now accepted. A single call cannot mixcomplexcharcells with integer or byte characters.
- window.hline(ch, n[, attr])¶
- window.hline(y, x, ch, n[, attr])
显示一行起始于
(y, x)长度为 n 且由具有属性 attr 的字符 ch 组成的水平线。 如果可用的单元格少于 n 个,则该线将在窗口的右边缘停止。在 3.16 版本发生变更: Wide and combining characters, and
complexcharcells, are now accepted.
- window.vline(ch, n[, attr])¶
- window.vline(y, x, ch, n[, attr])
显示一行起始于
(y, x)长度为 n 的由具有属性 attr 的字符 ch 组成的垂直线。在 3.16 版本发生变更: Wide and combining characters, and
complexcharcells, are now accepted.
Cursor position and window geometry¶
- window.move(new_y, new_x)¶
将光标移至
(new_y, new_x)。
- window.getyx()¶
返回当前光标相对于窗口左上角的位置的元组
(y, x)。
- window.getbegyx()¶
返回以左上角为原点的坐标元组
(y, x)。
- window.getmaxyx()¶
返回窗口高度和宽度的元组
(y, x)。
- window.getparyx()¶
将此窗口相对于父窗口的起始坐标作为元组
(y, x)返回。 如果此窗口没有父窗口则返回(-1, -1)。
- window.getparent()¶
Return the parent window of this subwindow, or
Noneif this window is not a subwindow.Added in version 3.16.
Creating and resizing windows¶
- window.subwin(begin_y, begin_x)¶
- window.subwin(nlines, ncols, begin_y, begin_x)
返回一个子窗口,其左上角位于相对于屏幕的坐标
(begin_y, begin_x),其宽度/高度为 ncols/nlines。默认情况下,子窗口将从指定位置扩展到窗口的右下角。
- window.derwin(begin_y, begin_x)¶
- window.derwin(nlines, ncols, begin_y, begin_x)
"derive window" 的缩写,
derwin()与调用subwin()等效,不同之处在于 begin_y 和 begin_x 是相对于窗口的初始位置,而不是相对于整个屏幕。 返回代表所派生窗口的窗口对象。
- window.subpad(begin_y, begin_x)¶
- window.subpad(nlines, ncols, begin_y, begin_x)
返回一个子面板,其左上角位于
(begin_y, begin_x),其宽度/高度为 ncols/nlines。 坐标相对于父面板(与使用屏幕坐标的subwin()不同)。 此方法仅适用于使用newpad()创建的面板。
- window.dupwin()¶
Return a new window that is an exact duplicate of the window: it has the same size, position, contents and attributes. Unlike a window created by
subwin()orderwin(), the duplicate is independent of the original -- it has its own cell buffer, so later changes to one do not affect the other.Added in version 3.16.
- window.mvwin(new_y, new_x)¶
移动窗口以使其左上角位于
(new_y, new_x)。移动窗口导致其任何部分超出屏幕都是错误的:窗口不会移动,并且会引发
curses.error。
- window.mvderwin(y, x)¶
让窗口在其父窗口内移动。 窗口相对于屏幕的参数不会被更改。 此例程用于在屏幕的相同物理位置显示父窗口的不同部分。
Refreshing and redrawing¶
- window.refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])¶
立即更新显示(将实际屏幕与之前的绘制/删除方法进行同步)。
仅当窗口是使用
newpad()创建的面板时,才可以指定这 6 个参数(并且此时是必需的)。 需要额外的参数来指示所涉及的面板和屏幕部分。 pminrow 和 pmincol 指定要在面板中显示的矩形的左上角。 sminrow, smincol, smaxrow 和 smaxcol 指定要在屏幕上显示的矩形的边缘。 要在面板中显示的矩形的右下角是根据屏幕坐标计算的,因为这两个矩形的大小必须相同。 两个矩形都必须完全包含在其各自的结构内。 pminrow, pmincol, sminrow 或 smincol 的负值将被视为零。
- window.noutrefresh()¶
- window.noutrefresh(pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol)
标记为刷新但保持等待。 此函数会更新代表预期窗口状态的数据结构,但并不强制更新物理屏幕。 要完成后者,请调用
doupdate()。仅当窗口是使用
newpad()创建的面板时,才可以指定这 6 个参数(并且此时是必需的);它们的含义与refresh()相同。
Output options¶
- window.idlok(flag)¶
如果 flag 为
True,curses将尝试使用硬件行编辑功能。 否则,curses 将不使用它们。
- window.idcok(flag)¶
如果 flag 为
False,curses 将不再考虑使用终端的硬件插入/删除字符功能;如果 flag 为True,则会启用字符插入和删除。 当 curses 首次初始化时,默认会启用字符插入/删除。
- window.immedok(flag)¶
如果 flag 为
True,窗口图像中的任何改变都会自动导致窗口被刷新;你不必再自己调用refresh()。 但是,这可能会由于重复调用 wrefresh 而显著降低性能。 此选项默认被禁用。
- window.leaveok(flag)¶
如果 flag 为
True,则在更新时光标将停留在原地,而不是处于“光标位置”。 这在可能的情况下减少了光标移动。如果 flag 为
False,光标在更新后将总是位于“光标位置”。
- window.scrollok(flag)¶
控制当一个窗口的光标移出窗口或滚动区域边缘时会发生什么,这可能是在底端行执行换行操作,或者在最后一行输入最后一个字符导致的结果。 如果 flag 为
False,光标会留在底端行。 如果 flag 为True,窗口会向上滚动一行。 请注意为了在终端上获得实际的滚动效果,还需要调用idlok()。
- 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. Raise
curses.errorunless scrolling has been enabled for the window withscrollok().
- window.setscrreg(top, bottom)¶
设置从 top 行至 bottom 行的滚动区域。 所有滚动操作将在此区域中进行。
- window.getscrreg()¶
Return a tuple
(top, bottom)of the window's current scrolling region, as set bysetscrreg().Added in version 3.16.
Input options¶
- 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. Keypad mode is disabled by default, butwrapper()enables it for the main window.
- window.notimeout(flag)¶
如果 flag 为
True,则转义序列将不会发生超时。如果 flag 为
False,则在几毫秒之后,转义序列将不会被解析,并将保持在输入流中的原样。
Overlapping and touch¶
- 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.touchline(start, count[, changed])¶
假定从行 start 开始的 count 行已被更改。 如果提供了 changed,它将指明是将受影响的行标记为已更改 (changed
=True) 还是未更改 (changed=False)。
- window.touchwin()¶
假定整个窗口已被更改,其目的是用于绘制优化。
- window.syncup()¶
触碰已在窗口中被改变的此窗口的各个上级窗口中的所有位置。
- window.cursyncup()¶
更新窗口所有上级窗口的当前光标位置以反映窗口的当前光标位置。
Coordinate conversion¶
- window.enclose(y, x)¶
检测给定的相对屏幕的字符-单元格坐标是否被给定的窗口所包围,返回
True或False。 它适用于确定是哪个屏幕窗口子集包围着某个鼠标事件的位置。在 3.10 版本发生变更: 在之前版本中它会返回
1或0而不是True或False。
- window.mouse_trafo(y, x, to_screen)¶
Convert between window-relative and screen-relative (
stdscr-relative) character-cell coordinates. If to_screen is true, convert the window-relative coordinates y, x to screen-relative coordinates; otherwise convert in the opposite direction. The two coordinate systems differ when lines are reserved on the screen, for example for soft labels.Return the converted coordinates as a
(y, x)tuple, orNoneif they lie outside the window.Added in version 3.16.
Other¶
- window.encoding¶
在不支持宽字符的构建版本中,用于编码方法字符串参数并解码其结果的编码。 encoding 属性是在创建子窗口时从父窗口继承的,例如通过
window.subwin()。 在默认情况下,使用当前语言区域的编码(参见locale.getencoding())。Added in version 3.3.
- window.use(func, /, *args, **kwargs)¶
Call
func(window, *args, **kwargs)with the lock of the window held, and return its result. This provides automatic protection for the window against concurrent access from another thread.Availability: if the underlying curses library provides
use_window().Added in version 3.16.
State queries¶
- window.is_linetouched(line)¶
如果指定的行自上次调用
refresh()后发生了改变则返回True;否则返回False。 如果 line 对于给定的窗口不可用则会引发curses.error异常。
- window.is_notimeout()¶
Return the current value set by
notimeout().Added in version 3.16.
- window.is_scrollok()¶
Return the current value set by
scrollok().Added in version 3.16.
Screen objects¶
- class curses.screen¶
A screen object represents a terminal initialized by
newterm()(ornew_prescr()), in addition to the default screen created byinitscr(). Screen objects are returned by those functions; they cannot be instantiated directly.A screen is freed automatically once it is no longer referenced, either directly or through one of its windows. Each window keeps its screen alive, so a screen remains valid as long as any of its windows does.
Added in version 3.16.
- screen.close()¶
Detach the screen's standard window, breaking the reference cycle between them so the screen can be reclaimed promptly instead of waiting for a garbage collection. Afterwards
stdscrisNoneand the window it returned earlier can no longer be used. The screen's resources are released once it and all its windows are no longer referenced.Added in version 3.16.
- screen.stdscr¶
The standard window of the screen, covering the whole terminal, or
Nonefor a screen created bynew_prescr().
- screen.use(func, /, *args, **kwargs)¶
Call
func(screen, *args, **kwargs)with the lock of the screen held, and return its result. This provides automatic protection for the screen against concurrent access from another thread.Availability: if the underlying curses library provides
use_screen().Added in version 3.16.
Complex character objects¶
- class curses.complexchar(text, /, attr=0, pair=0)¶
A complex character (or complexchar) is an immutable styled character cell: a spacing character optionally followed by combining characters, together with a set of attributes and a color pair.
text is the cell's text, attr a combination of the WA_* attributes (equivalent to the matching
A_*constants), and pair a color pair number. Unlike the packedchtypeused byinch()and theA_*methods, the color pair is stored separately and is not limited to the value that fits in acolor_pair().Complex characters are returned by
window.in_wch()andwindow.getbkgrnd(), and are accepted (along with an integer, a byte or a string) by the character-cell methods such aswindow.addch(),window.insch(),window.bkgd(),window.border(),window.hline()andwindow.vline(). A complex character already carries its own rendition, so it cannot be combined with an explicit attr argument.str()returns the cell's text; two complex characters are equal when their text, attributes and color pair all match.The same code works on both wide- and narrow-character builds. On a narrow build a cell holds a single character (no combining marks) that must encode to one byte in the window's encoding (8-bit locales only), and pair is limited to the value that fits in a
color_pair().- attr¶
The attributes of the character cell (read-only).
- pair¶
The color pair number of the character cell (read-only).
Added in version 3.16.
- class curses.complexstr(cells[, attr[, pair]])¶
A complex character string (or complexstr) is an immutable sequence of styled character cells -- the string counterpart of
complexchar(asstris to a single character).If cells is a string, it is split into character cells (each a spacing character optionally followed by combining characters), and attr (a combination of the WA_* attributes) and pair (a color pair number), if given, are applied to every cell.
Otherwise cells is an iterable whose items are themselves cells, each a
complexcharor a string; each item then carries its own rendition, and attr and pair must be omitted.It is returned by
window.in_wchstr(), and accepted bywindow.addstr(),addnstr(),insstr()andinsnstr(), so a run read from a window can be written back unchanged.It behaves like an immutable sequence:
len(s)is the number of cells,s[i]is the i-th cell as acomplexchar, slicing and concatenation produce newcomplexstrinstances, and iterating yields the cells.str()returns the cells' text joined together, and two complex character strings are equal when their cells all match. It is hashable.To build or edit a run of cells, use an ordinary
listofcomplexchar(or strings); acomplexstris the immutable form returned by a read.Like
complexchar, this type works on both wide- and narrow-character builds, with the same per-cell limitations on a narrow build.Added in version 3.16.
常量¶
General¶
curses 模块定义了下列数据成员:
- 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¶
屏幕的宽度,即列数。 它只有在调用
initscr()之后才会被定义。 由update_lines_cols()、resizeterm()和resize_term()更新。
- curses.LINES¶
屏幕的高度,即行数。 它只有在调用
initscr()之后才会被定义。 由update_lines_cols()、resizeterm()和resize_term()更新。
Attributes¶
有些常量可用于指定字符单元属性。 实际可用的常量取决于具体的系统。
属性 |
Wide attribute |
含意 |
|---|---|---|
|
|
备用字符集模式 |
|
|
闪烁模式 |
|
|
粗体模式 |
|
|
暗淡模式 |
|
|
不可见或空白模式 |
|
|
斜体模式 |
|
|
正常属性 |
|
|
保护模式 |
|
|
反转背景色和前景色 |
|
|
突出模式 |
|
|
下划线模式 |
|
|
水平突出显示 |
|
|
左高亮 |
|
|
底部高亮 |
|
|
右高亮 |
|
|
顶部高亮 |
|
|
垂直突出显示 |
Added in version 3.7: 新增 A_ITALIC。
The attr_get(), attr_set(), attr_on()
and attr_off() methods use the parallel set of WA_* constants
listed above.
Each has the same meaning as the corresponding A_* attribute
(WA_BOLD like A_BOLD, and so on), but belongs to the
attr_t type rather than being packed into a character.
In ncurses the two sets share the same values, but other curses implementations
may give them different ones, so use the WA_* constants with the attr_*
methods.
Added in version 3.16: The WA_* constants were added.
有几个常量可用于提取某些方法返回的相应属性。
位掩码 |
Wide bit-mask |
含意 |
|---|---|---|
|
|
用于提取属性的位掩码 |
|
用于提取字符的位掩码 |
|
|
用于提取颜色对字段信息的位掩码 |
Keys¶
键由名称以 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 |
Alternate character set¶
下表列出了替代字符集中的字符。 这些字符继承自 VT100 终端,在 X 终端等软件模拟器上通常均为可用。 当没有可用的图形时,curses 会回退为粗糙的可打印 ASCII 近似符号。
Every character has two names.
The ACS_* code is an integer character, restricted to the 8-bit
alternate character set of the terminal.
The WACS_* code is the same character as a complexchar cell,
which is not restricted to the alternate character set.
备注
These are available only after initscr() has been called.
The WACS_* codes are only available if Python is built with
wide character support.
Added in version 3.16: The WACS_* codes.
ACS代码 |
WACS code |
含意 |
|---|---|---|
|
|
右上角的别名 |
|
|
实心方块 |
|
|
正方形 |
|
|
水平线的别名 |
|
|
左上角的别名 |
|
|
顶部 T 型的别名 |
|
|
底部 T 型 |
|
|
正方形 |
|
|
棋盘(点刻) |
|
|
向下箭头 |
|
|
度数符号 |
|
|
菱形 |
|
|
大于或等于 |
|
|
水平线 |
|
|
灯形符号 |
|
|
向左箭头 |
|
|
小于或等于 |
|
|
左下角 |
|
|
右下角 |
|
|
左侧 T 型 |
|
|
不等号 |
|
|
字母π |
|
|
正负号 |
|
|
加号 |
|
|
向右箭头 |
|
|
右侧 T 型 |
|
|
扫描线 1 |
|
|
扫描线3 |
|
|
扫描线7 |
|
|
扫描线 9 |
|
|
右下角的别名 |
|
|
垂直线的别名 |
|
|
右侧 T 型的别名 |
|
|
左下角的别名 |
|
|
底部 T 型的别名 |
|
|
左侧 T 型的别名 |
|
|
交叉或大加号的替代名称 |
|
|
英镑 |
|
|
顶部 T 型 |
|
|
向上箭头 |
|
|
左上角 |
|
|
右上角 |
|
|
垂线 |
The following table lists the double-line and thick-line characters.
They have no ACS_* counterpart, and are not provided by every implementation.
As in the table above, the alternate name spells out the four sides of the
character, clockwise from the top:
B for a blank side, S for a single line, D for a double line and
T for a thick line.
WACS code |
Alternate name |
含意 |
|---|---|---|
|
|
double-line bottom tee |
|
|
double-line horizontal line |
|
|
double-line lower-left corner |
|
|
double-line lower-right corner |
|
|
double-line left tee |
|
|
double-line big plus sign |
|
|
double-line right tee |
|
|
double-line top tee |
|
|
double-line upper-left corner |
|
|
double-line upper-right corner |
|
|
double-line vertical line |
|
|
thick-line bottom tee |
|
|
thick-line horizontal line |
|
|
thick-line lower-left corner |
|
|
thick-line lower-right corner |
|
|
thick-line left tee |
|
|
thick-line big plus sign |
|
|
thick-line right tee |
|
|
thick-line top tee |
|
|
thick-line upper-left corner |
|
|
thick-line upper-right corner |
|
|
thick-line vertical line |
Colors¶
下表列出了预定义的颜色:
常量 |
颜色 |
|---|---|
|
黑色 |
|
蓝色 |
|
青色(浅绿蓝色) |
|
绿色 |
|
洋红色(紫红色) |
|
红色 |
|
白色 |
|
黄色 |
curses.textpad --- 用于 curses 程序的文本输入控件¶
curses.textpad 模块提供了一个 Textbox 类,该类在 curses 窗口中处理基本的文本编辑,支持一组与 Emacs 类似的键绑定(因此这也适用于 Netscape Navigator, BBedit 6.x, FrameMaker 和许多其他程序)。 该模块还提供了一个绘制矩形的函数,适用于容纳文本框或其他目的。
curses.textpad 模块定义了以下函数:
- curses.textpad.rectangle(win, uly, ulx, lry, lrx)¶
绘制一个矩形。 第一个参数必须是窗口对象;其余参数是相对于该窗口的坐标。 第二个和第三个参数是要绘制的矩形左上角的 y 和 x 坐标;第四个和第五个参数是右下角的 y 和 x 坐标。 在支持的终端上(包括 xterm 和大多数其他软件终端模拟器),将使用 VT100/IBM PC 表格字符绘制矩形。 否则将使用 ASCII 破折号、竖线和加号绘制。
文本框对象¶
你可以通过如下方式实例化一个 Textbox:
- class curses.textpad.Textbox(win, insert_mode=False)¶
返回一个文本框微件对象。 win 参数应该是一个包含文本框的 curses 窗口 对象。 如果 insert_mode 为真,文本框将插入输入的字符,并将现有文本向右移动,而不是覆盖它。 文本框的编辑光标最初位于包含窗口的左上角,坐标为
(0, 0)。 实例的stripspaces标志最初处于开启状态。在 3.16 版本发生变更: Entering and reading back the full Unicode range, including combining characters, is now supported when curses is built with wide-character support.
Textbox对象具有以下方法:- edit(validate=None)¶
这是你通常使用的入口点。 它接受编辑击键,直到输入终止击键之一。 如果提供了 validate,它必须是一个函数。 将对输入的每个击键调用该函数,并以该击键作为参数;根据结果进行命令分发。 如果它返回假值,则忽略该击键。 该方法将窗口内容作为字符串返回;窗口中的空白是否包含受
stripspaces属性影响。在 3.16 版本发生变更: validate is now called with a non-ASCII character as a string; other keystrokes are still passed as an integer.
- do_command(ch)¶
处理单个命令击键。 如果处理了终止击键,则返回
0,否则返回1以继续编辑。 以下是受支持的特殊击键:按键
动作
Control-A
转到窗口的左边缘。
Control-B
光标向左,如果可能,包含前一行。
Control-D
删除光标下的字符。
Control-E
前往右边缘(stripspaces 关闭时)或者行尾(stripspaces 启用时)。
Control-F
向右移动光标,适当时换行到下一行。
Control-G
终止,返回窗口内容。
Control-H
向后删除字符。
Control-J
如果窗口只有 1 行则终止,否则移动到下一行的开头。
Control-K
如果行为空,则删除它,否则清除到行尾。
Control-L
刷新屏幕。
Control-N
光标向下;向下移动一行。
Control-O
在光标位置插入一个空行。
Control-P
光标向上;向上移动一行。
如果光标位于无法移动的边缘,则移动操作不执行任何操作。在可能的情况下,支持以下同义词:
常量
按键
Control-B
Control-F
Control-P
Control-N
Control-h
所有其他按键将被视为插入给定字符并右移的命令(带有自动折行)。
- gather()¶
以字符串形式返回窗口内容;是否包括窗口中的空白将受到
stripspaces成员的影响。
- stripspaces¶
此属性是控制窗口中空白解读方式的旗标。 当启用时,每一行的末尾空白会被忽略;任何将光标定位至末尾空白的光标动作都将改为前往该行末尾,并且在收集窗口内容时将去除末尾空白。