|
C# Ncurses
Make curses available on Windows by using PDCurses library. Alternative patch for issue 1005895 using current trunk. Attached.bat file is used to compile it under MinGW, but I'd be glad to know how to integrate the patch into Python build system. PDCurses includes support for mouse functions compatible with ncurses, but to turn it on.
What is curses?¶
- The curses package is part of the Python standard library and is useful for creating text-based user interfaces and generally controlling the screen and keyboard input. The big problem is that it doesn't work out-of-the-box on Windows. This show you how to get curses working in Windows. This was tested in Windows 10 with Python 3.6.
- Python’s support adds a text-input widget that makes up some of the lack. The most common way to get input to a window is to use its getch method. Getch pauses and waits for the user to hit a key, displaying it if echo has been called earlier. You can optionally specify a coordinate to which the cursor should be moved before pausing.
The curses library supplies a terminal-independent screen-painting andkeyboard-handling facility for text-based terminals; such terminalsinclude VT100s, the Linux console, and the simulated terminal providedby various programs. Display terminals support various control codesto perform common operations such as moving the cursor, scrolling thescreen, and erasing areas. Different terminals use widely differingcodes, and often have their own minor quirks.
In a world of graphical displays, one might ask “why bother”? It’strue that character-cell display terminals are an obsolete technology,but there are niches in which being able to do fancy things with themare still valuable. One niche is on small-footprint or embeddedUnixes that don’t run an X server. Another is tools such as OSinstallers and kernel configurators that may have to run before anygraphical support is available.
The curses library provides fairly basic functionality, providing theprogrammer with an abstraction of a display containing multiplenon-overlapping windows of text. The contents of a window can bechanged in various ways—adding text, erasing it, changing itsappearance—and the curses library will figure out what control codesneed to be sent to the terminal to produce the right output. cursesdoesn’t provide many user-interface concepts such as buttons, checkboxes,or dialogs; if you need such features, consider a user interface library such asUrwid.
The curses library was originally written for BSD Unix; the later System Vversions of Unix from AT&T added many enhancements and new functions. BSD cursesis no longer maintained, having been replaced by ncurses, which is anopen-source implementation of the AT&T interface. If you’re using anopen-source Unix such as Linux or FreeBSD, your system almost certainly usesncurses. Since most current commercial Unix versions are based on System Vcode, all the functions described here will probably be available. The olderversions of curses carried by some proprietary Unixes may not supporteverything, though.
The Windows version of Python doesn’t include the curses
module. A ported version called UniCurses is available. You couldalso try the Console modulewritten by Fredrik Lundh, which doesn’tuse the same API as curses but provides cursor-addressable text outputand full support for mouse and keyboard input.
The Python curses module¶
The Python module is a fairly simple wrapper over the C functions provided bycurses; if you’re already familiar with curses programming in C, it’s reallyeasy to transfer that knowledge to Python. The biggest difference is that thePython interface makes things simpler by merging different C functions such asaddstr()
, mvaddstr()
, and mvwaddstr()
into a singleaddstr()
method. You’ll see this covered in moredetail later.
Ncurses Windows 10
This HOWTO is an introduction to writing text-mode programs with cursesand Python. It doesn’t attempt to be a complete guide to the curses API; forthat, see the Python library guide’s section on ncurses, and the C manual pagesfor ncurses. It will, however, give you the basic ideas.