15.11. curses — Terminal handling for character-cell displays¶
Changed in version 1.6: Added support for the ncurses library and converted to a package.
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 DOS, OS/2, 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.
Note
Since version 5.4, the ncurses library decides how to interpret non-ASCII data
using the nl_langinfo function. That means that you have to call
locale.setlocale() in the application and encode Unicode strings
using one of the system’s available encodings. This example uses the
system’s default encoding:
import locale
locale.setlocale(locale.LC_ALL, '')
code = locale.getpreferredencoding()
Then use code as the encoding for str.encode() calls.
See also
- Module
curses.ascii Utilities for working with ASCII characters, regardless of your locale settings.
- Module
curses.panel A panel stack extension that adds depth to curses windows.
- Module
curses.textpad Editable text widget for curses supporting Emacs-like bindings.
- Curses Programming with Python
Tutorial material on using curses with Python, by Andrew Kuchling and Eric Raymond.
The Demo/curses/ directory in the Python source distribution contains some example programs using the curses bindings provided by this module.
15.11.1. Functions¶
The module curses defines the following exception:
-
exception
curses.error¶ Exception raised when a curses library function returns an error.
Note
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.
The module curses defines the following functions:
-
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()¶ Return
TrueorFalse, 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()thencbreak()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
0andCOLORS. A 3-tuple is returned, containing the R,G,B values for the given color, which will be between0(no component) and1000(maximum amount of component).
-
curses.color_pair(color_number)¶ Return the attribute value for displaying text in the specified color. This attribute value can be combined with
A_STANDOUT,A_REVERSE, and the otherA_*attributes.pair_number()is the counterpart to this function.
-
curses.curs_set(visibility)¶ Set the cursor state. visibility can be set to 0, 1, or 2, for invisible, normal, or very visible. If the terminal supports the visibility requested, the previous cursor state is returned; otherwise, an exception is raised. 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 asaddstr()have been performed on a window. The normalrefresh()call is simplynoutrefresh()followed bydoupdate(); if you have to update multiple windows, you can speed performance and perhaps reduce screen flicker by issuingnoutrefresh()calls on all windows, followed by a singledoupdate().
-
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. 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 beforeinitscr()is called. The effect is that, during those calls,LINESis set to 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()¶ After
getch()returnsKEY_MOUSEto signal a mouse event, this method should be call 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 4:BUTTONn_PRESSED,BUTTONn_RELEASED,BUTTONn_CLICKED,BUTTONn_DOUBLE_CLICKED,BUTTONn_TRIPLE_CLICKED,BUTTON_SHIFT,BUTTON_CTRL,BUTTON_ALT.
-
curses.getsyx()¶ Return the current coordinates of the virtual screen cursor in y and x. If leaveok is currently true, then -1,-1 is returned.
-
curses.getwin(file)¶ Read window related data stored in the file by an earlier
putwin()call. The routine then creates and initializes a new window using that data, returning the new window object.
-
curses.has_colors()¶ Return
Trueif the terminal can display colors; otherwise, returnFalse.
-
curses.has_ic()¶ Return
Trueif the terminal has insert- and delete-character capabilities. This function is included for historical reasons only, as all modern software terminal emulators have such capabilities.
-
curses.has_il()¶ Return
Trueif the terminal has insert- and delete-line capabilities, or can simulate them using scrolling regions. This function is included for historical reasons only, as all modern software terminal emulators have such capabilities.
-
curses.
