4. Displaying Text

From a C programmer's point of view, curses may sometimes look like a twisty maze of functions, all subtly different. For example, addstr() displays a string at the current cursor location in the stdscr window, while mvaddstr() moves to a given y,x coordinate first before displaying the string. waddstr() is just like addstr(), but allows specifying a window to use, instead of using stdscr by default. mvwaddstr() follows similarly.

Fortunately the Python interface hides all these details; stdscr is a window object like any other, and methods like addstr() accept multiple argument forms. Usually there are four different forms.

Form Description
str or ch Display the string str or character ch
str or ch, attr Display the string str or character ch, using attribute attr
y, x, str or ch Move to position y,x within the window, and display str or ch
y, x, str or ch, attr Move to position y,x within the window, and display str or ch, using attribute attr

Attributes allow displaying text in highlighted forms, such as in boldface, underline, reverse code, or in colour. They'll be explained in more detail in the next subsection.

The addstr() function takes a Python string as the value to be displayed, while the addch() functions take a character, which can be either a Python string of length 1, or an integer. If it's a string, you're limited to displaying characters between 0 and 255. SVr4 curses provides constants for extension characters; these constants are integers greater than 255. For example, ACS_PLMINUS is a +/- symbol, and ACS_ULCORNER is the upper left corner of a box (handy for drawing borders).

Windows remember where the cursor was left after the last operation, so if you leave out the y,x coordinates, the string or character will be displayed wherever the last operation left off. You can also move the cursor with the move(y,x) method. Because some terminals always display a flashing cursor, you may want to ensure that the cursor is positioned in some location where it won't be distracting; it can be confusing to have the cursor blinking at some apparently random location. If your application doesn't need a blinking cursor at all, you can call the leaveok(bool) method. When bool is true, the curses library will attempt to suppress the flashing cursor, and you won't need to worry about leaving it in odd locations.