Debian Codegraph @

Doc/library/curses.rst

raw
1 :mod:`!curses` --- Terminal handling for character-cell displays
2 ================================================================
3
4 .. module:: curses
5 :synopsis: An interface to the curses library, providing portable
6 terminal handling.
7
8 .. sectionauthor:: Moshe Zadka <[email protected]>
9 .. sectionauthor:: Eric Raymond <[email protected]>
10
11 **Source code:** :source:`Lib/curses`
12
13 --------------
14
15 The :mod:`curses` module provides an interface to the curses library, the
16 de-facto standard for portable advanced terminal handling.
17
18 While curses is most widely used in the Unix environment, versions are available
19 for Windows, DOS, and possibly other systems as well. This extension module is
20 designed to match the API of ncurses, an open-source curses library hosted on
21 Linux and the BSD variants of Unix.
22
23 .. include:: ../includes/wasm-mobile-notavail.rst
24
25 .. availability:: Unix.
26
27 .. note::
28
29 Whenever the documentation mentions a *character* it can be specified
30 as an integer, a one-character Unicode string or a one-byte byte string.
31
32 Whenever the documentation mentions a *character string* it can be specified
33 as a Unicode string or a byte string.
34
35 .. seealso::
36
37 Module :mod:`curses.ascii`
38 Utilities for working with ASCII characters, regardless of your locale settings.
39
40 Module :mod:`curses.panel`
41 A panel stack extension that adds depth to curses windows.
42
43 Module :mod:`curses.textpad`
44 Editable text widget for curses supporting :program:`Emacs`\ -like bindings.
45
46 :ref:`curses-howto`
47 Tutorial material on using curses with Python, by Andrew Kuchling and Eric
48 Raymond.
49
50
51 .. _curses-functions:
52
53 Functions
54 ---------
55
56 The module :mod:`curses` defines the following exception:
57
58
59 .. exception:: error
60
61 Exception raised when a curses library function returns an error.
62
63 .. note::
64
65 Whenever *x* or *y* arguments to a function or a method are optional, they
66 default to the current cursor location. Whenever *attr* is optional, it defaults
67 to :const:`A_NORMAL`.
68
69 The module :mod:`curses` defines the following functions:
70
71
72 .. function:: baudrate()
73
74 Return the output speed of the terminal in bits per second. On software
75 terminal emulators it will have a fixed high value. Included for historical
76 reasons; in former times, it was used to write output loops for time delays and
77 occasionally to change interfaces depending on the line speed.
78
79
80 .. function:: beep()
81
82 Emit a short attention sound.
83
84
85 .. function:: can_change_color()
86
87 Return ``True`` or ``False``, depending on whether the programmer can change the colors
88 displayed by the terminal.
89
90
91 .. function:: cbreak()
92
93 Enter cbreak mode. In cbreak mode (sometimes called "rare" mode) normal tty
94 line buffering is turned off and characters are available to be read one by one.
95 However, unlike raw mode, special characters (interrupt, quit, suspend, and flow
96 control) retain their effects on the tty driver and calling program. Calling
97 first :func:`raw` then :func:`cbreak` leaves the terminal in cbreak mode.
98
99
100 .. function:: color_content(color_number)
101
102 Return the intensity of the red, green, and blue (RGB) components in the color
103 *color_number*, which must be between ``0`` and ``COLORS - 1``. Return a 3-tuple,
104 containing the R,G,B values for the given color, which will be between
105 ``0`` (no component) and ``1000`` (maximum amount of component). Raise an
106 exception if the color is not supported.
107
108
109 .. function:: color_pair(pair_number)
110
111 Return the attribute value for displaying text in the specified color pair.
112 Only the first 256 color pairs are supported. This
113 attribute value can be combined with :const:`A_STANDOUT`, :const:`A_REVERSE`,
114 and the other :const:`!A_\*` attributes. :func:`pair_number` is the counterpart
115 to this function.
116
117
118 .. function:: curs_set(visibility)
119
120 Set the cursor state. *visibility* can be set to ``0``, ``1``, or ``2``, for invisible,
121 normal, or very visible. If the terminal supports the visibility requested, return the
122 previous cursor state; otherwise raise an exception. On many
123 terminals, the "visible" mode is an underline cursor and the "very visible" mode
124 is a block cursor.
125
126
127 .. function:: def_prog_mode()
128
129 Save the current terminal mode as the "program" mode, the mode when the running
130 program is using curses. (Its counterpart is the "shell" mode, for when the
131 program is not in curses.) Subsequent calls to :func:`reset_prog_mode` will
132 restore this mode.
133
134
135 .. function:: def_shell_mode()
136
137 Save the current terminal mode as the "shell" mode, the mode when the running
138 program is not using curses. (Its counterpart is the "program" mode, when the
139 program is using curses capabilities.) Subsequent calls to
140 :func:`reset_shell_mode` will restore this mode.
141
142
143 .. function:: delay_output(ms)
144
145 Insert an *ms* millisecond pause in output.
146
147
148 .. function:: doupdate()
149
150 Update the physical screen. The curses library keeps two data structures, one
151 representing the current physical screen contents and a virtual screen
152 representing the desired next state. The :func:`doupdate` function updates the
153 physical screen to match the virtual screen.
154
155 The virtual screen may be updated by a :meth:`~window.noutrefresh` call after write
156 operations such as :meth:`~window.addstr` have been performed on a window. The normal
157 :meth:`~window.refresh` call is simply :meth:`!noutrefresh` followed by :func:`!doupdate`;
158 if you have to update multiple windows, you can speed performance and perhaps
159 reduce screen flicker by issuing :meth:`!noutrefresh` calls on all windows,
160 followed by a single :func:`!doupdate`.
161
162
163 .. function:: echo()
164
165 Enter echo mode. In echo mode, each character input is echoed to the screen as
166 it is entered.
167
168
169 .. function:: endwin()
170
171 De-initialize the library, and return terminal to normal status.
172
173
174 .. function:: erasechar()
175
176 Return the user's current erase character as a one-byte bytes object. Under Unix operating systems this
177 is a property of the controlling tty of the curses program, and is not set by
178 the curses library itself.
179
180
181 .. function:: filter()
182
183 The :func:`.filter` routine, if used, must be called before :func:`initscr` is
184 called. The effect is that, during the initialization, :envvar:`LINES` is set to ``1``; the
185 capabilities ``clear``, ``cup``, ``cud``, ``cud1``, ``cuu1``, ``cuu``, ``vpa`` are disabled; and the ``home``
186 string is set to the value of ``cr``. The effect is that the cursor is confined to
187 the current line, and so are screen updates. This may be used for enabling
188 character-at-a-time line editing without touching the rest of the screen.
189
190
191 .. function:: flash()
192
193 Flash the screen. That is, change it to reverse-video and then change it back
194 in a short interval. Some people prefer such as 'visible bell' to the audible
195 attention signal produced by :func:`beep`.
196
197
198 .. function:: flushinp()
199
200 Flush all input buffers. This throws away any typeahead that has been typed
201 by the user and has not yet been processed by the program.
202
203
204 .. function:: getmouse()
205
206 After :meth:`~window.getch` returns :const:`KEY_MOUSE` to signal a mouse event, this
207 method should be called to retrieve the queued mouse event, represented as a
208 5-tuple ``(id, x, y, z, bstate)``. *id* is an ID value used to distinguish
209 multiple devices, and *x*, *y*, *z* are the event's coordinates. (*z* is
210 currently unused.) *bstate* is an integer value whose bits will be set to
211 indicate the type of event, and will be the bitwise OR of one or more of the
212 following constants, where *n* is the button number from 1 to 5:
213 :const:`BUTTONn_PRESSED`, :const:`BUTTONn_RELEASED`, :const:`BUTTONn_CLICKED`,
214 :const:`BUTTONn_DOUBLE_CLICKED`, :const:`BUTTONn_TRIPLE_CLICKED`,
215 :const:`BUTTON_SHIFT`, :const:`BUTTON_CTRL`, :const:`BUTTON_ALT`.
216
217 .. versionchanged:: 3.10
218 The ``BUTTON5_*`` constants are now exposed if they are provided by the
219 underlying curses library.
220
221
222 .. function:: getsyx()
223
224 Return the current coordinates of the virtual screen cursor as a tuple
225 ``(y, x)``. If :meth:`leaveok <window.leaveok>` is currently ``True``, then return ``(-1, -1)``.
226
227
228 .. function:: getwin(file)
229
230 Read window-related data stored in the file by an earlier :meth:`window.putwin` call.
231 The routine then creates and initializes a new window using that data, returning
232 the new window object. The *file* argument must be a file object opened for
233 reading in binary mode.
234
235
236 .. function:: has_colors()
237
238 Return ``True`` if the terminal can display colors; otherwise, return ``False``.
239
240 .. function:: has_extended_color_support()
241
242 Return ``True`` if the module supports extended colors; otherwise, return
243 ``False``. Extended color support allows more than 256 color pairs for
244 terminals that support more than 16 colors (for example, xterm-256color).
245
246 Extended color support requires ncurses version 6.1 or later.
247
248 .. versionadded:: 3.10
249
250 .. function:: has_ic()
251
252 Return ``True`` if the terminal has insert- and delete-character capabilities.
253 This function is included for historical reasons only, as all modern software
254 terminal emulators have such capabilities.
255
256
257 .. function:: has_il()
258
259 Return ``True`` if the terminal has insert- and delete-line capabilities, or can
260 simulate them using scrolling regions. This function is included for
261 historical reasons only, as all modern software terminal emulators have such
262 capabilities.
263
264
265 .. function:: has_key(ch)
266
267 Take a key value *ch*, and return ``True`` if the current terminal type recognizes
268 a key with that value.
269
270
271 .. function:: halfdelay(tenths)
272
273 Used for half-delay mode, which is similar to cbreak mode in that characters
274 typed by the user are immediately available to the program. However, after
275 blocking for *tenths* tenths of seconds, raise an exception if nothing has
276 been typed. The value of *tenths* must be a number between ``1`` and ``255``. Use
277 :func:`nocbreak` to leave half-delay mode.
278
279
280 .. function:: init_color(color_number, r, g, b)
281
282 Change the definition of a color, taking the number of the color to be changed
283 followed by three RGB values (for the amounts of red, green, and blue
284 components). The value of *color_number* must be between ``0`` and
285 ``COLORS - 1``. Each of *r*, *g*, *b*, must be a value between ``0`` and
286 ``1000``. When :func:`init_color` is used, all occurrences of that color on the
287 screen immediately change to the new definition. This function is a no-op on
288 most terminals; it is active only if :func:`can_change_color` returns ``True``.
289
290
291 .. function:: init_pair(pair_number, fg, bg)
292
293 Change the definition of a color-pair. It takes three arguments: the number of
294 the color-pair to be changed, the foreground color number, and the background
295 color number. The value of *pair_number* must be between ``1`` and
296 ``COLOR_PAIRS - 1`` (the ``0`` color pair is wired to white on black and cannot
297 be changed). The value of *fg* and *bg* arguments must be between ``0`` and
298 ``COLORS - 1``, or, after calling :func:`use_default_colors`, ``-1``.
299 If the color-pair was previously initialized, the screen is
300 refreshed and all occurrences of that color-pair are changed to the new
301 definition.
302
303
304 .. function:: initscr()
305
306 Initialize the library. Return a :ref:`window <curses-window-objects>` object
307 which represents the whole screen.
308
309 See :func:`setupterm` for a caveat about calling it before this function.
310
311 .. note::
312
313 If there is an error opening the terminal, the underlying curses library may
314 cause the interpreter to exit.
315
316
317 .. function:: intrflush(flag)
318
319 If *flag* is ``True``, pressing an interrupt key (interrupt, break, or quit)
320 will flush all output in the terminal driver queue. If *flag* is ``False``,
321 no flushing is done.
322
323
324 .. function:: is_term_resized(nlines, ncols)
325
326 Return ``True`` if :func:`resize_term` would modify the window structure,
327 ``False`` otherwise.
328
329
330 .. function:: isendwin()
331
332 Return ``True`` if :func:`endwin` has been called (that is, the curses library has
333 been deinitialized).
334
335
336 .. function:: keyname(k)
337
338 Return the name of the key numbered *k* as a bytes object. The name of a key generating printable
339 ASCII character is the key's character. The name of a control-key combination
340 is a two-byte bytes object consisting of a caret (``b'^'``) followed by the corresponding
341 printable ASCII character. The name of an alt-key combination (128--255) is a
342 bytes object consisting of the prefix ``b'M-'`` followed by the name of the corresponding
343 ASCII character.
344
345 Raise a :exc:`ValueError` if *k* is negative.
346
347
348 .. function:: killchar()
349
350 Return the user's current line kill character as a one-byte bytes object. Under Unix operating systems
351 this is a property of the controlling tty of the curses program, and is not set
352 by the curses library itself.
353
354
355 .. function:: longname()
356
357 Return a bytes object containing the terminfo long name field describing the current
358 terminal. The maximum length of a verbose description is 128 characters. It is
359 defined only after the call to :func:`initscr`.
360
361
362 .. function:: meta(flag)
363
364 If *flag* is ``True``, allow 8-bit characters to be input. If
365 *flag* is ``False``, allow only 7-bit chars.
366
367
368 .. function:: mouseinterval(interval)
369
370 Set the maximum time in milliseconds that can elapse between press and release
371 events in order for them to be recognized as a click, and return the previous
372 interval value. The default value is 166 milliseconds, or one sixth of a second.
373 Use a negative *interval* to obtain the interval value without changing it.
374
375
376 .. function:: mousemask(mousemask)
377
378 Set the mouse events to be reported, and return a tuple ``(availmask,
379 oldmask)``. *availmask* indicates which of the specified mouse events can be
380 reported; on complete failure it returns ``0``. *oldmask* is the previous value of
381 the mouse event mask. If this function is never called, no mouse
382 events are ever reported.
383
384
385 .. function:: napms(ms)
386
387 Sleep for *ms* milliseconds.
388
389
390 .. function:: newpad(nlines, ncols)
391
392 Create and return a pointer to a new pad data structure with the given number
393 of lines and columns. Return a pad as a window object.
394
395 A pad is like a window, except that it is not restricted by the screen size, and
396 is not necessarily associated with a particular part of the screen. Pads can be
397 used when a large window is needed, and only a part of the window will be on the
398 screen at one time. Automatic refreshes of pads (such as from scrolling or
399 echoing of input) do not occur. The :meth:`~window.refresh` and :meth:`~window.noutrefresh`
400 methods of a pad require 6 arguments to specify the part of the pad to be
401 displayed and the location on the screen to be used for the display. The
402 arguments are *pminrow*, *pmincol*, *sminrow*, *smincol*, *smaxrow*, *smaxcol*; the *p*
403 arguments refer to the upper-left corner of the pad region to be displayed and
404 the *s* arguments define a clipping box on the screen within which the pad region
405 is to be displayed.
406
407
408 .. function:: newwin(nlines, ncols)
409 newwin(nlines, ncols, begin_y, begin_x)
410
411 Return a new :ref:`window <curses-window-objects>`, whose left-upper corner
412 is at ``(begin_y, begin_x)``, and whose height/width is *nlines*/*ncols*.
413
414 By default, the window will extend from the specified position to the lower
415 right corner of the screen.
416
417
418 .. function:: nl(flag=True)
419
420 Enter newline mode. This mode translates the return key into newline on input,
421 and translates newline into return and line-feed on output. Newline mode is
422 initially on.
423
424 If *flag* is ``False``, the effect is the same as calling :func:`nonl`.
425
426
427 .. function:: nocbreak()
428
429 Leave cbreak mode. Return to normal "cooked" mode with line buffering.
430
431
432 .. function:: noecho()
433
434 Leave echo mode. Echoing of input characters is turned off.
435
436
437 .. function:: nonl()
438
439 Leave newline mode. Disable translation of return into newline on input, and
440 disable low-level translation of newline into newline/return on output (but this
441 does not change the behavior of ``addch('\n')``, which always does the
442 equivalent of return and line feed on the virtual screen). With translation
443 off, curses can sometimes speed up vertical motion a little; also, it will be
444 able to detect the return key on input.
445
446
447 .. function:: noqiflush()
448
449 When the :func:`!noqiflush` routine is used, normal flush of input and output queues
450 associated with the ``INTR``, ``QUIT`` and ``SUSP`` characters will not be done. You may
451 want to call :func:`!noqiflush` in a signal handler if you want output to
452 continue as though the interrupt had not occurred, after the handler exits.
453
454
455 .. function:: noraw()
456
457 Leave raw mode. Return to normal "cooked" mode with line buffering.
458
459
460 .. function:: pair_content(pair_number)
461
462 Return a tuple ``(fg, bg)`` containing the colors for the requested color pair.
463 The value of *pair_number* must be between ``0`` and ``COLOR_PAIRS - 1``.
464
465
466 .. function:: pair_number(attr)
467
468 Return the number of the color-pair set by the attribute value *attr*.
469 :func:`color_pair` is the counterpart to this function.
470
471
472 .. function:: putp(str)
473
474 Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified
475 terminfo capability for the current terminal. Note that the output of :func:`putp`
476 always goes to standard output.
477
478 :func:`setupterm` (or :func:`initscr`) must be called first.
479
480
481 .. function:: qiflush([flag])
482
483 If *flag* is ``False``, the effect is the same as calling :func:`noqiflush`. If
484 *flag* is ``True``, or no argument is provided, the queues will be flushed when
485 these control characters are read.
486
487
488 .. function:: raw()
489
490 Enter raw mode. In raw mode, normal line buffering and processing of
491 interrupt, quit, suspend, and flow control keys are turned off; characters are
492 presented to curses input functions one by one.
493
494
495 .. function:: reset_prog_mode()
496
497 Restore the terminal to "program" mode, as previously saved by
498 :func:`def_prog_mode`.
499
500
501 .. function:: reset_shell_mode()
502
503 Restore the terminal to "shell" mode, as previously saved by
504 :func:`def_shell_mode`.
505
506
507 .. function:: resetty()
508
509 Restore the state of the terminal modes to what it was at the last call to
510 :func:`savetty`.
511
512
513 .. function:: resize_term(nlines, ncols)
514
515 Backend function used by :func:`resizeterm`, performing most of the work;
516 when resizing the windows, :func:`resize_term` blank-fills the areas that are
517 extended. The calling application should fill in these areas with
518 appropriate data. The :func:`!resize_term` function attempts to resize all
519 windows. However, due to the calling convention of pads, it is not possible
520 to resize these without additional interaction with the application.
521
522
523 .. function:: resizeterm(nlines, ncols)
524
525 Resize the standard and current windows to the specified dimensions, and
526 adjusts other bookkeeping data used by the curses library that record the
527 window dimensions (in particular the SIGWINCH handler).
528
529
530 .. function:: savetty()
531
532 Save the current state of the terminal modes in a buffer, usable by
533 :func:`resetty`.
534
535 .. function:: get_escdelay()
536
537 Retrieves the value set by :func:`set_escdelay`.
538
539 .. versionadded:: 3.9
540
541 .. function:: set_escdelay(ms)
542
543 Sets the number of milliseconds to wait after reading an escape character,
544 to distinguish between an individual escape character entered on the
545 keyboard from escape sequences sent by cursor and function keys.
546
547 .. versionadded:: 3.9
548
549 .. function:: get_tabsize()
550
551 Retrieves the value set by :func:`set_tabsize`.
552
553 .. versionadded:: 3.9
554
555 .. function:: set_tabsize(size)
556
557 Sets the number of columns used by the curses library when converting a tab
558 character to spaces as it adds the tab to a window.
559
560 .. versionadded:: 3.9
561
562 .. function:: setsyx(y, x)
563
564 Set the virtual screen cursor to *y*, *x*. If *y* and *x* are both ``-1``, then
565 :meth:`leaveok <window.leaveok>` is set ``True``.
566
567
568 .. function:: setupterm(term=None, fd=-1)
569
570 Initialize the terminal. *term* is a string giving
571 the terminal name, or ``None``; if omitted or ``None``, the value of the
572 :envvar:`TERM` environment variable will be used. *fd* is the
573 file descriptor to which any initialization sequences will be sent; if not
574 supplied or ``-1``, the file descriptor for ``sys.stdout`` will be used.
575
576 Raise a :exc:`curses.error` if the terminal could not be found or its
577 terminfo database entry could not be read. If the terminal has already
578 been initialized, this function has no effect.
579
580 .. note::
581
582 Calling :func:`initscr` after :func:`setupterm`
583 leaks the terminal that :func:`setupterm` allocated:
584 the curses library keeps only a single current terminal
585 and does not free the previously allocated one.
586
587
588 .. function:: start_color()
589
590 Must be called if the programmer wants to use colors, and before any other color
591 manipulation routine is called. It is good practice to call this routine right
592 after :func:`initscr`.
593
594 :func:`start_color` initializes eight basic colors (black, red, green, yellow,
595 blue, magenta, cyan, and white), and two global variables in the :mod:`curses`
596 module, :const:`COLORS` and :const:`COLOR_PAIRS`, containing the maximum number
597 of colors and color-pairs the terminal can support. It also restores the colors
598 on the terminal to the values they had when the terminal was just turned on.
599
600
601 .. function:: termattrs()
602
603 Return a logical OR of all video attributes supported by the terminal. This
604 information is useful when a curses program needs complete control over the
605 appearance of the screen.
606
607
608 .. function:: termname()
609
610 Return the value of the environment variable :envvar:`TERM`, as a bytes object,
611 truncated to 14 characters.
612
613
614 .. function:: tigetflag(capname)
615
616 Return the value of the Boolean capability corresponding to the terminfo
617 capability name *capname* as an integer. Return the value ``-1`` if *capname* is not a
618 Boolean capability, or ``0`` if it is canceled or absent from the terminal
619 description.
620
621 :func:`setupterm` (or :func:`initscr`) must be called first.
622
623
624 .. function:: tigetnum(capname)
625
626 Return the value of the numeric capability corresponding to the terminfo
627 capability name *capname* as an integer. Return the value ``-2`` if *capname* is not a
628 numeric capability, or ``-1`` if it is canceled or absent from the terminal
629 description.
630
631 :func:`setupterm` (or :func:`initscr`) must be called first.
632
633
634 .. function:: tigetstr(capname)
635
636 Return the value of the string capability corresponding to the terminfo
637 capability name *capname* as a bytes object. Return ``None`` if *capname*
638 is not a terminfo "string capability", or is canceled or absent from the
639 terminal description.
640
641 :func:`setupterm` (or :func:`initscr`) must be called first.
642
643
644 .. function:: tparm(str[, ...])
645
646 Instantiate the bytes object *str* with the supplied parameters, where *str* should
647 be a parameterized string obtained from the terminfo database. For example,
648 ``tparm(tigetstr("cup"), 5, 3)`` could result in ``b'\033[6;4H'``, the exact
649 result depending on terminal type. Up to nine integer parameters may be supplied.
650
651 :func:`setupterm` (or :func:`initscr`) must be called first.
652
653
654 .. function:: typeahead(fd)
655
656 Specify that the file descriptor *fd* be used for typeahead checking. If *fd*
657 is ``-1``, then no typeahead checking is done.
658
659 The curses library does "line-breakout optimization" by looking for typeahead
660 periodically while updating the screen. If input is found, and it is coming
661 from a tty, the current update is postponed until refresh or doupdate is called
662 again, allowing faster response to commands typed in advance. This function
663 allows specifying a different file descriptor for typeahead checking.
664
665
666 .. function:: unctrl(ch)
667
668 Return a bytes object which is a printable representation of the character *ch*.
669 Control characters are represented as a caret followed by the character, for
670 example as ``b'^C'``. Printing characters are left as they are.
671
672
673 .. function:: ungetch(ch)
674
675 Push *ch* so the next :meth:`~window.getch` will return it.
676
677 .. note::
678
679 Only one *ch* can be pushed before :meth:`!getch` is called.
680
681
682 .. function:: update_lines_cols()
683
684 Update the :const:`LINES` and :const:`COLS` module variables.
685 Useful for detecting manual screen resize.
686
687 .. versionadded:: 3.5
688
689
690 .. function:: unget_wch(ch)
691
692 Push *ch* so the next :meth:`~window.get_wch` will return it.
693
694 .. note::
695
696 Only one *ch* can be pushed before :meth:`!get_wch` is called.
697
698 .. versionadded:: 3.3
699
700
701 .. function:: ungetmouse(id, x, y, z, bstate)
702
703 Push a :const:`KEY_MOUSE` event onto the input queue, associating the given
704 state data with it.
705
706
707 .. function:: use_env(flag)
708
709 If used, this function should be called before :func:`initscr` or newterm are
710 called. When *flag* is ``False``, the values of lines and columns specified in the
711 terminfo database will be used, even if environment variables :envvar:`LINES`
712 and :envvar:`COLUMNS` (used by default) are set, or if curses is running in a
713 window (in which case default behavior would be to use the window size if
714 :envvar:`LINES` and :envvar:`COLUMNS` are not set).
715
716
717 .. function:: use_default_colors()
718
719 Allow use of default values for colors on terminals supporting this feature. Use
720 this to support transparency in your application. The default color is assigned
721 to the color number ``-1``. After calling this function, ``init_pair(x,
722 curses.COLOR_RED, -1)`` initializes, for instance, color pair *x* to a red
723 foreground color on the default background.
724
725
726 .. function:: wrapper(func, /, *args, **kwargs)
727
728 Initialize curses and call another callable object, *func*, which should be the
729 rest of your curses-using application. If the application raises an exception,
730 this function will restore the terminal to a sane state before re-raising the
731 exception and generating a traceback. The callable object *func* is then passed
732 the main window 'stdscr' as its first argument, followed by any other arguments
733 passed to :func:`!wrapper`. Before calling *func*, :func:`!wrapper` turns on
734 cbreak mode, turns off echo, enables the terminal keypad, and initializes colors
735 if the terminal has color support. On exit (whether normally or by exception)
736 it restores cooked mode, turns on echo, and disables the terminal keypad.
737
738
739 .. _curses-window-objects:
740
741 Window objects
742 --------------
743
744 .. class:: window
745
746 Window objects, as returned by :func:`initscr` and :func:`newwin` above, have
747 the following methods and attributes:
748
749
750 .. method:: window.addch(ch[, attr])
751 window.addch(y, x, ch[, attr])
752
753 Paint character *ch* at ``(y, x)`` with attributes *attr*, overwriting any
754 character previously painted at that location. By default, the character
755 position and attributes are the current settings for the window object.
756
757 .. note::
758
759 Writing outside the window, subwindow, or pad raises a :exc:`curses.error`.
760 Attempting to write to the lower-right corner of a window, subwindow,
761 or pad will cause an exception to be raised after the character is printed.
762
763
764 .. method:: window.addnstr(str, n[, attr])
765 window.addnstr(y, x, str, n[, attr])
766
767 Paint at most *n* characters of the character string *str* at
768 ``(y, x)`` with attributes
769 *attr*, overwriting anything previously on the display.
770
771
772 .. method:: window.addstr(str[, attr])
773 window.addstr(y, x, str[, attr])
774
775 Paint the character string *str* at ``(y, x)`` with attributes
776 *attr*, overwriting anything previously on the display.
777
778 .. note::
779
780 * Writing outside the window, subwindow, or pad raises :exc:`curses.error`.
781 Attempting to write to the lower-right corner of a window, subwindow,
782 or pad will cause an exception to be raised after the string is printed.
783
784 * A `bug in ncurses <https://bugs.python.org/issue35924>`_, the backend
785 for this Python module, can cause SegFaults when resizing windows. This
786 is fixed in ncurses-6.1-20190511. If you are stuck with an earlier
787 ncurses, you can avoid triggering this if you do not call :func:`addstr`
788 with a *str* that has embedded newlines. Instead, call :func:`addstr`
789 separately for each line.
790
791
792 .. method:: window.attroff(attr)
793
794 Remove attribute *attr* from the "background" set applied to all writes to the
795 current window.
796
797
798 .. method:: window.attron(attr)
799
800 Add attribute *attr* to the "background" set applied to all writes to the
801 current window.
802
803
804 .. method:: window.attrset(attr)
805
806 Set the "background" set of attributes to *attr*. This set is initially
807 ``0`` (no attributes).
808
809
810 .. method:: window.bkgd(ch[, attr])
811
812 Set the background property of the window to the character *ch*, with
813 attributes *attr*. The change is then applied to every character position in
814 that window:
815
816 * The attribute of every character in the window is changed to the new
817 background attribute.
818
819 * Wherever the former background character appears, it is changed to the new
820 background character.
821
822
823 .. method:: window.bkgdset(ch[, attr])
824
825 Set the window's background. A window's background consists of a character and
826 any combination of attributes. The attribute part of the background is combined
827 (OR'ed) with all non-blank characters that are written into the window. Both
828 the character and attribute parts of the background are combined with the blank
829 characters. The background becomes a property of the character and moves with
830 the character through any scrolling and insert/delete line/character operations.
831
832
833 .. method:: window.border([ls[, rs[, ts[, bs[, tl[, tr[, bl[, br]]]]]]]])
834
835 Draw a border around the edges of the window. Each parameter specifies the
836 character to use for a specific part of the border; see the table below for more
837 details.
838
839 .. note::
840
841 A ``0`` value for any parameter will cause the default character to be used for
842 that parameter. Keyword parameters can *not* be used. The defaults are listed
843 in this table:
844
845 +-----------+---------------------+-----------------------+
846 | Parameter | Description | Default value |
847 +===========+=====================+=======================+
848 | *ls* | Left side | :const:`ACS_VLINE` |
849 +-----------+---------------------+-----------------------+
850 | *rs* | Right side | :const:`ACS_VLINE` |
851 +-----------+---------------------+-----------------------+
852 | *ts* | Top | :const:`ACS_HLINE` |
853 +-----------+---------------------+-----------------------+
854 | *bs* | Bottom | :const:`ACS_HLINE` |
855 +-----------+---------------------+-----------------------+
856 | *tl* | Upper-left corner | :const:`ACS_ULCORNER` |
857 +-----------+---------------------+-----------------------+
858 | *tr* | Upper-right corner | :const:`ACS_URCORNER` |
859 +-----------+---------------------+-----------------------+
860 | *bl* | Bottom-left corner | :const:`ACS_LLCORNER` |
861 +-----------+---------------------+-----------------------+
862 | *br* | Bottom-right corner | :const:`ACS_LRCORNER` |
863 +-----------+---------------------+-----------------------+
864
865
866 .. method:: window.box([vertch, horch])
867
868 Similar to :meth:`border`, but both *ls* and *rs* are *vertch* and both *ts* and
869 *bs* are *horch*. The default corner characters are always used by this function.
870
871
872 .. method:: window.chgat(attr)
873 window.chgat(num, attr)
874 window.chgat(y, x, attr)
875 window.chgat(y, x, num, attr)
876
877 Set the attributes of *num* characters at the current cursor position, or at
878 position ``(y, x)`` if supplied. If *num* is not given or is ``-1``,
879 the attribute will be set on all the characters to the end of the line. This
880 function moves cursor to position ``(y, x)`` if supplied. The changed line
881 will be touched using the :meth:`touchline` method so that the contents will
882 be redisplayed by the next window refresh.
883
884
885 .. method:: window.clear()
886
887 Like :meth:`erase`, but also cause the whole window to be repainted upon next
888 call to :meth:`refresh`.
889
890
891 .. method:: window.clearok(flag)
892
893 If *flag* is ``True``, the next call to :meth:`refresh` will clear the window
894 completely.
895
896
897 .. method:: window.clrtobot()
898
899 Erase from cursor to the end of the window: all lines below the cursor are
900 deleted, and then the equivalent of :meth:`clrtoeol` is performed.
901
902
903 .. method:: window.clrtoeol()
904
905 Erase from cursor to the end of the line.
906
907
908 .. method:: window.cursyncup()
909
910 Update the current cursor position of all the ancestors of the window to
911 reflect the current cursor position of the window.
912
913
914 .. method:: window.delch([y, x])
915
916 Delete the character under the cursor, or at ``(y, x)`` if specified. All
917 characters to the right on the same line are shifted one position left.
918
919
920 .. method:: window.deleteln()
921
922 Delete the line under the cursor. All following lines are moved up by one line.
923
924
925 .. method:: window.derwin(begin_y, begin_x)
926 window.derwin(nlines, ncols, begin_y, begin_x)
927
928 An abbreviation for "derive window", :meth:`derwin` is the same as calling
929 :meth:`subwin`, except that *begin_y* and *begin_x* are relative to the origin
930 of the window, rather than relative to the entire screen. Return a window
931 object for the derived window.
932
933
934 .. method:: window.echochar(ch[, attr])
935
936 Add character *ch* with attribute *attr*, and immediately call :meth:`refresh`
937 on the window.
938
939
940 .. method:: window.enclose(y, x)
941
942 Test whether the given pair of screen-relative character-cell coordinates are
943 enclosed by the given window, returning ``True`` or ``False``. It is useful for
944 determining what subset of the screen windows enclose the location of a mouse
945 event.
946
947 .. versionchanged:: 3.10
948 Previously it returned ``1`` or ``0`` instead of ``True`` or ``False``.
949
950
951 .. attribute:: window.encoding
952
953 Encoding used to encode method arguments (Unicode strings and characters).
954 The encoding attribute is inherited from the parent window when a subwindow
955 is created, for example with :meth:`window.subwin`.
956 By default, current locale encoding is used (see :func:`locale.getencoding`).
957
958 .. versionadded:: 3.3
959
960
961 .. method:: window.erase()
962
963 Clear the window.
964
965
966 .. method:: window.getbegyx()
967
968 Return a tuple ``(y, x)`` of coordinates of upper-left corner.
969
970
971 .. method:: window.getbkgd()
972
973 Return the given window's current background character/attribute pair.
974 Its components can be extracted like those of :meth:`inch`.
975
976
977 .. method:: window.getch([y, x])
978
979 Get a character. Note that the integer returned does *not* have to be in ASCII
980 range: function keys, keypad keys and so on are represented by numbers higher
981 than 255. In no-delay mode, return ``-1`` if there is no input, otherwise
982 wait until a key is pressed.
983
984
985 .. method:: window.get_wch([y, x])
986
987 Get a wide character. Return a character for most keys, or an integer for
988 function keys, keypad keys, and other special keys.
989 In no-delay mode, raise an exception if there is no input.
990
991 .. versionadded:: 3.3
992
993
994 .. method:: window.getkey([y, x])
995
996 Get a character, returning a string instead of an integer, as :meth:`getch`
997 does. Function keys, keypad keys and other special keys return a multibyte
998 string containing the key name. In no-delay mode, raise an exception if
999 there is no input.
1000
1001
1002 .. method:: window.getmaxyx()
1003
1004 Return a tuple ``(y, x)`` of the height and width of the window.
1005
1006
1007 .. method:: window.getparyx()
1008
1009 Return the beginning coordinates of this window relative to its parent window
1010 as a tuple ``(y, x)``. Return ``(-1, -1)`` if this window has no
1011 parent.
1012
1013
1014 .. method:: window.getstr()
1015 window.getstr(n)
1016 window.getstr(y, x)
1017 window.getstr(y, x, n)
1018
1019 Read a bytes object from the user, with primitive line editing capacity.
1020 At most *n* characters are read (1023 by default).
1021 The maximum value for *n* is 1023.
1022
1023
1024 .. method:: window.getyx()
1025
1026 Return a tuple ``(y, x)`` of current cursor position relative to the window's
1027 upper-left corner.
1028
1029
1030 .. method:: window.hline(ch, n[, attr])
1031 window.hline(y, x, ch, n[, attr])
1032
1033 Display a horizontal line starting at ``(y, x)`` with length *n* consisting of
1034 the character *ch* with attributes *attr*. The line stops at the right edge
1035 of the window if fewer than *n* cells are available.
1036
1037
1038 .. method:: window.idcok(flag)
1039
1040 If *flag* is ``False``, curses no longer considers using the hardware insert/delete
1041 character feature of the terminal; if *flag* is ``True``, use of character insertion
1042 and deletion is enabled. When curses is first initialized, use of character
1043 insert/delete is enabled by default.
1044
1045
1046 .. method:: window.idlok(flag)
1047
1048 If *flag* is ``True``, :mod:`curses` will try and use hardware line
1049 editing facilities. Otherwise, line insertion/deletion are disabled.
1050
1051
1052 .. method:: window.immedok(flag)
1053
1054 If *flag* is ``True``, any change in the window image automatically causes the
1055 window to be refreshed; you no longer have to call :meth:`refresh` yourself.
1056 However, it may degrade performance considerably, due to repeated calls to
1057 wrefresh. This option is disabled by default.
1058
1059
1060 .. method:: window.inch([y, x])
1061
1062 Return the character at the given position in the window.
1063 The bottom 8 bits are the character proper and the upper bits are the attributes;
1064 extract them with the :data:`A_CHARTEXT` and :data:`A_ATTRIBUTES` bit-masks,
1065 and the color pair with :func:`pair_number`.
1066 The character byte is the locale-encoded byte of the cell's character,
1067 consistent with :meth:`instr`.
1068 On a wide-character build, a character that does not fit in a single byte
1069 in the current locale has a character byte of ``0``;
1070 use :meth:`instr` to read such characters.
1071
1072
1073 .. method:: window.insch(ch[, attr])
1074 window.insch(y, x, ch[, attr])
1075
1076 Insert character *ch* with attributes *attr* before the character under the
1077 cursor, or at ``(y, x)`` if specified. All characters to the right of the
1078 cursor are shifted one position right, with the rightmost character on the
1079 line being lost. The cursor position does not change.
1080
1081
1082 .. method:: window.insdelln(nlines)
1083
1084 Insert *nlines* lines into the specified window above the current line. The
1085 *nlines* bottom lines are lost. For negative *nlines*, delete *nlines* lines
1086 starting with the one under the cursor, and move the remaining lines up. The
1087 bottom *nlines* lines are cleared. The current cursor position remains the
1088 same.
1089
1090
1091 .. method:: window.insertln()
1092
1093 Insert a blank line under the cursor. All following lines are moved down by one
1094 line.
1095
1096
1097 .. method:: window.insnstr(str, n[, attr])
1098 window.insnstr(y, x, str, n[, attr])
1099
1100 Insert a character string (as many characters as will fit on the line) before
1101 the character under the cursor, up to *n* characters. If *n* is zero or
1102 negative, the entire string is inserted. All characters to the right of the
1103 cursor are shifted right, with the rightmost characters on the line being lost.
1104 The cursor position does not change (after moving to *y*, *x*, if specified).
1105
1106
1107 .. method:: window.insstr(str[, attr])
1108 window.insstr(y, x, str[, attr])
1109
1110 Insert a character string (as many characters as will fit on the line) before
1111 the character under the cursor. All characters to the right of the cursor are
1112 shifted right, with the rightmost characters on the line being lost. The cursor
1113 position does not change (after moving to *y*, *x*, if specified).
1114
1115
1116 .. method:: window.instr([n])
1117 window.instr(y, x[, n])
1118
1119 Return a bytes object of characters, extracted from the window starting at the
1120 current cursor position, or at *y*, *x* if specified, and stopping at the end
1121 of the line. Attributes and color information are stripped
1122 from the characters. If *n* is specified, :meth:`instr` returns a string
1123 at most *n* characters long (exclusive of the trailing NUL).
1124
1125
1126 .. method:: window.is_linetouched(line)
1127
1128 Return ``True`` if the specified line was modified since the last call to
1129 :meth:`refresh`; otherwise return ``False``. Raise a :exc:`curses.error`
1130 exception if *line* is not valid for the given window.
1131
1132
1133 .. method:: window.is_wintouched()
1134
1135 Return ``True`` if the specified window was modified since the last call to
1136 :meth:`refresh`; otherwise return ``False``.
1137
1138
1139 .. method:: window.keypad(flag)
1140
1141 If *flag* is ``True``, escape sequences generated by some keys (keypad, function keys)
1142 will be interpreted by :mod:`curses`. If *flag* is ``False``, escape sequences will be
1143 left as is in the input stream.
1144
1145
1146 .. method:: window.leaveok(flag)
1147
1148 If *flag* is ``True``, cursor is left where it is on update, instead of being at "cursor
1149 position." This reduces cursor movement where possible.
1150
1151 If *flag* is ``False``, cursor will always be at "cursor position" after an update.
1152
1153
1154 .. method:: window.move(new_y, new_x)
1155
1156 Move cursor to ``(new_y, new_x)``.
1157
1158
1159 .. method:: window.mvderwin(y, x)
1160
1161 Move the window inside its parent window. The screen-relative parameters of
1162 the window are not changed. This routine is used to display different parts of
1163 the parent window at the same physical position on the screen.
1164
1165
1166 .. method:: window.mvwin(new_y, new_x)
1167
1168 Move the window so its upper-left corner is at ``(new_y, new_x)``.
1169
1170 Moving the window so that any part of it would be off the screen is an error:
1171 the window is not moved and :exc:`curses.error` is raised.
1172
1173
1174 .. method:: window.nodelay(flag)
1175
1176 If *flag* is ``True``, :meth:`getch` will be non-blocking.
1177
1178
1179 .. method:: window.notimeout(flag)
1180
1181 If *flag* is ``True``, escape sequences will not be timed out.
1182
1183 If *flag* is ``False``, after a few milliseconds, an escape sequence will not be
1184 interpreted, and will be left in the input stream as is.
1185
1186
1187 .. method:: window.noutrefresh()
1188 window.noutrefresh(pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol)
1189
1190 Mark for refresh but wait. This function updates the data structure
1191 representing the desired state of the window, but does not force an update of
1192 the physical screen. To accomplish that, call :func:`doupdate`.
1193
1194 The 6 arguments can only be specified, and are then required, when the window
1195 is a pad created with :func:`newpad`; they have the same meaning as for
1196 :meth:`refresh`.
1197
1198
1199 .. method:: window.overlay(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
1200
1201 Overlay the window on top of *destwin*. The windows need not be the same size,
1202 only the overlapping region is copied. This copy is non-destructive, which means
1203 that the current background character does not overwrite the old contents of
1204 *destwin*.
1205
1206 To get fine-grained control over the copied region, the second form of
1207 :meth:`overlay` can be used. *sminrow* and *smincol* are the upper-left
1208 coordinates of the source window, and the other variables mark a rectangle in
1209 the destination window.
1210
1211
1212 .. method:: window.overwrite(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
1213
1214 Overwrite the window on top of *destwin*. The windows need not be the same size,
1215 in which case only the overlapping region is copied. This copy is destructive,
1216 which means that the current background character overwrites the old contents of
1217 *destwin*.
1218
1219 To get fine-grained control over the copied region, the second form of
1220 :meth:`overwrite` can be used. *sminrow* and *smincol* are the upper-left
1221 coordinates of the source window, the other variables mark a rectangle in the
1222 destination window.
1223
1224
1225 .. method:: window.putwin(file)
1226
1227 Write all data associated with the window into the provided file object. This
1228 information can be later retrieved using the :func:`getwin` function.
1229
1230
1231 .. method:: window.redrawln(beg, num)
1232
1233 Indicate that the *num* screen lines, starting at line *beg*, are corrupted and
1234 should be completely redrawn on the next :meth:`refresh` call.
1235
1236
1237 .. method:: window.redrawwin()
1238
1239 Touch the entire window, causing it to be completely redrawn on the next
1240 :meth:`refresh` call.
1241
1242
1243 .. method:: window.refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])
1244
1245 Update the display immediately (sync actual screen with previous
1246 drawing/deleting methods).
1247
1248 The 6 arguments can only be specified, and are then required, when the window
1249 is a pad created with :func:`newpad`. The additional parameters are needed to indicate what part
1250 of the pad and screen are involved. *pminrow* and *pmincol* specify the
1251 upper-left corner of the rectangle to be displayed in the pad. *sminrow*,
1252 *smincol*, *smaxrow*, and *smaxcol* specify the edges of the rectangle to be
1253 displayed on the screen. The lower-right corner of the rectangle to be
1254 displayed in the pad is calculated from the screen coordinates, since the
1255 rectangles must be the same size. Both rectangles must be entirely contained
1256 within their respective structures. Negative values of *pminrow*, *pmincol*,
1257 *sminrow*, or *smincol* are treated as if they were zero.
1258
1259
1260 .. method:: window.resize(nlines, ncols)
1261
1262 Reallocate storage for a curses window to adjust its dimensions to the
1263 specified values. If either dimension is larger than the current values, the
1264 window's data is filled with blanks that have the current background
1265 rendition (as set by :meth:`bkgdset`) merged into them.
1266
1267
1268 .. method:: window.scroll([lines=1])
1269
1270 Scroll the screen or scrolling region. Scroll upward by *lines* lines if
1271 *lines* is positive, or downward if it is negative. Scrolling has no effect
1272 unless it has been enabled for the window with :meth:`scrollok`.
1273
1274
1275 .. method:: window.scrollok(flag)
1276
1277 Control what happens when the cursor of a window is moved off the edge of the
1278 window or scrolling region, either as a result of a newline action on the bottom
1279 line, or typing the last character of the last line. If *flag* is ``False``, the
1280 cursor is left on the bottom line. If *flag* is ``True``, the window is scrolled up
1281 one line. Note that in order to get the physical scrolling effect on the
1282 terminal, it is also necessary to call :meth:`idlok`.
1283
1284
1285 .. method:: window.setscrreg(top, bottom)
1286
1287 Set the scrolling region from line *top* to line *bottom*. All scrolling actions
1288 will take place in this region.
1289
1290
1291 .. method:: window.standend()
1292
1293 Turn off the standout attribute. On some terminals this has the side effect of
1294 turning off all attributes.
1295
1296
1297 .. method:: window.standout()
1298
1299 Turn on attribute *A_STANDOUT*.
1300
1301
1302 .. method:: window.subpad(begin_y, begin_x)
1303 window.subpad(nlines, ncols, begin_y, begin_x)
1304
1305 Return a sub-pad, whose upper-left corner is at ``(begin_y, begin_x)``, and
1306 whose width/height is *ncols*/*nlines*. The coordinates are relative to the
1307 parent pad (unlike :meth:`subwin`, which uses screen coordinates). This
1308 method is only available for pads created with :func:`newpad`.
1309
1310
1311 .. method:: window.subwin(begin_y, begin_x)
1312 window.subwin(nlines, ncols, begin_y, begin_x)
1313
1314 Return a sub-window, whose upper-left corner is at the screen-relative
1315 coordinates ``(begin_y, begin_x)``, and whose width/height is *ncols*/*nlines*.
1316
1317 By default, the sub-window will extend from the specified position to the lower
1318 right corner of the window.
1319
1320
1321 .. method:: window.syncdown()
1322
1323 Touch each location in the window that has been touched in any of its ancestor
1324 windows. This routine is called by :meth:`refresh`, so it should almost never
1325 be necessary to call it manually.
1326
1327
1328 .. method:: window.syncok(flag)
1329
1330 If *flag* is ``True``, then :meth:`syncup` is called automatically
1331 whenever there is a change in the window.
1332
1333
1334 .. method:: window.syncup()
1335
1336 Touch all locations in ancestors of the window that have been changed in the
1337 window.
1338
1339
1340 .. method:: window.timeout(delay)
1341
1342 Set blocking or non-blocking read behavior for the window. If *delay* is
1343 negative, blocking read is used (which will wait indefinitely for input). If
1344 *delay* is zero, then non-blocking read is used, and :meth:`getch` will
1345 return ``-1`` if no input is waiting. If *delay* is positive, then
1346 :meth:`getch` will block for *delay* milliseconds, and return ``-1`` if there is
1347 still no input at the end of that time.
1348
1349
1350 .. method:: window.touchline(start, count[, changed])
1351
1352 Pretend *count* lines have been changed, starting with line *start*. If
1353 *changed* is supplied, it specifies whether the affected lines are marked as
1354 having been changed (*changed*\ ``=True``) or unchanged (*changed*\ ``=False``).
1355
1356
1357 .. method:: window.touchwin()
1358
1359 Pretend the whole window has been changed, for purposes of drawing
1360 optimizations.
1361
1362
1363 .. method:: window.untouchwin()
1364
1365 Mark all lines in the window as unchanged since the last call to
1366 :meth:`refresh`.
1367
1368
1369 .. method:: window.vline(ch, n[, attr])
1370 window.vline(y, x, ch, n[, attr])
1371
1372 Display a vertical line starting at ``(y, x)`` with length *n* consisting of the
1373 character *ch* with attributes *attr*.
1374
1375
1376 Constants
1377 ---------
1378
1379 The :mod:`curses` module defines the following data members:
1380
1381
1382 .. data:: ERR
1383
1384 Some curses routines that return an integer, such as :meth:`~window.getch`, return
1385 :const:`ERR` upon failure.
1386
1387
1388 .. data:: OK
1389
1390 Some curses routines that return an integer, such as :func:`napms`, return
1391 :const:`OK` upon success.
1392
1393
1394 .. data:: version
1395
1396 A bytes object representing the current version of the module.
1397
1398
1399 .. data:: ncurses_version
1400
1401 A named tuple containing the three components of the ncurses library
1402 version: *major*, *minor*, and *patch*. All values are integers. The
1403 components can also be accessed by name, so ``curses.ncurses_version[0]``
1404 is equivalent to ``curses.ncurses_version.major`` and so on.
1405
1406 Availability: if the ncurses library is used.
1407
1408 .. versionadded:: 3.8
1409
1410 .. data:: COLORS
1411
1412 The maximum number of colors the terminal can support.
1413 It is defined only after the call to :func:`start_color`.
1414
1415 .. data:: COLOR_PAIRS
1416
1417 The maximum number of color pairs the terminal can support.
1418 It is defined only after the call to :func:`start_color`.
1419
1420 .. data:: COLS
1421
1422 The width of the screen, that is, the number of columns.
1423 It is defined only after the call to :func:`initscr`.
1424 Updated by :func:`update_lines_cols`, :func:`resizeterm` and
1425 :func:`resize_term`.
1426
1427 .. data:: LINES
1428
1429 The height of the screen, that is, the number of lines.
1430 It is defined only after the call to :func:`initscr`.
1431 Updated by :func:`update_lines_cols`, :func:`resizeterm` and
1432 :func:`resize_term`.
1433
1434
1435 Some constants are available to specify character cell attributes.
1436 The exact constants available are system dependent.
1437
1438 +------------------------+-------------------------------+
1439 | Attribute | Meaning |
1440 +========================+===============================+
1441 | .. data:: A_ALTCHARSET | Alternate character set mode |
1442 +------------------------+-------------------------------+
1443 | .. data:: A_BLINK | Blink mode |
1444 +------------------------+-------------------------------+
1445 | .. data:: A_BOLD | Bold mode |
1446 +------------------------+-------------------------------+
1447 | .. data:: A_DIM | Dim mode |
1448 +------------------------+-------------------------------+
1449 | .. data:: A_INVIS | Invisible or blank mode |
1450 +------------------------+-------------------------------+
1451 | .. data:: A_ITALIC | Italic mode |
1452 +------------------------+-------------------------------+
1453 | .. data:: A_NORMAL | Normal attribute |
1454 +------------------------+-------------------------------+
1455 | .. data:: A_PROTECT | Protected mode |
1456 +------------------------+-------------------------------+
1457 | .. data:: A_REVERSE | Reverse background and |
1458 | | foreground colors |
1459 +------------------------+-------------------------------+
1460 | .. data:: A_STANDOUT | Standout mode |
1461 +------------------------+-------------------------------+
1462 | .. data:: A_UNDERLINE | Underline mode |
1463 +------------------------+-------------------------------+
1464 | .. data:: A_HORIZONTAL | Horizontal highlight |
1465 +------------------------+-------------------------------+
1466 | .. data:: A_LEFT | Left highlight |
1467 +------------------------+-------------------------------+
1468 | .. data:: A_LOW | Low highlight |
1469 +------------------------+-------------------------------+
1470 | .. data:: A_RIGHT | Right highlight |
1471 +------------------------+-------------------------------+
1472 | .. data:: A_TOP | Top highlight |
1473 +------------------------+-------------------------------+
1474 | .. data:: A_VERTICAL | Vertical highlight |
1475 +------------------------+-------------------------------+
1476
1477 .. versionadded:: 3.7
1478 ``A_ITALIC`` was added.
1479
1480 Several constants are available to extract corresponding attributes returned
1481 by some methods.
1482
1483 +-------------------------+-------------------------------+
1484 | Bit-mask | Meaning |
1485 +=========================+===============================+
1486 | .. data:: A_ATTRIBUTES | Bit-mask to extract |
1487 | | attributes |
1488 +-------------------------+-------------------------------+
1489 | .. data:: A_CHARTEXT | Bit-mask to extract a |
1490 | | character |
1491 +-------------------------+-------------------------------+
1492 | .. data:: A_COLOR | Bit-mask to extract |
1493 | | color-pair field information |
1494 +-------------------------+-------------------------------+
1495
1496 Keys are referred to by integer constants with names starting with ``KEY_``.
1497 The exact keycaps available are system dependent.
1498
1499 .. XXX this table is far too large! should it be alphabetized?
1500
1501 +-------------------------+--------------------------------------------+
1502 | Key constant | Key |
1503 +=========================+============================================+
1504 | .. data:: KEY_MIN | Minimum key value |
1505 +-------------------------+--------------------------------------------+
1506 | .. data:: KEY_BREAK | Break key (unreliable) |
1507 +-------------------------+--------------------------------------------+
1508 | .. data:: KEY_DOWN | Down-arrow |
1509 +-------------------------+--------------------------------------------+
1510 | .. data:: KEY_UP | Up-arrow |
1511 +-------------------------+--------------------------------------------+
1512 | .. data:: KEY_LEFT | Left-arrow |
1513 +-------------------------+--------------------------------------------+
1514 | .. data:: KEY_RIGHT | Right-arrow |
1515 +-------------------------+--------------------------------------------+
1516 | .. data:: KEY_HOME | Home key (upward+left arrow) |
1517 +-------------------------+--------------------------------------------+
1518 | .. data:: KEY_BACKSPACE | Backspace (unreliable) |
1519 +-------------------------+--------------------------------------------+
1520 | .. data:: KEY_F0 | Function keys. Up to 64 function keys are |
1521 | | supported. |
1522 +-------------------------+--------------------------------------------+
1523 | .. data:: KEY_Fn | Value of function key *n* |
1524 +-------------------------+--------------------------------------------+
1525 | .. data:: KEY_DL | Delete line |
1526 +-------------------------+--------------------------------------------+
1527 | .. data:: KEY_IL | Insert line |
1528 +-------------------------+--------------------------------------------+
1529 | .. data:: KEY_DC | Delete character |
1530 +-------------------------+--------------------------------------------+
1531 | .. data:: KEY_IC | Insert char or enter insert mode |
1532 +-------------------------+--------------------------------------------+
1533 | .. data:: KEY_EIC | Exit insert char mode |
1534 +-------------------------+--------------------------------------------+
1535 | .. data:: KEY_CLEAR | Clear screen |
1536 +-------------------------+--------------------------------------------+
1537 | .. data:: KEY_EOS | Clear to end of screen |
1538 +-------------------------+--------------------------------------------+
1539 | .. data:: KEY_EOL | Clear to end of line |
1540 +-------------------------+--------------------------------------------+
1541 | .. data:: KEY_SF | Scroll 1 line forward |
1542 +-------------------------+--------------------------------------------+
1543 | .. data:: KEY_SR | Scroll 1 line backward (reverse) |
1544 +-------------------------+--------------------------------------------+
1545 | .. data:: KEY_NPAGE | Next page |
1546 +-------------------------+--------------------------------------------+
1547 | .. data:: KEY_PPAGE | Previous page |
1548 +-------------------------+--------------------------------------------+
1549 | .. data:: KEY_STAB | Set tab |
1550 +-------------------------+--------------------------------------------+
1551 | .. data:: KEY_CTAB | Clear tab |
1552 +-------------------------+--------------------------------------------+
1553 | .. data:: KEY_CATAB | Clear all tabs |
1554 +-------------------------+--------------------------------------------+
1555 | .. data:: KEY_ENTER | Enter or send (unreliable) |
1556 +-------------------------+--------------------------------------------+
1557 | .. data:: KEY_SRESET | Soft (partial) reset (unreliable) |
1558 +-------------------------+--------------------------------------------+
1559 | .. data:: KEY_RESET | Reset or hard reset (unreliable) |
1560 +-------------------------+--------------------------------------------+
1561 | .. data:: KEY_PRINT | Print |
1562 +-------------------------+--------------------------------------------+
1563 | .. data:: KEY_LL | Home down or bottom (lower left) |
1564 +-------------------------+--------------------------------------------+
1565 | .. data:: KEY_A1 | Upper left of keypad |
1566 +-------------------------+--------------------------------------------+
1567 | .. data:: KEY_A3 | Upper right of keypad |
1568 +-------------------------+--------------------------------------------+
1569 | .. data:: KEY_B2 | Center of keypad |
1570 +-------------------------+--------------------------------------------+
1571 | .. data:: KEY_C1 | Lower left of keypad |
1572 +-------------------------+--------------------------------------------+
1573 | .. data:: KEY_C3 | Lower right of keypad |
1574 +-------------------------+--------------------------------------------+
1575 | .. data:: KEY_BTAB | Back tab |
1576 +-------------------------+--------------------------------------------+
1577 | .. data:: KEY_BEG | Beg (beginning) |
1578 +-------------------------+--------------------------------------------+
1579 | .. data:: KEY_CANCEL | Cancel |
1580 +-------------------------+--------------------------------------------+
1581 | .. data:: KEY_CLOSE | Close |
1582 +-------------------------+--------------------------------------------+
1583 | .. data:: KEY_COMMAND | Cmd (command) |
1584 +-------------------------+--------------------------------------------+
1585 | .. data:: KEY_COPY | Copy |
1586 +-------------------------+--------------------------------------------+
1587 | .. data:: KEY_CREATE | Create |
1588 +-------------------------+--------------------------------------------+
1589 | .. data:: KEY_END | End |
1590 +-------------------------+--------------------------------------------+
1591 | .. data:: KEY_EXIT | Exit |
1592 +-------------------------+--------------------------------------------+
1593 | .. data:: KEY_FIND | Find |
1594 +-------------------------+--------------------------------------------+
1595 | .. data:: KEY_HELP | Help |
1596 +-------------------------+--------------------------------------------+
1597 | .. data:: KEY_MARK | Mark |
1598 +-------------------------+--------------------------------------------+
1599 | .. data:: KEY_MESSAGE | Message |
1600 +-------------------------+--------------------------------------------+
1601 | .. data:: KEY_MOVE | Move |
1602 +-------------------------+--------------------------------------------+
1603 | .. data:: KEY_NEXT | Next |
1604 +-------------------------+--------------------------------------------+
1605 | .. data:: KEY_OPEN | Open |
1606 +-------------------------+--------------------------------------------+
1607 | .. data:: KEY_OPTIONS | Options |
1608 +-------------------------+--------------------------------------------+
1609 | .. data:: KEY_PREVIOUS | Prev (previous) |
1610 +-------------------------+--------------------------------------------+
1611 | .. data:: KEY_REDO | Redo |
1612 +-------------------------+--------------------------------------------+
1613 | .. data:: KEY_REFERENCE | Ref (reference) |
1614 +-------------------------+--------------------------------------------+
1615 | .. data:: KEY_REFRESH | Refresh |
1616 +-------------------------+--------------------------------------------+
1617 | .. data:: KEY_REPLACE | Replace |
1618 +-------------------------+--------------------------------------------+
1619 | .. data:: KEY_RESTART | Restart |
1620 +-------------------------+--------------------------------------------+
1621 | .. data:: KEY_RESUME | Resume |
1622 +-------------------------+--------------------------------------------+
1623 | .. data:: KEY_SAVE | Save |
1624 +-------------------------+--------------------------------------------+
1625 | .. data:: KEY_SBEG | Shifted Beg (beginning) |
1626 +-------------------------+--------------------------------------------+
1627 | .. data:: KEY_SCANCEL | Shifted Cancel |
1628 +-------------------------+--------------------------------------------+
1629 | .. data:: KEY_SCOMMAND | Shifted Command |
1630 +-------------------------+--------------------------------------------+
1631 | .. data:: KEY_SCOPY | Shifted Copy |
1632 +-------------------------+--------------------------------------------+
1633 | .. data:: KEY_SCREATE | Shifted Create |
1634 +-------------------------+--------------------------------------------+
1635 | .. data:: KEY_SDC | Shifted Delete char |
1636 +-------------------------+--------------------------------------------+
1637 | .. data:: KEY_SDL | Shifted Delete line |
1638 +-------------------------+--------------------------------------------+
1639 | .. data:: KEY_SELECT | Select |
1640 +-------------------------+--------------------------------------------+
1641 | .. data:: KEY_SEND | Shifted End |
1642 +-------------------------+--------------------------------------------+
1643 | .. data:: KEY_SEOL | Shifted Clear line |
1644 +-------------------------+--------------------------------------------+
1645 | .. data:: KEY_SEXIT | Shifted Exit |
1646 +-------------------------+--------------------------------------------+
1647 | .. data:: KEY_SFIND | Shifted Find |
1648 +-------------------------+--------------------------------------------+
1649 | .. data:: KEY_SHELP | Shifted Help |
1650 +-------------------------+--------------------------------------------+
1651 | .. data:: KEY_SHOME | Shifted Home |
1652 +-------------------------+--------------------------------------------+
1653 | .. data:: KEY_SIC | Shifted Input |
1654 +-------------------------+--------------------------------------------+
1655 | .. data:: KEY_SLEFT | Shifted Left arrow |
1656 +-------------------------+--------------------------------------------+
1657 | .. data:: KEY_SMESSAGE | Shifted Message |
1658 +-------------------------+--------------------------------------------+
1659 | .. data:: KEY_SMOVE | Shifted Move |
1660 +-------------------------+--------------------------------------------+
1661 | .. data:: KEY_SNEXT | Shifted Next |
1662 +-------------------------+--------------------------------------------+
1663 | .. data:: KEY_SOPTIONS | Shifted Options |
1664 +-------------------------+--------------------------------------------+
1665 | .. data:: KEY_SPREVIOUS | Shifted Prev |
1666 +-------------------------+--------------------------------------------+
1667 | .. data:: KEY_SPRINT | Shifted Print |
1668 +-------------------------+--------------------------------------------+
1669 | .. data:: KEY_SREDO | Shifted Redo |
1670 +-------------------------+--------------------------------------------+
1671 | .. data:: KEY_SREPLACE | Shifted Replace |
1672 +-------------------------+--------------------------------------------+
1673 | .. data:: KEY_SRIGHT | Shifted Right arrow |
1674 +-------------------------+--------------------------------------------+
1675 | .. data:: KEY_SRSUME | Shifted Resume |
1676 +-------------------------+--------------------------------------------+
1677 | .. data:: KEY_SSAVE | Shifted Save |
1678 +-------------------------+--------------------------------------------+
1679 | .. data:: KEY_SSUSPEND | Shifted Suspend |
1680 +-------------------------+--------------------------------------------+
1681 | .. data:: KEY_SUNDO | Shifted Undo |
1682 +-------------------------+--------------------------------------------+
1683 | .. data:: KEY_SUSPEND | Suspend |
1684 +-------------------------+--------------------------------------------+
1685 | .. data:: KEY_UNDO | Undo |
1686 +-------------------------+--------------------------------------------+
1687 | .. data:: KEY_MOUSE | Mouse event has occurred |
1688 +-------------------------+--------------------------------------------+
1689 | .. data:: KEY_RESIZE | Terminal resize event |
1690 +-------------------------+--------------------------------------------+
1691 | .. data:: KEY_MAX | Maximum key value |
1692 +-------------------------+--------------------------------------------+
1693
1694 On VT100s and their software emulations, such as X terminal emulators, there are
1695 normally at least four function keys (:const:`KEY_F1 <KEY_Fn>`, :const:`KEY_F2 <KEY_Fn>`,
1696 :const:`KEY_F3 <KEY_Fn>`, :const:`KEY_F4 <KEY_Fn>`) available, and the arrow keys mapped to
1697 :const:`KEY_UP`, :const:`KEY_DOWN`, :const:`KEY_LEFT` and :const:`KEY_RIGHT` in
1698 the obvious way. If your machine has a PC keyboard, it is safe to expect arrow
1699 keys and twelve function keys (older PC keyboards may have only ten function
1700 keys); also, the following keypad mappings are standard:
1701
1702 +------------------+-----------+
1703 | Keycap | Constant |
1704 +==================+===========+
1705 | :kbd:`Insert` | KEY_IC |
1706 +------------------+-----------+
1707 | :kbd:`Delete` | KEY_DC |
1708 +------------------+-----------+
1709 | :kbd:`Home` | KEY_HOME |
1710 +------------------+-----------+
1711 | :kbd:`End` | KEY_END |
1712 +------------------+-----------+
1713 | :kbd:`Page Up` | KEY_PPAGE |
1714 +------------------+-----------+
1715 | :kbd:`Page Down` | KEY_NPAGE |
1716 +------------------+-----------+
1717
1718 .. _curses-acs-codes:
1719
1720 The following table lists characters from the alternate character set. These are
1721 inherited from the VT100 terminal, and will generally be available on software
1722 emulations such as X terminals. When there is no graphic available, curses
1723 falls back on a crude printable ASCII approximation.
1724
1725 .. note::
1726
1727 These are available only after :func:`initscr` has been called.
1728
1729 +------------------------+------------------------------------------+
1730 | ACS code | Meaning |
1731 +========================+==========================================+
1732 | .. data:: ACS_BBSS | alternate name for upper-right corner |
1733 +------------------------+------------------------------------------+
1734 | .. data:: ACS_BLOCK | solid square block |
1735 +------------------------+------------------------------------------+
1736 | .. data:: ACS_BOARD | board of squares |
1737 +------------------------+------------------------------------------+
1738 | .. data:: ACS_BSBS | alternate name for horizontal line |
1739 +------------------------+------------------------------------------+
1740 | .. data:: ACS_BSSB | alternate name for upper-left corner |
1741 +------------------------+------------------------------------------+
1742 | .. data:: ACS_BSSS | alternate name for top tee |
1743 +------------------------+------------------------------------------+
1744 | .. data:: ACS_BTEE | bottom tee |
1745 +------------------------+------------------------------------------+
1746 | .. data:: ACS_BULLET | bullet |
1747 +------------------------+------------------------------------------+
1748 | .. data:: ACS_CKBOARD | checker board (stipple) |
1749 +------------------------+------------------------------------------+
1750 | .. data:: ACS_DARROW | arrow pointing down |
1751 +------------------------+------------------------------------------+
1752 | .. data:: ACS_DEGREE | degree symbol |
1753 +------------------------+------------------------------------------+
1754 | .. data:: ACS_DIAMOND | diamond |
1755 +------------------------+------------------------------------------+
1756 | .. data:: ACS_GEQUAL | greater-than-or-equal-to |
1757 +------------------------+------------------------------------------+
1758 | .. data:: ACS_HLINE | horizontal line |
1759 +------------------------+------------------------------------------+
1760 | .. data:: ACS_LANTERN | lantern symbol |
1761 +------------------------+------------------------------------------+
1762 | .. data:: ACS_LARROW | left arrow |
1763 +------------------------+------------------------------------------+
1764 | .. data:: ACS_LEQUAL | less-than-or-equal-to |
1765 +------------------------+------------------------------------------+
1766 | .. data:: ACS_LLCORNER | lower-left corner |
1767 +------------------------+------------------------------------------+
1768 | .. data:: ACS_LRCORNER | lower-right corner |
1769 +------------------------+------------------------------------------+
1770 | .. data:: ACS_LTEE | left tee |
1771 +------------------------+------------------------------------------+
1772 | .. data:: ACS_NEQUAL | not-equal sign |
1773 +------------------------+------------------------------------------+
1774 | .. data:: ACS_PI | letter pi |
1775 +------------------------+------------------------------------------+
1776 | .. data:: ACS_PLMINUS | plus-or-minus sign |
1777 +------------------------+------------------------------------------+
1778 | .. data:: ACS_PLUS | big plus sign |
1779 +------------------------+------------------------------------------+
1780 | .. data:: ACS_RARROW | right arrow |
1781 +------------------------+------------------------------------------+
1782 | .. data:: ACS_RTEE | right tee |
1783 +------------------------+------------------------------------------+
1784 | .. data:: ACS_S1 | scan line 1 |
1785 +------------------------+------------------------------------------+
1786 | .. data:: ACS_S3 | scan line 3 |
1787 +------------------------+------------------------------------------+
1788 | .. data:: ACS_S7 | scan line 7 |
1789 +------------------------+------------------------------------------+
1790 | .. data:: ACS_S9 | scan line 9 |
1791 +------------------------+------------------------------------------+
1792 | .. data:: ACS_SBBS | alternate name for lower-right corner |
1793 +------------------------+------------------------------------------+
1794 | .. data:: ACS_SBSB | alternate name for vertical line |
1795 +------------------------+------------------------------------------+
1796 | .. data:: ACS_SBSS | alternate name for right tee |
1797 +------------------------+------------------------------------------+
1798 | .. data:: ACS_SSBB | alternate name for lower-left corner |
1799 +------------------------+------------------------------------------+
1800 | .. data:: ACS_SSBS | alternate name for bottom tee |
1801 +------------------------+------------------------------------------+
1802 | .. data:: ACS_SSSB | alternate name for left tee |
1803 +------------------------+------------------------------------------+
1804 | .. data:: ACS_SSSS | alternate name for crossover or big plus |
1805 +------------------------+------------------------------------------+
1806 | .. data:: ACS_STERLING | pound sterling |
1807 +------------------------+------------------------------------------+
1808 | .. data:: ACS_TTEE | top tee |
1809 +------------------------+------------------------------------------+
1810 | .. data:: ACS_UARROW | up arrow |
1811 +------------------------+------------------------------------------+
1812 | .. data:: ACS_ULCORNER | upper-left corner |
1813 +------------------------+------------------------------------------+
1814 | .. data:: ACS_URCORNER | upper-right corner |
1815 +------------------------+------------------------------------------+
1816 | .. data:: ACS_VLINE | vertical line |
1817 +------------------------+------------------------------------------+
1818
1819 The following table lists mouse button constants used by :meth:`getmouse`:
1820
1821 +----------------------------------+---------------------------------------------+
1822 | Mouse button constant | Meaning |
1823 +==================================+=============================================+
1824 | .. data:: BUTTONn_PRESSED | Mouse button *n* pressed |
1825 +----------------------------------+---------------------------------------------+
1826 | .. data:: BUTTONn_RELEASED | Mouse button *n* released |
1827 +----------------------------------+---------------------------------------------+
1828 | .. data:: BUTTONn_CLICKED | Mouse button *n* clicked |
1829 +----------------------------------+---------------------------------------------+
1830 | .. data:: BUTTONn_DOUBLE_CLICKED | Mouse button *n* double clicked |
1831 +----------------------------------+---------------------------------------------+
1832 | .. data:: BUTTONn_TRIPLE_CLICKED | Mouse button *n* triple clicked |
1833 +----------------------------------+---------------------------------------------+
1834 | .. data:: BUTTON_SHIFT | Shift was down during button state change |
1835 +----------------------------------+---------------------------------------------+
1836 | .. data:: BUTTON_CTRL | Control was down during button state change |
1837 +----------------------------------+---------------------------------------------+
1838 | .. data:: BUTTON_ALT | Alt was down during button state change |
1839 +----------------------------------+---------------------------------------------+
1840
1841 .. versionchanged:: 3.10
1842 The ``BUTTON5_*`` constants are now exposed if they are provided by the
1843 underlying curses library.
1844
1845 The following table lists the predefined colors:
1846
1847 +-------------------------+----------------------------+
1848 | Constant | Color |
1849 +=========================+============================+
1850 | .. data:: COLOR_BLACK | Black |
1851 +-------------------------+----------------------------+
1852 | .. data:: COLOR_BLUE | Blue |
1853 +-------------------------+----------------------------+
1854 | .. data:: COLOR_CYAN | Cyan (light greenish blue) |
1855 +-------------------------+----------------------------+
1856 | .. data:: COLOR_GREEN | Green |
1857 +-------------------------+----------------------------+
1858 | .. data:: COLOR_MAGENTA | Magenta (purplish red) |
1859 +-------------------------+----------------------------+
1860 | .. data:: COLOR_RED | Red |
1861 +-------------------------+----------------------------+
1862 | .. data:: COLOR_WHITE | White |
1863 +-------------------------+----------------------------+
1864 | .. data:: COLOR_YELLOW | Yellow |
1865 +-------------------------+----------------------------+
1866
1867
1868 :mod:`curses.textpad` --- Text input widget for curses programs
1869 ===============================================================
1870
1871 .. module:: curses.textpad
1872 :synopsis: Emacs-like input editing in a curses window.
1873 .. moduleauthor:: Eric Raymond <[email protected]>
1874 .. sectionauthor:: Eric Raymond <[email protected]>
1875
1876
1877 The :mod:`curses.textpad` module provides a :class:`Textbox` class that handles
1878 elementary text editing in a curses window, supporting a set of keybindings
1879 resembling those of Emacs (thus, also of Netscape Navigator, BBedit 6.x,
1880 FrameMaker, and many other programs). The module also provides a
1881 rectangle-drawing function useful for framing text boxes or for other purposes.
1882
1883 The module :mod:`curses.textpad` defines the following function:
1884
1885
1886 .. function:: rectangle(win, uly, ulx, lry, lrx)
1887
1888 Draw a rectangle. The first argument must be a window object; the remaining
1889 arguments are coordinates relative to that window. The second and third
1890 arguments are the y and x coordinates of the upper-left corner of the
1891 rectangle to be drawn; the fourth and fifth arguments are the y and x
1892 coordinates of the lower-right corner. The rectangle will be drawn using
1893 VT100/IBM PC forms characters on terminals that make this possible (including
1894 xterm and most other software terminal emulators). Otherwise it will be drawn
1895 with ASCII dashes, vertical bars, and plus signs.
1896
1897
1898 .. _curses-textpad-objects:
1899
1900 Textbox objects
1901 ---------------
1902
1903 You can instantiate a :class:`Textbox` object as follows:
1904
1905
1906 .. class:: Textbox(win, insert_mode=False)
1907
1908 Return a textbox widget object. The *win* argument should be a curses
1909 :ref:`window <curses-window-objects>` object in which the textbox is to
1910 be contained. If *insert_mode* is true, the textbox inserts typed
1911 characters, shifting existing text to the right, rather than overwriting it.
1912 The edit cursor of the textbox is initially located at the
1913 upper-left corner of the containing window, with coordinates ``(0, 0)``.
1914 The instance's :attr:`stripspaces` flag is initially on.
1915
1916 :class:`Textbox` objects have the following methods:
1917
1918
1919 .. method:: edit(validate=None)
1920
1921 This is the entry point you will normally use. It accepts editing
1922 keystrokes until one of the termination keystrokes is entered. If
1923 *validate* is supplied, it must be a function. It will be called for
1924 each keystroke entered with the keystroke as a parameter; command dispatch
1925 is done on the result. If it returns a false value, the keystroke is
1926 ignored. This method returns the window contents as a
1927 string; whether blanks in the window are included is affected by the
1928 :attr:`stripspaces` attribute.
1929
1930
1931 .. method:: do_command(ch)
1932
1933 Process a single command keystroke. Returns ``1`` to continue editing,
1934 or ``0`` if a termination keystroke was processed. Here are the supported
1935 special keystrokes:
1936
1937 +------------------+-------------------------------------------+
1938 | Keystroke | Action |
1939 +==================+===========================================+
1940 | :kbd:`Control-A` | Go to left edge of window. |
1941 +------------------+-------------------------------------------+
1942 | :kbd:`Control-B` | Cursor left, wrapping to previous line if |
1943 | | appropriate. |
1944 +------------------+-------------------------------------------+
1945 | :kbd:`Control-D` | Delete character under cursor. |
1946 +------------------+-------------------------------------------+
1947 | :kbd:`Control-E` | Go to right edge (stripspaces off) or end |
1948 | | of line (stripspaces on). |
1949 +------------------+-------------------------------------------+
1950 | :kbd:`Control-F` | Cursor right, wrapping to next line when |
1951 | | appropriate. |
1952 +------------------+-------------------------------------------+
1953 | :kbd:`Control-G` | Terminate, returning the window contents. |
1954 +------------------+-------------------------------------------+
1955 | :kbd:`Control-H` | Delete character backward. |
1956 +------------------+-------------------------------------------+
1957 | :kbd:`Control-J` | Terminate if the window is 1 line, |
1958 | | otherwise move to the start of the next |
1959 | | line. |
1960 +------------------+-------------------------------------------+
1961 | :kbd:`Control-K` | If line is blank, delete it, otherwise |
1962 | | clear to end of line. |
1963 +------------------+-------------------------------------------+
1964 | :kbd:`Control-L` | Refresh screen. |
1965 +------------------+-------------------------------------------+
1966 | :kbd:`Control-N` | Cursor down; move down one line. |
1967 +------------------+-------------------------------------------+
1968 | :kbd:`Control-O` | Insert a blank line at cursor location. |
1969 +------------------+-------------------------------------------+
1970 | :kbd:`Control-P` | Cursor up; move up one line. |
1971 +------------------+-------------------------------------------+
1972
1973 Move operations do nothing if the cursor is at an edge where the movement
1974 is not possible. The following synonyms are supported where possible:
1975
1976 +--------------------------------+------------------+
1977 | Constant | Keystroke |
1978 +================================+==================+
1979 | :const:`~curses.KEY_LEFT` | :kbd:`Control-B` |
1980 +--------------------------------+------------------+
1981 | :const:`~curses.KEY_RIGHT` | :kbd:`Control-F` |
1982 +--------------------------------+------------------+
1983 | :const:`~curses.KEY_UP` | :kbd:`Control-P` |
1984 +--------------------------------+------------------+
1985 | :const:`~curses.KEY_DOWN` | :kbd:`Control-N` |
1986 +--------------------------------+------------------+
1987 | :const:`~curses.KEY_BACKSPACE` | :kbd:`Control-h` |
1988 +--------------------------------+------------------+
1989
1990 All other keystrokes are treated as a command to insert the given
1991 character and move right (with line wrapping).
1992
1993
1994 .. method:: gather()
1995
1996 Return the window contents as a string; whether blanks in the
1997 window are included is affected by the :attr:`stripspaces` member.
1998
1999
2000 .. attribute:: stripspaces
2001
2002 This attribute is a flag which controls the interpretation of blanks in
2003 the window. When it is on, trailing blanks on each line are ignored; any
2004 cursor motion that would land the cursor on a trailing blank goes to the
2005 end of that line instead, and trailing blanks are stripped when the window
2006 contents are gathered.