curses
--- 终端字符单元显示的处理¶
curses
模块提供了 curses 库的接口,这是可移植高级终端处理的事实标准。
虽然 curses 在 Unix 环境中使用最为广泛,但也有适用于 Windows,DOS 以及其他可能的系统的版本。此扩展模块旨在匹配 ncurses 的 API,这是一个部署在 Linux 和 Unix 的 BSD 变体上的开源 curses 库。
備註
每当文档提到 字符 时,它可以被指定为一个整数,一个单字符 Unicode 字符串或者一个单字节的字节字符串。
每当此文档提到 字符串 时,它可以被指定为一个 Unicode 字符串或者一个字节字符串。
備註
从 5.4 版本开始,ncurses 库使用 nl_langinfo
函数来决定如何解释非 ASCII 数据。这意味着你需要在程序中调用 locale.setlocale()
函数,并使用一种系统中可用的编码方法来编码 Unicode 字符串。这个例子使用了系统默认的编码:
import locale
locale.setlocale(locale.LC_ALL, '')
code = locale.getpreferredencoding()
然后使用 code 作为 str.encode()
调用的编码。
也參考
- 模块
curses.ascii
在 ASCII 字符上工作的工具,无论你的区域设置是什么。
- 模块
curses.panel
为 curses 窗口添加深度的面板栈扩展。
- 模块
curses.textpad
用于使 curses 支持 Emacs 式绑定的可编辑文本部件。
- 用 Python 进行 Curses 编程
关于配合 Python 使用 curses 的教学材料,由 Andrew Kuchling 和 Eric Raymond 撰写。
Python 源码发布包的 Tools/demo/ 目录包含了一些使用此模块所提供的 curses 绑定的示例程序。
函数¶
curses
模块定义了以下异常:
-
exception
curses.
error
¶ 当 curses 库中函数返回一个错误时引发的异常。
備註
只要一个函数或方法的 x 或 y 参数是可选项,它们会默认为当前光标位置。 而当 attr 是可选项时,它会默认为 A_NORMAL
。
curses
模块定义了以下函数:
-
curses.
baudrate
()¶ 以每秒比特数为单位返回终端输出速度。 在软件终端模拟器上它将具有一个固定的最高值。 此函数出于历史原因被包括;在以前,它被用于写输出循环以提供时间延迟,并偶尔根据线路速度来改变接口。
-
curses.
beep
()¶ 发出短促的提醒声音。
-
curses.
can_change_color
()¶ 根据程序员能否改变终端显示的颜色返回
True
或False
。
-
curses.
cbreak
()¶ 进入 cbreak 模式。 在 cbreak 模式(有时也称为“稀有”模式)通常的 tty 行缓冲会被关闭并且字符可以被一个一个地读取。 但是,与原始模式不同,特殊字符(中断、退出、挂起和流程控制)会在 tty 驱动和调用程序上保留其效果。 首先调用
raw()
然后调用cbreak()
会将终端置于 cbreak 模式。
-
curses.
color_content
(color_number)¶ 返回颜色值 color_number 中红、绿和蓝(RGB)分量的强度,此强度值必须介于
0
和COLORS
之间。 返回一个 3 元组,其中包含给定颜色的 R,G,B 值,它们必须介于0
(无分量) 和1000
(最大分量) 之间。
-
curses.
color_pair
(color_number)¶ 返回用于显示指定颜色的文本的属性值。 该属性值可与
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
()¶ 更新物理屏幕。 curses 库会保留两个数据结构,一个代表当前物理屏幕的内容以及一个虚拟屏幕代表需要的后续状态。
doupdate()
整体更新物理屏幕以匹配虚拟屏幕。虚拟屏幕可以通过在写入操作例如在一个窗口上执行
addstr()
之后调用noutrefresh()
来刷新。 普通的refresh()
调用只是简单的noutrefresh()
加doupdate()
;如果你需要更新多个窗口,你可以通过在所有窗口上发出noutrefresh()
调用再加单次doupdate()
来提升性能并可减少屏幕闪烁。
-
curses.
echo
()¶ 进入 echo 模式。 在 echo 模式下,输入的每个字符都会在输入后回显到屏幕上。
-
curses.
endwin
()¶ 撤销库的初始化,使终端返回正常状态。
-
curses.
erasechar
()¶ 将用户的当前擦除字符以单字节字节串对象的形式返回。 在 Unix 操作系统下这是 curses 程序用来控制 tty 的属性,而不是由 curses 库本身来设置的。
-
curses.
filter
()¶ 如果要使用
filter()
例程,它必须在调用initscr()
之前被调用。 其效果是在这些调用期间,LINES
会被设为1
;clear
,cup
,cud
,cud1
,cuu1
,cuu
,vpa
等功能会被禁用;而home
字符串会被设为cr
的值。 其影响是光标会被限制在当前行内,屏幕刷新也是如此。 这可被用于启用单字符模式的行编辑而不触及屏幕的其余部分。
-
curses.
flushinp
()¶ 刷新所有输入缓冲区。 这会丢弃任何已被用户输入但尚未被程序处理的预输入内容。
-
curses.
getmouse
()¶ 在
getch()
返回KEY_MOUSE
以发出鼠标事件信号之后,应当调用此方法来获取加入队列的鼠标事件,事件以一个 5 元组(id, x, y, z, bstate)
来表示。 id 为用于区分多个设置的 ID 值, x, y, z 为事件的坐标。 (z 目前未被使用。) bstate 为一个整数值,其各比特位将设置用于表示事件类型,并将是下列常量中一个或多个按位 OR 的结果,其中 n 是以 1 至 4 表示的键号:BUTTONn_PRESSED
,BUTTONn_RELEASED
,BUTTONn_CLICKED
,BUTTONn_DOUBLE_CLICKED
,BUTTONn_TRIPLE_CLICKED
,BUTTON_SHIFT
,BUTTON_CTRL
,BUTTON_ALT
。
-
curses.
getwin
(file)¶ 读取由之前的
putwin()
调用存放在文件中的窗口相关数据。 该例程随后将使用该数据创建并初始化一个新窗口,并返回该新窗口对象。
-
curses.
has_colors
()¶ 如果终端能显示彩色则返回
True
;否则返回False
。
-
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
之间的数字。 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
号颜色对固定为黑底白字而无法更改)。 fg 和 bg 参数值必须为0
和COLORS
之间的数字。 如果颜色对之前已被初始化,则屏幕会被刷新使得出现在屏幕上的该颜色对会立即按照新定义来更改。
-
curses.
is_term_resized
(nlines, ncols)¶ 如果
resize_term()
会修改窗口结构则返回True
,否则返回False
。
-
curses.
keyname
(k)¶ 将编号为 k 的键名称作为字节串对象返回。 生成可打印 ASCII 字符的键名称就是键所对应的字符。 Ctrl-键组合的键名称则是一个两字节的字节串对象,它由插入符 (
b'^'
) 加对应的可打印 ASCII 字符组成。 Alt-键组合 (128--255) 的键名称则是由前缀b'M-'
加对应的可打印 ASCII 字符组成的字节串对象。
-
curses.
killchar
()¶ 将用户的当前行删除字符以单字节字节串对象的形式返回。 在 Unix 操作系统下这是 curses 程序用来控制 tty 的属性,而不是由 curses 库本身来设置的。
-
curses.
longname
()¶ 返回一个字节串对象,其中包含描述当前终端的 terminfo 长名称字段。 详细描述的最大长度为 128 个字符。 它仅在调用
initscr()
之后才会被定义。
-
curses.
meta
(flag)¶ 如果 flag 为
True
,则允许输入 8 比特位的字符。 如果 flag 为False
,则只允许 7 比特位的字符。
-
curses.
mouseinterval
(interval)¶ 以毫秒为单位设置能够被识别为点击的按下和释放事件之间可以间隔的最长时间,并返回之前的间隔值。 默认值为 200 毫秒,即五分之一秒。
-
curses.
mousemask
(mousemask)¶ 设置要报告的鼠标事件,并返回一个元组
(availmask, oldmask)
。 availmask 表明指定的鼠标事件中哪些可以被报告;当完全失败时将返回0
。 oldmask 是给定窗口的鼠标事件之前的掩码值。 如果从未调用此函数,则不会报告任何鼠标事件。
-
curses.
napms
(ms)¶ 休眠 ms 毫秒。
-
curses.
newpad
(nlines, ncols)¶ 创建并返回一个指向具有给定行数和列数新的填充数据结构的指针。 将填充作为窗口对象返回。
面板类似于窗口,区别在于它不受屏幕大小的限制,并且不必与屏幕的特定部分相关联。 面板可以在需要使用大窗口时使用,并且每次只需将窗口的一部分放在屏幕上。 面板不会发生自动刷新(例如由于滚动或输入回显)。 面板的
refresh()
和noutrefresh()
方法需要 6 个参数来指定面板要显示的部分以及要用于显示的屏幕位置。 这些参数是 pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol;p 参数表示要显示的面板区域的左上角而 s 参数定义了要显示的面板区域在屏幕上的剪切框。
-
curses.
newwin
(nlines, ncols)¶ -
curses.
newwin
(nlines, ncols, begin_y, begin_x) 返回一个新的 窗口,其左上角位于
(begin_y, begin_x)
,并且其高度/宽度为 nlines/ncols。默认情况下,窗口将从指定位置扩展到屏幕的右下角。
-
curses.
nl
()¶ 进入换行模式。 此模式会在输入时将回车转换为换行符,并在输出时将换行符转换为回车加换行。 换行模式会在初始时启用。
-
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 的值必须在1
和COLOR_PAIRS - 1
之间。
-
curses.
pair_number
(attr)¶ 返回通过属性值 attr 所设置的颜色对的编号。
color_pair()
是此函数的对应操作。
-
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.
setupterm
(term=None, fd=-1)¶ 初始化终端。 term 为给出终端名称的字符串或为
None
;如果省略或为None
,则将使用TERM
环境变量的值。 fd 是任何初始化序列将被发送到的文件描述符;如未指定或为-1
,则将使用sys.stdout
的文件描述符。
-
curses.
start_color
()¶ 如果程序员想要使用颜色,则必须在任何其他颜色操作例程被调用之前调用它。 在
initscr()
之后立即调用此例程是一个很好的做法。start_color()
initializes eight basic colors (black, red, green, yellow, blue, magenta, cyan, and white), and two global variables in thecurses
module,COLORS
andCOLOR_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
()¶ Return a logical OR of all video attributes supported by the terminal. This information is useful when a curses program needs complete control over the appearance of the screen.
-
curses.
termname
()¶ Return the value of the environment variable
TERM
, as a bytes object, truncated to 14 characters.
-
curses.
tigetflag
(capname)¶ Return the value of the Boolean capability corresponding to the terminfo capability name capname as an integer. Return the value
-1
if capname is not a Boolean capability, or0
if it is canceled or absent from the terminal description.
-
curses.
tigetnum
(capname)¶ Return the value of the numeric capability corresponding to the terminfo capability name capname as an integer. Return the value
-2
if capname is not a numeric capability, or-1
if it is canceled or absent from the terminal description.
-
curses.
tigetstr
(capname)¶ Return the value of the string capability corresponding to the terminfo capability name capname as a bytes object. Return
None
if capname is not a terminfo "string capability", or is canceled or absent from the terminal description.
-
curses.
tparm
(str[, ...])¶ Instantiate the bytes object str with the supplied parameters, where str should be a parameterized string obtained from the terminfo database. E.g.
tparm(tigetstr("cup"), 5, 3)
could result inb'\033[6;4H'
, the exact result depending on terminal type.
-
curses.
typeahead
(fd)¶ Specify that the file descriptor fd be used for typeahead checking. If fd is
-1
, then no typeahead checking is done.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 or doupdate is called again, allowing faster response to commands typed in advance. This function allows specifying a different file descriptor for typeahead checking.
-
curses.
unctrl
(ch)¶ Return a bytes object which is a printable representation of the character ch. Control characters are represented as a caret followed by the character, for example as
b'^C'
. Printing characters are left as they are.
-
curses.
ungetch
(ch)¶ Push ch so the next
getch()
will return it.備註
Only one ch can be pushed before
getch()
is called.
-
curses.
update_lines_cols
()¶ Update
LINES
andCOLS
. Useful for detecting manual screen resize.3.5 版新加入.
-
curses.
unget_wch
(ch)¶ Push ch so the next
get_wch()
will return it.備註
Only one ch can be pushed before
get_wch()
is called.3.3 版新加入.
-
curses.
ungetmouse
(id, x, y, z, bstate)¶ Push a
KEY_MOUSE
event onto the input queue, associating the given state data with it.
-
curses.
use_env
(flag)¶ If used, this function should be called before
initscr()
or newterm are called. When flag isFalse
, the values of lines and columns specified in the terminfo database will be used, even if environment variablesLINES
andCOLUMNS
(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 ifLINES
andCOLUMNS
are not set).
-
curses.
use_default_colors
()¶ Allow use of default values for colors on terminals supporting this feature. Use this to support transparency in your application. The default color is assigned to the color number
-1
. After calling this function,init_pair(x, curses.COLOR_RED, -1)
initializes, for instance, color pair x to a red foreground color on the default background.
-
curses.
wrapper
(func, ...)¶ Initialize curses and call another callable object, func, which should be the rest of your curses-using application. If the application raises an exception, this function will restore the terminal to a sane state before re-raising the exception and generating a traceback. The callable object func is then passed the main window 'stdscr' as its first argument, followed by any other arguments passed to
wrapper()
. Before calling func,wrapper()
turns on cbreak mode, turns off echo, enables the terminal keypad, and initializes colors if the terminal has color support. On exit (whether normally or by exception) it restores cooked mode, turns on echo, and disables the terminal keypad.
Window Objects¶
Window objects, as returned by initscr()
and newwin()
above, have
the following methods and attributes:
-
window.
addch
(ch[, attr])¶ -
window.
addch
(y, x, ch[, attr]) Paint character ch at
(y, x)
with attributes attr, overwriting any character previously painted at that location. By default, the character position and attributes are the current settings for the window object.備註
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]) Paint at most n characters of the character string str at
(y, x)
with attributes attr, overwriting anything previously on the display.
-
window.
addstr
(str[, attr])¶ -
window.
addstr
(y, x, str[, attr]) Paint the character string str at
(y, x)
with attributes attr, overwriting anything previously on the display.備註
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, can cause SegFaults when resizing windows. This is fixed in ncurses-6.1-20190511. If you are stuck with an earlier ncurses, you can avoid triggering this if you do not call
addstr()
with a str that has embedded newlines. Instead, calladdstr()
separately for each line.
-
window.
attroff
(attr)¶ Remove attribute attr from the "background" set applied to all writes to the current window.
-
window.
attron
(attr)¶ Add attribute attr from the "background" set applied to all writes to the current window.
-
window.
attrset
(attr)¶ Set the "background" set of attributes to attr. This set is initially
0
(no attributes).
-
window.
bkgd
(ch[, attr])¶ Set the background property of the window to the character ch, with attributes attr. The change is then applied to every character position in that window:
The attribute of every character in the window is changed to the new background attribute.
Wherever the former background character appears, it is changed to the new background character.
-
window.
bkgdset
(ch[, attr])¶ Set the window's background. A window's background consists of a character and any combination of attributes. The attribute part of the background is combined (OR'ed) with all non-blank characters that are written into the window. Both the character and attribute parts of the background are combined with the blank characters. The background becomes a property of the character and moves with the character through any scrolling and insert/delete line/character operations.
-
window.
border
([ls[, rs[, ts[, bs[, tl[, tr[, bl[, br]]]]]]]])¶ 在窗口边缘绘制边框。每个参数指定用于边界特定部分的字符;请参阅下表了解更多详情。
備註
A
0
value for any parameter will cause the default character to be used for that parameter. Keyword parameters can not be used. The defaults are listed in this table:参数
描述
默认值
ls
左侧
ACS_VLINE
rs
右侧
ACS_VLINE
ts
顶部
ACS_HLINE
bs
底部
ACS_HLINE
tl
左上角
ACS_ULCORNER
tr
右上角
ACS_URCORNER
bl
左下角
ACS_LLCORNER
br
右下角
ACS_LRCORNER
-
window.
box
([vertch, horch])¶ Similar to
border()
, but both ls and rs are vertch and both ts and bs are horch. The default corner characters are always used by this function.
-
window.
chgat
(attr)¶ -
window.
chgat
(num, attr) -
window.
chgat
(y, x, attr) -
window.
chgat
(y, x, num, attr) Set the attributes of num characters at the current cursor position, or at position
(y, x)
if supplied. If num is not given or is-1
, the attribute will be set on all the characters to the end of the line. This function moves cursor to position(y, x)
if supplied. The changed line will be touched using thetouchline()
method so that the contents will be redisplayed by the next window refresh.
-
window.
clear
()¶ Like
erase()
, but also cause the whole window to be repainted upon next call torefresh()
.
-
window.
clrtobot
()¶ Erase from cursor to the end of the window: all lines below the cursor are deleted, and then the equivalent of
clrtoeol()
is performed.
-
window.
clrtoeol
()¶ Erase from cursor to the end of the line.
-
window.
cursyncup
()¶ Update the current cursor position of all the ancestors of the window to reflect the current cursor position of the window.
-
window.
delch
([y, x])¶ Delete any character at
(y, x)
.
-
window.
deleteln
()¶ Delete the line under the cursor. All following lines are moved up by one line.
-
window.
derwin
(begin_y, begin_x)¶ -
window.
derwin
(nlines, ncols, begin_y, begin_x) An abbreviation for "derive window",
derwin()
is the same as callingsubwin()
, except that begin_y and begin_x are relative to the origin of the window, rather than relative to the entire screen. Return a window object for the derived window.
-
window.
echochar
(ch[, attr])¶ Add character ch with attribute attr, and immediately call
refresh()
on the window.
-
window.
enclose
(y, x)¶ Test whether the given pair of screen-relative character-cell coordinates are enclosed by the given window, returning
True
orFalse
. It is useful for determining what subset of the screen windows enclose the location of a mouse event.
-
window.
encoding
¶ Encoding used to encode method arguments (Unicode strings and characters). The encoding attribute is inherited from the parent window when a subwindow is created, for example with
window.subwin()
. By default, the locale encoding is used (seelocale.getpreferredencoding()
).3.3 版新加入.
-
window.
erase
()¶ Clear the window.
-
window.
getbegyx
()¶ Return a tuple
(y, x)
of co-ordinates of upper-left corner.
-
window.
getbkgd
()¶ Return the given window's current background character/attribute pair.
-
window.
getch
([y, x])¶ Get a character. Note that the integer returned does not have to be in ASCII range: function keys, keypad keys and so on are represented by numbers higher than 255. In no-delay mode, return
-1
if there is no input, otherwise wait until a key is pressed.
-
window.
get_wch
([y, x])¶ Get a wide character. Return a character for most keys, or an integer for function keys, keypad keys, and other special keys. In no-delay mode, raise an exception if there is no input.
3.3 版新加入.
-
window.
getkey
([y, x])¶ Get a character, returning a string instead of an integer, as
getch()
does. Function keys, keypad keys and other special keys return a multibyte string containing the key name. In no-delay mode, raise an exception if there is no input.
-
window.
getmaxyx
()¶ Return a tuple
(y, x)
of the height and width of the window.
-
window.
getparyx
()¶ Return the beginning coordinates of this window relative to its parent window as a tuple
(y, x)
. Return(-1, -1)
if this window has no parent.
-
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.
-
window.
getyx
()¶ Return a tuple
(y, x)
of current cursor position relative to the window's upper-left corner.
-
window.
hline
(ch, n)¶ -
window.
hline
(y, x, ch, n) Display a horizontal line starting at
(y, x)
with length n consisting of the character ch.
-
window.
idcok
(flag)¶ If flag is
False
, curses no longer considers using the hardware insert/delete character feature of the terminal; if flag isTrue
, use of character insertion and deletion is enabled. When curses is first initialized, use of character insert/delete is enabled by default.
-
window.
idlok
(flag)¶ If flag is
True
,curses
will try and use hardware line editing facilities. Otherwise, line insertion/deletion are disabled.
-
window.
immedok
(flag)¶ If flag is
True
, any change in the window image automatically causes the window to be refreshed; you no longer have to callrefresh()
yourself. However, it may degrade performance considerably, due to repeated calls to wrefresh. This option is disabled by default.
-
window.
inch
([y, x])¶ Return the character at the given position in the window. The bottom 8 bits are the character proper, and upper bits are the attributes.
-
window.
insch
(ch[, attr])¶ -
window.
insch
(y, x, ch[, attr]) Paint character ch at
(y, x)
with attributes attr, moving the line from position x right by one character.
-
window.
insdelln
(nlines)¶ Insert nlines lines into the specified window above the current line. The nlines bottom lines are lost. For negative nlines, delete nlines lines starting with the one under the cursor, and move the remaining lines up. The bottom nlines lines are cleared. The current cursor position remains the same.
-
window.
insertln
()¶ Insert a blank line under the cursor. All following lines are moved down by one line.
-
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 zero or 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).
-
window.
insstr
(str[, attr])¶ -
window.
insstr
(y, x, str[, attr]) Insert a character string (as many characters as will fit on the line) before the character under the cursor. 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).
-
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. Attributes are stripped from the characters. If n is specified,
instr()
returns a string at most n characters long (exclusive of the trailing NUL).
-
window.
is_linetouched
(line)¶ Return
True
if the specified line was modified since the last call torefresh()
; otherwise returnFalse
. Raise acurses.error
exception if line is not valid for the given window.
-
window.
is_wintouched
()¶ Return
True
if the specified window was modified since the last call torefresh()
; otherwise returnFalse
.
-
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. If possible the cursor will be made invisible.If flag is
False
, cursor will always be at "cursor position" after an update.
-
window.
move
(new_y, new_x)¶ Move cursor to
(new_y, new_x)
.
-
window.
mvderwin
(y, x)¶ Move the window inside its parent window. The screen-relative parameters of the window are not changed. This routine is used to display different parts of the parent window at the same physical position on the screen.
-
window.
mvwin
(new_y, new_x)¶ Move the window so its upper-left corner is at
(new_y, new_x)
.
-
window.
notimeout
(flag)¶ If flag is
True
, escape sequences will not be timed out.If flag is
False
, after a few milliseconds, an escape sequence will not be interpreted, and will be left in the input stream as is.
-
window.
noutrefresh
()¶ Mark for refresh but wait. This function updates the data structure representing the desired state of the window, but does not force an update of the physical screen. To accomplish that, call
doupdate()
.
-
window.
overlay
(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])¶ Overlay the window on top of destwin. The windows need not be the same size, only the overlapping region is copied. This copy is non-destructive, which means that the current background character does not overwrite the old contents of destwin.
To get fine-grained control over the copied region, the second form of
overlay()
can be used. sminrow and smincol are the upper-left coordinates of the source window, and the other variables mark a rectangle in the destination window.
-
window.
overwrite
(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])¶ Overwrite the window on top of destwin. The windows need not be the same size, in which case only the overlapping region is copied. This copy is destructive, which means that the current background character overwrites the old contents of destwin.
To get fine-grained control over the copied region, the second form of
overwrite()
can be used. sminrow and smincol are the upper-left coordinates of the source window, the other variables mark a rectangle in the destination window.
-
window.
putwin
(file)¶ Write all data associated with the window into the provided file object. This information can be later retrieved using the
getwin()
function.
-
window.
redrawln
(beg, num)¶ Indicate that the num screen lines, starting at line beg, are corrupted and should be completely redrawn on the next
refresh()
call.
-
window.
redrawwin
()¶ Touch the entire window, causing it to be completely redrawn on the next
refresh()
call.
-
window.
refresh
([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])¶ Update the display immediately (sync actual screen with previous drawing/deleting methods).
The 6 optional arguments can only be specified 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-hand 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-hand 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)¶ Reallocate storage for a curses window to adjust its dimensions to the specified values. If either dimension is larger than the current values, the window's data is filled with blanks that have the current background rendition (as set by
bkgdset()
) merged into them.
-
window.
scroll
([lines=1])¶ Scroll the screen or scrolling region upward by lines lines.
-
window.
scrollok
(flag)¶ Control what happens when the cursor of a window is moved off the edge of the window or scrolling region, either as a result of a newline action on the bottom line, or typing the last character of the last line. If flag is
False
, the cursor is left on the bottom line. If flag isTrue
, the window is scrolled up one line. Note that in order to get the physical scrolling effect on the terminal, it is also necessary to callidlok()
.
-
window.
setscrreg
(top, bottom)¶ Set the scrolling region from line top to line bottom. All scrolling actions will take place in this region.
-
window.
standend
()¶ Turn off the standout attribute. On some terminals this has the side effect of turning off all attributes.
-
window.
standout
()¶ Turn on attribute A_STANDOUT.
-
window.
subpad
(begin_y, begin_x)¶ -
window.
subpad
(nlines, ncols, begin_y, begin_x) Return a sub-window, whose upper-left corner is at
(begin_y, begin_x)
, and whose width/height is ncols/nlines.
-
window.
subwin
(begin_y, begin_x)¶ -
window.
subwin
(nlines, ncols, begin_y, begin_x) Return a sub-window, whose upper-left corner is at
(begin_y, begin_x)
, and whose width/height is ncols/nlines.By default, the sub-window will extend from the specified position to the lower right corner of the window.
-
window.
syncdown
()¶ Touch each location in the window that has been touched in any of its ancestor windows. This routine is called by
refresh()
, so it should almost never be necessary to call it manually.
-
window.
syncok
(flag)¶ If flag is
True
, thensyncup()
is called automatically whenever there is a change in the window.
-
window.
syncup
()¶ Touch all locations in ancestors of the window that have been changed in the window.
-
window.
timeout
(delay)¶ Set blocking or non-blocking read behavior for the window. If delay is negative, blocking read is used (which will wait indefinitely for input). If delay is zero, then non-blocking read is used, and
getch()
will return-1
if no input is waiting. If delay is positive, thengetch()
will block for delay milliseconds, and return-1
if there is still no input at the end of that time.
-
window.
touchline
(start, count[, changed])¶ Pretend count lines have been changed, starting with line start. If changed is supplied, it specifies whether the affected lines are marked as having been changed (changed
=True
) or unchanged (changed=False
).
-
window.
touchwin
()¶ Pretend the whole window has been changed, for purposes of drawing optimizations.
-
window.
vline
(ch, n)¶ -
window.
vline
(y, x, ch, n) Display a vertical line starting at
(y, x)
with length n consisting of the character ch.
常量¶
The curses
module defines the following data members:
-
curses.
version
¶ A bytes object representing the current version of the module. Also available as
__version__
.
-
curses.
ncurses_version
¶ A named tuple containing the three components of the ncurses library version: major, minor, and patch. All values are integers. The components can also be accessed by name, so
curses.ncurses_version[0]
is equivalent tocurses.ncurses_version.major
and so on.Availability: if the ncurses library is used.
3.8 版新加入.
Some constants are available to specify character cell attributes. The exact constants available are system dependent.
属性 |
含义 |
---|---|
|
备用字符集模式 |
|
闪烁模式 |
|
粗体模式 |
|
暗淡模式 |
|
不可见或空白模式 |
|
斜体模式 |
|
正常属性 |
|
保护模式 |
|
反转背景色和前景色 |
|
突出模式 |
|
下划线模式 |
|
水平突出显示 |
|
左高亮 |
|
底部高亮 |
|
右高亮 |
|
顶部高亮 |
|
垂直突出显示 |
|
用于提取字符的位掩码 |
3.7 版新加入: A_ITALIC
was added.
有几个常量可用于提取某些方法返回的相应属性。
位掩码 |
含义 |
---|---|
|
用于提取属性的位掩码 |
|
用于提取字符的位掩码 |
|
用于提取颜色对字段信息的位掩码 |
键由名称以 KEY_
开头的整数常量引用。确切的可用键取决于系统。
关键常数 |
键 |
---|---|
|
最小键值 |
|
中断键(不可靠) |
|
向下箭头 |
|
向上箭头 |
|
向左箭头 |
|
向右箭头 |
|
Home key (upward+left arrow) |
|
退格(不可靠) |
|
Function keys. Up to 64 function keys are supported. |
|
Value of function key n |
|
删除行 |
|
插入行 |
|
Delete character |
|
Insert char or enter insert mode |
|
Exit insert char mode |
|
Clear screen |
|
Clear to end of screen |
|
Clear to end of line |
|
Scroll 1 line forward |
|
Scroll 1 line backward (reverse) |
|
下一页 |
|
上一页 |
|
Set tab |
|
Clear tab |
|
Clear all tabs |
|
Enter or send (unreliable) |
|
Soft (partial) reset (unreliable) |
|
Reset or hard reset (unreliable) |
|
打印 |
|
Home down or bottom (lower left) |
|
键盘的左上角 |
|
键盘的右上角 |
|
键盘的中心 |
|
键盘左下方 |
|
键盘右下方 |
|
Back tab |
|
Beg (beginning) |
|
取消 |
|
关闭 |
|
Cmd (命令行) |
|
复制 |
|
创建 |
|
End |
|
退出 |
|
查找 |
|
帮助 |
|
标记 |
|
消息 |
|
移动 |
|
下一个 |
|
打开 |
|
选项 |
|
Prev (previous) |
|
重做 |
|
Ref (reference) |
|
刷新 |
|
替换 |
|
重启 |
|
恢复 |
|
保存 |
|
Shifted Beg (beginning) |
|
Shifted Cancel |
|
Shifted Command |
|
Shifted Copy |
|
Shifted Create |
|
Shifted Delete char |
|
Shifted Delete line |
|
Select |
|
Shifted End |
|
Shifted Clear line |
|
Shifted Exit |
|
Shifted Find |
|
Shifted Help |
|
Shifted Home |
|
Shifted Input |
|
Shifted Left arrow |
|
Shifted Message |
|
Shifted Move |
|
Shifted Next |
|
Shifted Options |
|
Shifted Prev |
|
Shifted Print |
|
Shifted Redo |
|
Shifted Replace |
|
Shifted Right arrow |
|
Shifted Resume |
|
Shifted Save |
|
Shifted Suspend |
|
Shifted Undo |
|
Suspend |
|
撤销操作 |
|
Mouse event has occurred |
|
Terminal resize event |
|
Maximum key value |
在VT100及其软件仿真(例如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 |
The following table lists characters from the alternate character set. These are inherited from the VT100 terminal, and will generally be available on software emulations such as X terminals. When there is no graphic available, curses falls back on a crude printable ASCII approximation.
備註
只有在调用 initscr()
之后才能使用它们
ACS代码 |
含义 |
---|---|
|
右上角的别名 |
|
实心方块 |
|
正方形 |
|
水平线的别名 |
|
左上角的别名 |
|
顶部 T 型的别名 |
|
底部 T 型 |
|
正方形 |
|
棋盘(点刻) |
|
向下箭头 |
|
等级符 |
|
菱形 |
|
大于或等于 |
|
水平线 |
|
灯形符号 |
|
向左箭头 |
|
小于或等于 |
|
左下角 |
|
右下角 |
|
左侧 T 型 |
|
不等号 |
|
字母π |
|
正负号 |
|
加号 |
|
向右箭头 |
|
右侧 T 型 |
|
扫描线 1 |
|
扫描线3 |
|
扫描线7 |
|
扫描线 9 |
|
右下角的别名 |
|
垂直线的别名 |
|
右侧 T 型的别名 |
|
左下角的别名 |
|
底部 T 型的别名 |
|
左侧 T 型的别名 |
|
alternate name for crossover or big plus |
|
英镑 |
|
顶部 T 型 |
|
向上箭头 |
|
左上角 |
|
右上角 |
|
垂线 |
下表列出了预定义的颜色:
常数 |
颜色 |
---|---|
|
黑色 |
|
蓝色 |
|
青色(浅绿蓝色) |
|
绿色 |
|
洋红色(紫红色) |
|
红色 |
|
白色 |
|
黄色 |
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 hand corner of the rectangle to be drawn; the fourth and fifth arguments are the y and x coordinates of the lower right hand 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.
文本框对象¶
You can instantiate a Textbox
object as follows:
-
class
curses.textpad.
Textbox
(win)¶ Return a textbox widget object. The win argument should be a curses window object in which the textbox is to be contained. The edit cursor of the textbox is initially located at the upper left hand corner of the containing window, with coordinates
(0, 0)
. The instance'sstripspaces
flag is initially on.Textbox
objects have the following methods:-
edit
([validator])¶ This is the entry point you will normally use. It accepts editing keystrokes until one of the termination keystrokes is entered. If validator 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. This method returns the window contents as a string; whether blanks in the window are included is affected by the
stripspaces
attribute.
-
do_command
(ch)¶ 处理单个按键命令。以下是支持的特殊按键:
按键
动作
Control-A
转到窗口的左边缘。
Control-B
光标向左,如果可能,包含前一行。
Control-D
删除光标下的字符。
Control-E
Go to right edge (stripspaces off) or end of line (stripspaces on).
Control-F
向右移动光标,适当时换行到下一行。
Control-G
终止,返回窗口内容。
Control-H
向后删除字符。
Control-J
如果窗口是1行则终止,否则插入换行符。
Control-K
如果行为空,则删除它,否则清除到行尾。
Control-L
刷新屏幕。
Control-N
光标向下;向下移动一行。
Control-O
在光标位置插入一个空行。
Control-P
光标向上;向上移动一行。
如果光标位于无法移动的边缘,则移动操作不执行任何操作。在可能的情况下,支持以下同义词:
常数
按键
KEY_LEFT
Control-B
KEY_RIGHT
Control-F
KEY_UP
Control-P
KEY_DOWN
Control-N
KEY_BACKSPACE
Control-h
All other keystrokes are treated as a command to insert the given character and move right (with line wrapping).
-
gather
()¶ Return the window contents as a string; whether blanks in the window are included is affected by the
stripspaces
member.
-
stripspaces
¶ This attribute is a flag which controls the interpretation of blanks in the window. When it is on, trailing blanks on each line are ignored; any cursor motion that would land the cursor on a trailing blank goes to the end of that line instead, and trailing blanks are stripped when the window contents are gathered.
-