On this page
curses
Prev: ctypes | Up: stdlib_module | Next: dataclasses
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — curses
Tags: #public #python3 #stdlib
Import
import curses
Overview
Terminal-independent screen-painting and keyboard-handling library wrapping POSIX ncurses (wrapper, initscr, newwin, cbreak, noecho). Reach for curses.wrapper() for quick POSIX terminal UIs when you want terminfo abstraction; for modern TrueColor/async/zero-curses TUIs, consider raw ANSI + termios/tty.
Quick Start
import curses
def draw(stdscr: curses.window) -> None:
curses.curs_set(0)
stdscr.clear()
stdscr.addstr(0, 0, "Press q to quit")
stdscr.refresh()
while stdscr.getch() != ord("q"):
pass
# Invoke only on an interactive POSIX TTY:
# curses.wrapper(draw)
assert callable(curses.wrapper)
Implementation Walkthrough
1. Safe Initialization & Terminal Restoration (curses.wrapper)
Always launch via curses.wrapper(main_fn)—it enables noecho, cbreak, keypad keys, and color pairs, and guarantees your terminal is restored cleanly even if an unhandled exception occurs:
import curses
def app(stdscr: curses.window) -> str:
curses.start_color()
curses.use_default_colors()
curses.init_pair(1, curses.COLOR_CYAN, -1)
height, width = stdscr.getmaxyx()
msg = f"Terminal: {width}x{height}"[: max(0, width - 1)]
stdscr.addstr(0, 0, msg, curses.color_pair(1))
stdscr.refresh()
return msg
Gotchas & Boundaries
- POSIX-only in the standard library: Windows requires the third-party
windows-cursespackage. - Writing to the bottom-right cell raises
curses.error: Truncate strings towidth - 1or catchcurses.errorwhen drawing the final cell. - Never call
initscr()withouttry ... finally: endwin(): Usecurses.wrapper()so crashes don't leave the user's terminal stuck in raw/no-echo mode. - Handle
curses.KEY_RESIZEin your event loop when the user resizes the terminal window.
Common Functions
- curses.assume_default_colors() —
assume_default_colors(fg, bg, /)— Allow use of default values for colors on terminals supporting this feature. - curses.baudrate() —
baudrate()— Return the output speed of the terminal in bits per second. - curses.beep() —
beep()— Emit a short attention sound. - curses.can_change_color() —
can_change_color()— Return True if the programmer can change the colors displayed by the terminal. - curses.cbreak() —
cbreak(flag=True, /)— Enter cbreak mode. - curses.color_content() —
color_content(color_number, /)— Return the red, green, and blue (RGB) components of the specified color. - curses.color_pair() —
color_pair(pair_number, /)— Return the attribute value for displaying text in the specified color. - curses.curs_set() —
curs_set(visibility, /)— Set the cursor state. - curses.def_prog_mode() —
def_prog_mode()— Save the current terminal mode as the "program" mode. - curses.def_shell_mode() —
def_shell_mode()— Save the current terminal mode as the "shell" mode. - curses.delay_output() —
delay_output(ms, /)— Insert a pause in output. - curses.doupdate() —
doupdate()— Update the physical screen to match the virtual screen. - curses.echo() —
echo(flag=True, /)— Enter echo mode. - curses.endwin() —
endwin()— De-initialize the library, and return terminal to normal status. - curses.erasechar() —
erasechar()— Return the user's current erase character. - curses.filter() —
filter()— Restrict screen updates to the current line. - curses.flash() —
flash()— Flash the screen. - curses.flushinp() —
flushinp()— Flush all input buffers. - curses.get_escdelay() —
get_escdelay()— Gets the curses ESCDELAY setting. - curses.get_tabsize() —
get_tabsize()— Gets the curses TABSIZE setting. - curses.getmouse() —
getmouse()— Retrieve the queued mouse event. - curses.getsyx() —
getsyx()— Return the current coordinates of the virtual screen cursor. - curses.getwin() —
getwin(file, /)— Read window related data stored in the file by an earlier putwin() call. - curses.halfdelay() —
halfdelay(tenths, /)— Enter half-delay mode. - curses.has_colors() —
has_colors()— Return True if the terminal can display colors; otherwise, return False. - curses.has_extended_color_support() —
has_extended_color_support()— Return True if the module supports extended colors; otherwise, return False. - curses.has_ic() —
has_ic()— Return True if the terminal has insert- and delete-character capabilities. - curses.has_il() —
has_il()— Return True if the terminal has insert- and delete-line capabilities. - curses.has_key() —
has_key(key, /)— Return True if the current terminal type recognizes a key with that value. - curses.init_color() —
init_color(color_number, r, g, b, /)— Change the definition of a color. - curses.init_pair() —
init_pair(pair_number, fg, bg, /)— Change the definition of a color-pair. - curses.initscr() —
initscr()—curses.initscr()function. - curses.intrflush() —
intrflush(flag, /)— Control flushing of the output buffer when an interrupt key is pressed. - curses.is_term_resized() —
is_term_resized(nlines, ncols, /)— Return True if resize_term() would modify the window structure, False otherwise. - curses.isendwin() —
isendwin()— Return True if endwin() has been called.
Classes & Objects
- curses.error —
curses.errorclass. (Methods:.add_note(),.with_traceback()) - curses.window —
curses.windowclass. (Methods:.addch(),.addnstr(),.addstr(),.attroff(),.attron(),.attrset())
Constants & Attributes
curses.ALL_MOUSE_EVENTS(int) =134217727curses.A_ALTCHARSET(int) =4194304curses.A_ATTRIBUTES(int) =4294967040curses.A_BLINK(int) =524288curses.A_BOLD(int) =2097152curses.A_CHARTEXT(int) =255curses.A_COLOR(int) =65280curses.A_DIM(int) =1048576curses.A_HORIZONTAL(int) =33554432curses.A_INVIS(int) =8388608curses.A_ITALIC(int) =2147483648curses.A_LEFT(int) =67108864curses.A_LOW(int) =134217728curses.A_NORMAL(int) =0curses.A_PROTECT(int) =16777216
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/cmd
- tuts/python3/stdlib_module/ctypes
- tuts/python3/stdlib_module/curses/assume_default_colors
- tuts/python3/stdlib_module/curses/baudrate
- tuts/python3/stdlib_module/curses/beep
- tuts/python3/stdlib_module/curses/can_change_color
- tuts/python3/stdlib_module/curses/cbreak
- tuts/python3/stdlib_module/curses/color_content
- tuts/python3/stdlib_module/curses/color_pair
- tuts/python3/stdlib_module/curses/curs_set
- tuts/python3/stdlib_module/curses/def_prog_mode
- tuts/python3/stdlib_module/curses/def_shell_mode
- tuts/python3/stdlib_module/curses/delay_output
- tuts/python3/stdlib_module/curses/doupdate
- tuts/python3/stdlib_module/curses/echo
- tuts/python3/stdlib_module/curses/endwin
- tuts/python3/stdlib_module/curses/erasechar
- tuts/python3/stdlib_module/curses/error
- tuts/python3/stdlib_module/curses/filter
- tuts/python3/stdlib_module/curses/flash
- tuts/python3/stdlib_module/curses/flushinp
- tuts/python3/stdlib_module/curses/get_escdelay
- tuts/python3/stdlib_module/curses/get_tabsize
- tuts/python3/stdlib_module/curses/getmouse
- tuts/python3/stdlib_module/curses/getsyx
- tuts/python3/stdlib_module/curses/getwin
- tuts/python3/stdlib_module/curses/halfdelay
- tuts/python3/stdlib_module/curses/has_colors
- tuts/python3/stdlib_module/curses/has_extended_color_support
- tuts/python3/stdlib_module/curses/has_ic
- tuts/python3/stdlib_module/curses/has_il
- tuts/python3/stdlib_module/curses/has_key
- tuts/python3/stdlib_module/curses/init_color
- tuts/python3/stdlib_module/curses/init_pair
- tuts/python3/stdlib_module/curses/initscr
- tuts/python3/stdlib_module/curses/intrflush
- tuts/python3/stdlib_module/curses/is_term_resized
- tuts/python3/stdlib_module/curses/isendwin
- tuts/python3/stdlib_module/curses/window
- tuts/python3/stdlib_module/dataclasses
- tuts/python3/stdlib_module/msvcrt
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/ctypes Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/curses/assume_default_colors Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/curses/baudrate Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/curses/beep Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/curses/can_change_color Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/curses/cbreak Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/curses/color_content Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/curses/color_pair Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/curses/curs_set Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/curses/def_prog_mode Linked here; links to this page; shared tags: #stdlib