Python is an interpreted, high level language that is freely availŽable for developing applications. And Curses or ncurses is a library that lets you program GUIs in Python. Curses library is about window proŽgramming within the boundaries of the terminal. Using this library one can maŽnipulate data, create a sub window or create multiple windows of all sizes that overlap. A programmer can use colors, mouse pointer, etc on screen even if the 'X' server (in Linux or Unix) is not runŽning. For eg, when you're logged in to Linux on a terminal where neither X is installed nor you have the privilege to run it; you can use Curses to write apŽplications which can directly run on the terminal with Mouse and color support.
Although Curses is mostly used in 'C' language (as ncurses.h) for making suitable GUIs, in Python it helps proŽgrammers to develop applications in a simpler way. A lot of components such as buttons, menus and scroll bars are made using Curses. In this article, we discuss the basic concepts for using Curses in Python.
Prerequisites
Check if your system has Python inŽstalled, by issuing the following comŽmand in openSUSE or Fedora:
If the command output is "pythonŽ2.5.1-39;' it means Python version 2.5 is installed on the system. And if the system prompts "package python is not inŽstalled" then you have to install it by isŽsuing the following command.
Using Curses
The following steps show what processes take place from start till the end of a Curses application:
1. Import Curses library to Python (in our case).
2.Initialize Curses.
3.Create one or more windows, as per need.
4. ManipuLate windows for user input and output.
5. CLose the open windows & stop Curses.
Let's start with a simple example where we shall import Curses library to our Python programs and initialize those. First open the terminal and issue 'vi curses.py' command to start the text editor and change to insert mode by pressing the 'I' key. This is how you write it (ignore the numbers):
1.import cu rses
2.stdscr - curses. initscr()
Now save the file and exit from the text editor by pressing 'esc' button. Then write ":wq" and press enter. Initialization of the terminal is necessary beŽcause when Curses starts, it gives you a completely new window to work on. And when you exit, ie stop Curses then everything should be brought back to the previous state. After it saves the terŽminal state it automatically calls the subroutine named 'setupterm' which sets up a new terminal, independent of the previous terminal for curses. already been created. To create a window, add the following line to your program:
1.s - stdscr.subwin(23, 79,0, 0)
2.s.box()
3.s.refresh()
In the above code, the first line specifies the coordinate on the screen where the rectangular box will be drawn. The parameters are height, weight and Y, X coordinates. Note that unlike other programming parameters, here the Y coordinate is specified beŽfore the X coordinate. Next is the box subroutine which draws a box in memŽory and finally refreshes it to make changes to the physical display device.
Pad is another class of windows which is not restricted by the size of the terminal or the physical display device. And only a part of the pad can be viewed at a given time,
Windowing with curses
A window in Curses can have any display size and even be a single character long. As in other programming lanŽguages variables are defined for each data type, similarly in Curses, variables are declared for each kind of window. Whenever a window is created, its structure is stored in the memory, specifically allocated for the newly creŽated window. So, whenever changes are done to a window, they get reflected in the memory and one has to explicitly use "refresh()" to update the physical screen display. Virtually, whenever you initialize Curses, a default window called "stdscr" is created, which has the same height and width as the terminal.
Apart from main windows, there are sub windows as well. These windows reside within a window that has ing a full size 1600xl600(say) resolution picture on the picture viewer. In case of a normal window you need to refresh the whole window but here only the portion which is visible, needs to be refreshed.
Pads are used where multiple winŽdow are created to do certain tasks, beŽcause when you do 'refresh()' for a particular window, the rest of the winŽdows flicker unnecessarily. Just as in windows we have a sub window, here we have subpads. Creating a pad is slightly different than creating a winŽdow and can be done through the following code:
1.pad - curses.newpad(100. 100)
2.for y in range(O, 100):
3.for x in range(O. 100):
4.pad.addch(y,x. ord('a') + (x'x+y'y) % 26)
5. pad.refresh( 0.0. 5,5, 20,75)
First the Pad's height and width is provided and then for displaying the pad, the on screen coordinate is proŽvided. So in the refresh section, the pad's coordinate (portion of the pad to be displayed) and the on-screen area are provided, i.e. starting yx coordinate and the ending yx coordinate.
Terminating Curses
After all the required work has been done, you need to end the Curses process and return to initial state. As discussed, the initial state is already saved. So to end Curses and to return to the initial state, write the following code at the end of your program: In this article we have covered the basics of Curses. More details shall be covered in subsequent articles



Reply With Quote
Bookmarks