4
4
Curses Programming with Python
5
5
**********************************
6
6
7
+ .. currentmodule :: curses
8
+
7
9
:Author: A.M. Kuchling, Eric S. Raymond
8
10
:Release: 2.04
9
11
@@ -65,7 +67,7 @@ The Python module is a fairly simple wrapper over the C functions provided by
65
67
curses; if you're already familiar with curses programming in C, it's really
66
68
easy to transfer that knowledge to Python. The biggest difference is that the
67
69
Python interface makes things simpler by merging different C functions such as
68
- :c:func: `addstr `, :c:func: `mvaddstr `, and :c:func: `mvwaddstr ` into a single
70
+ :c:func: `! addstr `, :c:func: `! mvaddstr `, and :c:func: `! mvwaddstr ` into a single
69
71
:meth: `~curses.window.addstr ` method. You'll see this covered in more
70
72
detail later.
71
73
@@ -82,7 +84,7 @@ Before doing anything, curses must be initialized. This is done by
82
84
calling the :func: `~curses.initscr ` function, which will determine the
83
85
terminal type, send any required setup codes to the terminal, and
84
86
create various internal data structures. If successful,
85
- :func: `initscr ` returns a window object representing the entire
87
+ :func: `! initscr ` returns a window object representing the entire
86
88
screen; this is usually called ``stdscr `` after the name of the
87
89
corresponding C variable. ::
88
90
@@ -151,8 +153,8 @@ importing the :func:`curses.wrapper` function and using it like this::
151
153
152
154
The :func: `~curses.wrapper ` function takes a callable object and does the
153
155
initializations described above, also initializing colors if color
154
- support is present. :func: `wrapper ` then runs your provided callable.
155
- Once the callable returns, :func: `wrapper ` will restore the original
156
+ support is present. :func: `! wrapper ` then runs your provided callable.
157
+ Once the callable returns, :func: `! wrapper ` will restore the original
156
158
state of the terminal. The callable is called inside a
157
159
:keyword: `try `...\ :keyword: `except ` that catches exceptions, restores
158
160
the state of the terminal, and then re-raises the exception. Therefore
@@ -200,7 +202,7 @@ This is because curses was originally written with slow 300-baud
200
202
terminal connections in mind; with these terminals, minimizing the
201
203
time required to redraw the screen was very important. Instead curses
202
204
accumulates changes to the screen and displays them in the most
203
- efficient manner when you call :meth: `refresh `. For example, if your
205
+ efficient manner when you call :meth: `! refresh `. For example, if your
204
206
program displays some text in a window and then clears the window,
205
207
there's no need to send the original text because they're never
206
208
visible.
@@ -210,7 +212,7 @@ really complicate programming with curses much. Most programs go into a flurry
210
212
of activity, and then pause waiting for a keypress or some other action on the
211
213
part of the user. All you have to do is to be sure that the screen has been
212
214
redrawn before pausing to wait for user input, by first calling
213
- `` stdscr.refresh() `` or the :meth: `refresh ` method of some other relevant
215
+ :meth: ` ! stdscr.refresh` or the :meth: `! refresh ` method of some other relevant
214
216
window.
215
217
216
218
A pad is a special case of a window; it can be larger than the actual display
@@ -234,15 +236,15 @@ displayed. ::
234
236
# : filled with pad content.
235
237
pad.refresh( 0,0, 5,5, 20,75)
236
238
237
- The :meth: `refresh ` call displays a section of the pad in the rectangle
239
+ The :meth: `! refresh ` call displays a section of the pad in the rectangle
238
240
extending from coordinate (5,5) to coordinate (20,75) on the screen; the upper
239
241
left corner of the displayed section is coordinate (0,0) on the pad. Beyond
240
242
that difference, pads are exactly like ordinary windows and support the same
241
243
methods.
242
244
243
245
If you have multiple windows and pads on screen there is a more
244
246
efficient way to update the screen and prevent annoying screen flicker
245
- as each part of the screen gets updated. :meth: `refresh ` actually
247
+ as each part of the screen gets updated. :meth: `! refresh ` actually
246
248
does two things:
247
249
248
250
1) Calls the :meth: `~curses.window.noutrefresh ` method of each window
@@ -251,8 +253,8 @@ does two things:
251
253
2) Calls the function :func: `~curses.doupdate ` function to change the
252
254
physical screen to match the desired state recorded in the data structure.
253
255
254
- Instead you can call :meth: `noutrefresh ` on a number of windows to
255
- update the data structure, and then call :func: `doupdate ` to update
256
+ Instead you can call :meth: `! noutrefresh ` on a number of windows to
257
+ update the data structure, and then call :func: `! doupdate ` to update
256
258
the screen.
257
259
258
260
@@ -261,11 +263,11 @@ Displaying Text
261
263
262
264
From a C programmer's point of view, curses may sometimes look like a
263
265
twisty maze of functions, all subtly different. For example,
264
- :c:func: `addstr ` displays a string at the current cursor location in
265
- the ``stdscr `` window, while :c:func: `mvaddstr ` moves to a given y,x
266
- coordinate first before displaying the string. :c:func: `waddstr ` is just
267
- like :c:func: `addstr `, but allows specifying a window to use instead of
268
- using ``stdscr `` by default. :c:func: `mvwaddstr ` allows specifying both
266
+ :c:func: `! addstr ` displays a string at the current cursor location in
267
+ the ``stdscr `` window, while :c:func: `! mvaddstr ` moves to a given y,x
268
+ coordinate first before displaying the string. :c:func: `! waddstr ` is just
269
+ like :c:func: `! addstr `, but allows specifying a window to use instead of
270
+ using ``stdscr `` by default. :c:func: `! mvwaddstr ` allows specifying both
269
271
a window and a coordinate.
270
272
271
273
Fortunately the Python interface hides all these details. ``stdscr ``
@@ -298,7 +300,7 @@ the next subsection.
298
300
The :meth: `~curses.window.addstr ` method takes a Python string or
299
301
bytestring as the value to be displayed. The contents of bytestrings
300
302
are sent to the terminal as-is. Strings are encoded to bytes using
301
- the value of the window's :attr: `encoding ` attribute; this defaults to
303
+ the value of the window's :attr: `~window. encoding ` attribute; this defaults to
302
304
the default system encoding as returned by :func: `locale.getencoding `.
303
305
304
306
The :meth: `~curses.window.addch ` methods take a character, which can be
@@ -444,15 +446,15 @@ There are two methods for getting input from a window:
444
446
445
447
It's possible to not wait for the user using the
446
448
:meth: `~curses.window.nodelay ` window method. After ``nodelay(True) ``,
447
- :meth: `getch ` and :meth: `getkey ` for the window become
448
- non-blocking. To signal that no input is ready, :meth: `getch ` returns
449
- ``curses.ERR `` (a value of -1) and :meth: `getkey ` raises an exception.
449
+ :meth: `! getch ` and :meth: `! getkey ` for the window become
450
+ non-blocking. To signal that no input is ready, :meth: `! getch ` returns
451
+ ``curses.ERR `` (a value of -1) and :meth: `! getkey ` raises an exception.
450
452
There's also a :func: `~curses.halfdelay ` function, which can be used to (in
451
- effect) set a timer on each :meth: `getch `; if no input becomes
453
+ effect) set a timer on each :meth: `! getch `; if no input becomes
452
454
available within a specified delay (measured in tenths of a second),
453
455
curses raises an exception.
454
456
455
- The :meth: `getch ` method returns an integer; if it's between 0 and 255, it
457
+ The :meth: `! getch ` method returns an integer; if it's between 0 and 255, it
456
458
represents the ASCII code of the key pressed. Values greater than 255 are
457
459
special keys such as Page Up, Home, or the cursor keys. You can compare the
458
460
value returned to constants such as :const: `curses.KEY_PPAGE `,
0 commit comments