I'm using the curses library in python and the only way I know to get the dimensions of the screen is with curses.LINES
and curses.COLS
. However, those values never get updated, even when a "KEY_RESIZE"
key is read, like in the following example:
import curses
f = open("out.log", "w")
def log(msg):
f.write(msg)
f.flush()
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
while True:
stdscr.clear()
stdscr.refresh()
key = stdscr.getkey()
log(key)
if key == "KEY_RESIZE":
log("{} {}".format(curses.LINES, curses.COLS))
if key == "q":
break
stdscr.keypad(False)
curses.nocbreak()
curses.echo()
curses.endwin()
f.close()
In my output file out.log
, I can see that when I resize the curses window, it correctly writes KEY_RESIZE
y, but the value of curses.LINES
and curses.COLS
doesn't get updated. What I am missing?
To clear characters until the end of the line, use clrtoeol(), To clear characters until the end of the window, use clrtobot().
The curses library supplies a terminal-independent screen-painting and keyboard-handling facility for text-based terminals; such terminals include VT100s, the Linux console, and the simulated terminal provided by various programs.
The Windows version of Python doesn't include the curses module. A ported version called UniCurses is available. You could also try the Console module written by Fredrik Lundh, which doesn't use the same API as curses but provides cursor-addressable text output and full support for mouse and keyboard input.
Use rows, cols = stdscr.getmaxyx()
instead of curses.LINES
and curses.COLS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With