A class of Python applications is best to use an interactive user interface, which eliminates the overhead or complexity of the graphics environment. Interactive text-mode programs (in Linux/unix), such as ncurses libraries encapsulated in the standard curses modules of Python, are exactly what you need. In this article, Davidmertz discusses the use of curses in Python. He describes the curses environment using the sample source code from the front end to the txt2html program.
The Curses Library (ncurses) provides a terminal-independent approach to controlling character screens. Curses is a standard part of most UNIX-like systems, including Linux, and it has been ported to Windows and other systems. The curses program will run on a plain text system, xterm, and other windowing console sessions, making these applications portable.
Introduction Curses
Python's standard curses provides the basic interface for the common features of "glass Telex" (Glass teletype) (in the 1970s, when the original curses library was first created, it was called a CRT). There are many ways to make interactive text-mode programs written in Python more ingenious. These methods fall into two categories.
On the one hand, some Python modules support a ncurses (curses superset) or a complete set of functions for slang (similar but independent console libraries). Most notably, there is an enhanced library (encapsulated by the appropriate Python module) that allows you to add colors to the interface.
On the other hand, many of the advanced widget libraries built on curses (or Ncurses/slang) add features such as buttons, menus, scroll bars, and various common interface devices. If you've seen applications developed with libraries such as Borland ' s Turbowindows (DOS edition), you know how appealing these features are in the text-mode console. The widgets in the widget library can be achieved by using only curses, but can also take advantage of the results of other programmers at the advanced interface. See Resources for a link to the referenced module.
This article only deals with the characteristics of curses itself. Since the curses module is part of a standard release, you can find and use it (at least in Linux or UNIX systems) without downloading the support library or other Python modules. Understanding the basic support provided by curses is useful, even if only as a basis for understanding advanced modules. Even without the use of other modules, it is easy to build beautiful and useful Python text-mode applications using curses alone. A pre-release note mentions that Python 2.0 will include an enhanced version of curses, but in any case it should be compatible with the version described here.
Application
I will discuss the wrappers written for txt2html (the text to the HTML converter described in "cute Python: My first web-based Filter agent") as a test application for this article. There are several ways to run txt2html. But in keeping with the purpose of this article, we'll look at the txt2html that runs from the command line. One way to manipulate txt2html is to provide it with a set of command-line variables that describe all aspects of the transformation to be performed, and then run the application as a batch. For occasionally used users, a friendlier user interface provides an interactive selection screen that enables the user to traverse the conversion option (providing visual feedback for the selected option) before performing the actual conversion.
The Curses_txt2html interface is based on the common top bar menu, which comes with a drop-down and nested submenu. All menu-related features are designed "from the beginning" on the curses. Although these menus lack some of the features of the more complex curses wrappers, their basic functionality is implemented by just a few lines of code using only curses. The interface also comes with a simple scrolling help box and several user input fields. The following is a screenshot of the application that shows the general layout and style.
The application on the X terminal
Applications on Linux Terminals