Reputation: 14412
This is two questions really:
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
Upvotes: 35
Views: 34277
Reputation: 1273
This worked for me when using curses.wrapper():
if stdscr.getch() == curses.KEY_RESIZE:
curses.resizeterm(*stdscr.getmaxyx())
stdscr.clear()
stdscr.refresh()
Upvotes: 2
Reputation: 746
I use the code from here.
In my curses-script I don't use getch(), so I can't react to KEY_RESIZE
.
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()
Upvotes: 5
Reputation: 780
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.
Upvotes: 10
Reputation: 7
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.
Upvotes: -2
Reputation: 1112
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 getch
.
Upvotes: 36