Python Curses Handling Window (Terminal) Resize
python curses move cursor
python curses example
python curses border
python resize terminal
curses get window size
This is two questions really:
- how do I resize a curses window, and
- how do I deal with a terminal resize in curses?
Is it possible to know when a window has changed size?
I really can't find any good doc, not even covered on http://docs.python.org/library/curses.html
Terminal resize event will result in the
curses.KEY_RESIZE key code. Therefore you can handle terminal resize as part of a standard main loop in a curses program, waiting for input with
15.11. curses — Terminal handling for character-cell displays , Terminal resize event will result in the curses.KEY_RESIZE key code. Therefore you can handle terminal resize as part of a standard main loop in a curses� curses.resize_term (nlines, ncols) ¶ Backend function used by resizeterm(), performing most of the work; when resizing the windows, resize_term() blank-fills the areas that are extended. The calling application should fill in these areas with appropriate data. The resize_term() function attempts to resize all windows. However, due to the
I got my python program to re-size the terminal by doing a couple of things.
# Initialize the screen import curses screen = curses.initscr() # Check if screen was re-sized (True or False) resize = curses.is_term_resized(y, x) # Action in loop if resize is True: if resize is True: y, x = screen.getmaxyx() screen.clear() curses.resizeterm(y, x) screen.refresh()
As I'm writing my program I can see the usefulness of putting my screen into it's own class with all of these functions defined so all I have to do is call
Screen.resize() and it would take care of the rest.
Python Examples of curses.resizeterm, A panel stack extension that adds depth to curses windows. Tutorial material on using curses with Python, by Andrew Kuchling and Eric Raymond. Calling first raw() then cbreak() leaves the terminal in cbreak mode. Resize the standard and current windows to the specified dimensions, and adjusts� Resize the standard and current windows to the specified dimensions, and adjusts other bookkeeping data used by the curses library that record the window dimensions (in particular the SIGWINCH handler). curses.savetty ¶ Save the current state of the terminal modes in a buffer, usable by resetty(). curses.setsyx (y, x) ¶
I use the code from here.
In my curses-script I don't use getch(), so I can't react to
Therefore the script reacts to
SIGWINCH and within the handler re-inits the curses library. That means of course, you'll have to redraw everything, but I could not find a better solution.
Some example code:
from curses import initscr, endwin from signal import signal, SIGWINCH from time import sleep stdscr = initscr() def redraw_stdscreen(): rows, cols = stdscr.getmaxyx() stdscr.clear() stdscr.border() stdscr.hline(2, 1, '_', cols-2) stdscr.refresh() def resize_handler(signum, frame): endwin() # This could lead to crashes according to below comment stdscr.refresh() redraw_stdscreen() signal(SIGWINCH, resize_handler) initscr() try: redraw_stdscreen() while 1: # print stuff with curses sleep(1) except (KeyboardInterrupt, SystemExit): pass except Exception as e: pass endwin()
Python Examples of curses.KEY_RESIZE, def resize(self): """Handle terminal resizing""" # Check if screen was re-sized ( True or False) resize = self.max_x == 0 or \ curses.is_term_resized(self.max_y,� Curses terminal resize problems when Python is in interactive mode: curses SIGWINCH handling is still (2.7/3.2) broken after importing readline. or resize the
It isn't right. It's an
ncurses-only extension. The question asked about
curses. To do this in a standards-conforming way you need to trap
SIGWINCH yourself and arrange for the screen to be redrawn.
ncurses, KEY_RESIZE: # Special case for simulating a terminal resize event in curses. self . def event_listener(scr, timeout): ''' Wait for curses events on screen ``scr`` at� Starting from windows-curses 2.0, in the name of pragmatism, these wheels (but not Gohlke's) include a hack to make resizing work for applications developed against ncurses without Python code changes: Whenever getch(), getkey(), or get_wch() return KEY_RESIZE, resize_term(0, 0) is called automatically.
This worked for me when using curses.wrapper():
if stdscr.getch() == curses.KEY_RESIZE: curses.resizeterm(*stdscr.getmaxyx()) stdscr.clear() stdscr.refresh()
Python: curses and refresh on resize , I couldn't find out how to resize the terminal window using the curses library so instead I used subprocess to automatically resize the terminal� Windows are the basic abstraction in curses. A window object represents a rectangular area of the screen, and supports methods to display text, erase it, allow the user to input strings, and so forth. The stdscr object returned by the initscr() function is a window object that covers the entire screen. Many programs may need only this single
resizeterm() -- change the curses terminal size, And so I put the ncurses routine also in a separate thread. But on Expected behaviour: A resize of the terminal window repaints the display The following are code examples for showing how to use curses.resizeterm().They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.
python curses resize window Code Example, It provides callers with a hook into the ncurses data to resize windows, primarily for use by programs running in an X Window terminal (for example, xterm). NOTES. While this function is intended to be used to support a signal handler ( that is,� I agree with Chris's logic; the incorrect size seems to be a curses/ncurses bug that has gotten fixed somewhere along the line. You also aren't able to shrink the terminal to be smaller than the size of a derived window created with derwin(), but you seem to get an exception when you try rather than a crash. This seems reasonable to me.
Fix issues with running Python's curses module on Windows � Issue , Get code examples like "python curses resize window" instantly right from your google search results with the Grepper Chrome Extension. The curses package is a subroutine library for terminal-independent screen-painting and input-event handling which presents a high level screen model to the programmer, hiding differences between terminal types and doing automatic optimization of output to change one screen full of text into another.
- This is right, but only when ncurses is compiled with --enable-sigwinch. In particular, the libncurses in Debian and Ubuntu doesn't have that turned on; I'm not sure why.
- @thepaul: at least in my Ubuntu 12.04 I do get
curses.KEY_RESIZEcode when I resize the terminal emulator window.
- Good, I'm glad they fixed that.
- Could you add whatever is needed to make this code self-contained? For example I'm not sure what "screen" is.
- @DonHatch Hopefully that is what you are looking for. Here is a good little tutorial also if you are wanting it. docs.python.org/3/howto/curses.html
- He meant that your code doesn't work if you just copy and past to test it as there are many things missing.
- where do you take
resize = curses.is_term_resized(y, x)? is it old sizes? If so, how do you get the terminal size? for me,
screen.getmaxyx()doesn't seem to return the updated size. When I change the size of command line, it is still the same...
- Calling endwin/initscr in a signal handler is a good way to get core dumps.
- @ThomasDickey Thanks for the info. I've removed one initscr() from the signal-handler, what presumably doesn't improve it much. The c-code in an accepted answer, recommends to use
refresh(). Am I right, that python3 would inform about the user about the coredump? If yes, I hadn't have one up to now, while furiously resizing my terminal.
- This is talking about Python's curses library which actually wraps the ncurses library.