• Home
  • Raw
  • Download

Lines Matching refs:curses

13    This document describes how to use the :mod:`curses` extension
17 What is curses?
20 The curses library supplies a terminal-independent screen-painting and
36 The curses library provides fairly basic functionality, providing the
40 appearance---and the curses library will figure out what control codes
41 need to be sent to the terminal to produce the right output. curses
46 The curses library was originally written for BSD Unix; the later System V
47 versions of Unix from AT&T added many enhancements and new functions. BSD curses
53 versions of curses carried by some proprietary Unixes may not support
56 The Windows version of Python doesn't include the :mod:`curses`
61 use the same API as curses but provides cursor-addressable text output
65 The Python curses module
69 curses; if you're already familiar with curses programming in C, it's really
73 :meth:`~curses.window.addstr` method. You'll see this covered in more
76 This HOWTO is an introduction to writing text-mode programs with curses
77 and Python. It doesn't attempt to be a complete guide to the curses API; for
82 Starting and ending a curses application
85 Before doing anything, curses must be initialized. This is done by
86 calling the :func:`~curses.initscr` function, which will determine the
93 import curses
94 stdscr = curses.initscr()
96 Usually curses applications turn off automatic echoing of keys to the
99 :func:`~curses.noecho` function. ::
101 curses.noecho()
107 curses.cbreak()
112 curses can do it for you, returning a special value such as
113 :const:`curses.KEY_LEFT`. To get curses to do the job, you'll have to enable
118 Terminating a curses application is much easier than starting one. You'll need
121 curses.nocbreak()
123 curses.echo()
125 to reverse the curses-friendly terminal settings. Then call the
126 :func:`~curses.endwin` function to restore the terminal to its original
129 curses.endwin()
131 A common problem when debugging a curses application is to get your terminal
138 importing the :func:`curses.wrapper` function and using it like this::
140 from curses import wrapper
156 The :func:`~curses.wrapper` function takes a callable object and does the
170 Windows are the basic abstraction in curses. A window object represents a
174 The ``stdscr`` object returned by the :func:`~curses.initscr` function is a
178 :func:`~curses.newwin` function creates a new window of a given size,
183 win = curses.newwin(height, width, begin_y, begin_x)
185 Note that the coordinate system used in curses is unusual.
190 applications, but it's been part of curses since it was first written,
194 :data:`curses.LINES` and :data:`curses.COLS` variables to obtain the *y* and
196 ``(curses.LINES - 1, curses.COLS - 1)``.
200 :meth:`~curses.window.refresh` method of window objects to update the
203 This is because curses was originally written with slow 300-baud
205 time required to redraw the screen was very important. Instead curses
212 In practice, explicitly telling curses to redraw a window doesn't
213 really complicate programming with curses much. Most programs go into a flurry
226 pad = curses.newpad(100, 100)
252 1) Calls the :meth:`~curses.window.noutrefresh` method of each window
255 2) Calls the function :func:`~curses.doupdate` function to change the
266 From a C programmer's point of view, curses may sometimes look like a
277 :meth:`~curses.window.addstr` accept multiple argument forms. Usually there
302 The :meth:`~curses.window.addstr` method takes a Python string or
309 The :meth:`~curses.window.addch` methods take a character, which can be
328 with older curses versions, there's a ``leaveok(bool)`` function
329 that's a synonym for :func:`~curses.curs_set`. When *bool* is true, the
330 curses library will attempt to suppress the flashing cursor, and you
339 highlight certain words. curses supports this by allowing you to specify an
344 set, but curses doesn't guarantee that all the possible combinations
369 curses.A_REVERSE)
372 The curses library also supports color on those terminals that provide it. The
376 To use color, you must call the :func:`~curses.start_color` function soon
377 after calling :func:`~curses.initscr`, to initialize the default color set
378 (the :func:`curses.wrapper` function does this automatically). Once that's
379 done, the :func:`~curses.has_colors` function returns TRUE if the terminal
381 actually display color. (Note: curses uses the American spelling 'color',
386 The curses library maintains a finite number of color pairs, containing a
388 value corresponding to a color pair with the :func:`~curses.color_pair`
395 stdscr.addstr("Pretty text", curses.color_pair(1))
405 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The :mod:`curses`
407 :const:`curses.COLOR_BLACK`, :const:`curses.COLOR_RED`, and so forth.
412 curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
418 stdscr.addstr(0,0, "RED ALERT!", curses.color_pair(1))
425 :func:`~curses.can_change_color`, which returns ``True`` if the capability is
433 The C curses library offers only very simple input mechanisms. Python's
434 :mod:`curses` module adds a basic text-input widget. (Other libraries
440 * :meth:`~curses.window.getch` refreshes the screen and then waits for
441 the user to hit a key, displaying the key if :func:`~curses.echo` has been
445 * :meth:`~curses.window.getkey` does the same thing but converts the
451 :meth:`~curses.window.nodelay` window method. After ``nodelay(True)``,
454 ``curses.ERR`` (a value of -1) and :meth:`getkey` raises an exception.
455 There's also a :func:`~curses.halfdelay` function, which can be used to (in
458 curses raises an exception.
463 value returned to constants such as :const:`curses.KEY_PPAGE`,
464 :const:`curses.KEY_HOME`, or :const:`curses.KEY_LEFT`. The main loop of
473 elif c == curses.KEY_HOME:
476 The :mod:`curses.ascii` module supplies ASCII class membership functions that
480 and return the same type. For example, :func:`curses.ascii.ctrl` returns the
484 :meth:`~curses.window.getstr`. It isn't used very often, because its
489 curses.echo() # Enable echoing of characters
494 The :mod:`curses.textpad` module supplies a text box that supports an
496 :class:`~curses.textpad.Textbox` class support editing with input
500 import curses
501 from curses.textpad import Textbox, rectangle
506 editwin = curses.newwin(5,30, 2,1)
518 See the library documentation on :mod:`curses.textpad` for more details.
526 instance, but the Python library page for the :mod:`curses` module is now
529 If you're in doubt about the detailed behavior of the curses
530 functions, consult the manual pages for your curses implementation,
536 Because the curses API is so large, some functions aren't supported in
548 * `"Use curses... don't swear" <https://www.youtube.com/watch?v=eN1eZtjLEnU>`_:
549 video of a PyCon 2013 talk on controlling terminals using curses or Urwid.