curses — 用于字符单元显示的终端处理

源代码: Lib/curses


The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling.

While curses is most widely used in the Unix environment, versions are available for Windows, DOS, and possibly other systems as well. This extension module is designed to match the API of ncurses, an open-source curses library hosted on Linux and the BSD variants of Unix.

可用性 : not Android, not iOS, not WASI.

This module is not supported on mobile platforms or WebAssembly 平台 .

注意

Whenever the documentation mentions a character it can be specified as an integer, a one-character Unicode string or a one-byte byte string.

Whenever the documentation mentions a character string it can be specified as a Unicode string or a byte string.

另请参阅

模块 curses.ascii

Utilities for working with ASCII characters, regardless of your locale settings.

模块 curses.panel

A panel stack extension that adds depth to curses windows.

模块 curses.textpad

Editable text widget for curses supporting Emacs -like bindings.

采用 Python 进行 Curses 编程

Tutorial material on using curses with Python, by Andrew Kuchling and Eric Raymond.

函数

模块 curses defines the following exception:

exception curses. error

Exception raised when a curses library function returns an error.

注意

Whenever x or y arguments to a function or a method are optional, they default to the current cursor location. Whenever attr is optional, it defaults to A_NORMAL .

模块 curses 定义了下列函数:

curses. baudrate ( )

Return the output speed of the terminal in bits per second. On software terminal emulators it will have a fixed high value. Included for historical reasons; in former times, it was used to write output loops for time delays and occasionally to change interfaces depending on the line speed.

curses. beep ( )

Emit a short attention sound.

curses. can_change_color ( )

返回 True or False , depending on whether the programmer can change the colors displayed by the terminal.

curses. cbreak ( )

Enter cbreak mode. In cbreak mode (sometimes called “rare” mode) normal tty line buffering is turned off and characters are available to be read one by one. However, unlike raw mode, special characters (interrupt, quit, suspend, and flow control) retain their effects on the tty driver and calling program. Calling first raw() then cbreak() leaves the terminal in cbreak mode.

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 0 and COLORS - 1 . Return a 3-tuple, containing the R,G,B values for the given color, which will be between 0 (no component) and 1000 (maximum amount of component).

curses. color_pair ( pair_number )

Return the attribute value for displaying text in the specified color pair. Only the first 256 color pairs are supported. This attribute value can be combined with A_STANDOUT , A_REVERSE , and the other A_* 属性。 pair_number() is the counterpart to this function.

curses. curs_set ( visibility )

Set the cursor state. visibility can be set to 0 , 1 ,或 2 , for invisible, normal, or very visible. If the terminal supports the visibility requested, return the previous cursor state; otherwise raise an exception. On many terminals, the “visible” mode is an underline cursor and the “very visible” mode is a block cursor.

curses. def_prog_mode ( )

Save the current terminal mode as the “program” mode, the mode when the running program is using curses. (Its counterpart is the “shell” mode, for when the program is not in curses.) Subsequent calls to reset_prog_mode() will restore this mode.

curses. def_shell_mode ( )

Save the current terminal mode as the “shell” mode, the mode when the running program is not using curses. (Its counterpart is the “program” mode, when the program is using curses capabilities.) Subsequent calls to reset_shell_mode() will restore this mode.

curses. delay_output ( ms )

Insert an ms millisecond pause in output.

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() ground updates the physical screen to match the virtual screen.

The virtual screen may be updated by a noutrefresh() call after write operations such as addstr() have been performed on a window. The normal refresh() call is simply noutrefresh() followed by doupdate() ; if you have to update multiple windows, you can speed performance and perhaps reduce screen flicker by issuing noutrefresh() calls on all windows, followed by a single doupdate() .

curses. echo ( )

Enter echo mode. In echo mode, each character input is echoed to the screen as it is entered.

curses. endwin ( )

De-initialize the library, and return terminal to normal status.

curses. erasechar ( )

Return the user’s current erase character as a one-byte bytes object. 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.

curses. filter ( )

The filter() routine, if used, must be called before initscr() is called. The effect is that, during those calls, LINES 被设为 1 ; the capabilities clear , cup , cud , cud1 , cuu1 , cuu , vpa are disabled; and the home string is set to the value of cr . 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. flash ( )

Flash the screen. That is, change it to reverse-video and then change it back in a short interval. Some people prefer such as ‘visible bell’ to the audible attention signal produced by beep() .

curses. flushinp ( )

Flush all input buffers. This throws away any typeahead that has been typed by the user and has not yet been processed by the program.

curses. getmouse ( )

后于 getch() 返回 KEY_MOUSE to signal a mouse event, this method should be called to retrieve the queued mouse event, represented as a 5-tuple (id, x, y, z, bstate) . id is an ID value used to distinguish multiple devices, and x , y , z are the event’s coordinates. ( z is currently unused.) bstate is an integer value whose bits will be set to indicate the type of event, and will be the bitwise OR of one or more of the following constants, where n is the button number from 1 to 5: BUTTONn_PRESSED , BUTTONn_RELEASED , BUTTONn_CLICKED , BUTTONn_DOUBLE_CLICKED , BUTTONn_TRIPLE_CLICKED , BUTTON_SHIFT , BUTTON_CTRL , BUTTON_ALT .

3.10 版改变: The BUTTON5_* constants are now exposed if they are provided by the underlying curses library.

curses. getsyx ( )

Return the current coordinates of the virtual screen cursor as a tuple (y, x) 。若 leaveok is currently True , then return (-1, -1) .

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.

curses. has_colors ( )

返回 True if the terminal can display colors; otherwise, return False .

curses. has_extended_color_support ( )

返回 True if the module supports extended colors; otherwise, return False . Extended color support allows more than 256 color pairs for terminals that support more than 16 colors (e.g. xterm-256color).

Extended color support requires ncurses version 6.1 or later.

3.10 版添加。