Python Ncurses Windows

P: n/a
Chris Share <usenet at caesium.me.ukwrote:
I've been writing an application using curses, and have been trying to
handle resizing the terminal, if running in xterm or similar. Increasing
the window size is working perfectly, however shrinking it is not
working at all. No matter how much I shrink the window, the size
returned by getmaxyx() does not change. However as soon as I increase it
again, it works fine.
I've tracked the problem down to the fact I have created a window
derived from stdscr. A small script showing the effect is at the end of
this post.

odd (thanks for the example - I don't know if this is a problem in
ncurses or in the python interface to it, but will check/see).
Can anyone suggest how to solve this problem, that doesn't involve not
making a derwin of stdscr?
I've been googling for hours, but found nothing to help.
Python version is 2.3.4 on debian testing.

probably should report it as a bug (so it's not overlooked).
--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
http://web.cs.mun.ca/~rod/ncurses/ncurses.html#xterm says 'The ncurses
library does not catch [the SIGWINCH aka resizing] signal, because it
cannot in general know how you want the screen re-painted'. First, is
this really true? When I make my xterms smaller they clip what is
displayed--is that a function of curses or the xterm?
Second, if true, it explains /what/ is going on--stdscr, only--, but
isn't really satisfactory; it doesn't solve the original poster's bug.
#!/usr/bin/env python
''
curses_resize.py -run the program without hooking SIGWINCH
curses_resize.py 1 -run the program with hooking SIGWINCH
''
import sys, curses, signal, time
def sigwinch_handler(n, frame):
curses.endwin()
curses.initscr()
def main(stdscr):
''just repeatedly redraw a long string to reveal the window boundaries''
while 1:
stdscr.insstr(0,0,'abcd'*40)
time.sleep(1)
if __name__'__main__':
if len(sys.argv)2 and sys.argv[1]'1':
signal.signal(signal.SIGWINCH, sigwinch_handler)
curses.wrapper(main)
If you run this without sigwinch then the line never gets resized, but
if you do then it works fine. What we can glean from this is that
stdscr only reads off it's size upon initialization. This behaviour
may seem a bit strange, but 1) it's legacy and 2) avoids breaking the
semantics of windows (which don't change size on their own).
The 'curses.initscr()' part is kind of unhappy though. It modifies the
stdscr variable without us explicitly assigning anything (but I can't
think of any other way to do it, beyond making stdscr a global, and
that feels worse) and will break if initscr() ever returns a new
Window structure instead of just updating and returning the old one.
Does anyone have any tips for how to structure this so that the screen
can actually be assigned to?
In conclusion, it's not a bug, it's a feature. Joy! The workaround is
to write a sigwinch_handler that at least does `endwin(); initscr()`.
Sorry for reviving an old thread, but it doesn't seem the issue was
ever resolved (the thread doesn't go anywhere and there's no warning
about this in the docs that I noticed).
(please CC: me if anyone cares, I'm not on the list)
-Nick Guenther

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.

Windows

What is curses?¶

  1. 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.
  2. 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.
Python Ncurses Windows

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 cursesmodule. 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.