Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get updated screen size in python curses

Tags:

python

curses

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_RESIZEy, but the value of curses.LINES and curses.COLS doesn't get updated. What I am missing?

like image 524
Mei Zhang Avatar asked Oct 27 '18 07:10

Mei Zhang


People also ask

How do you clear the screen curse in Python?

To clear characters until the end of the line, use clrtoeol(), To clear characters until the end of the window, use clrtobot().

What is the curses function in Python?

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.

Does Python curses work on Windows?

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.


1 Answers

Use rows, cols = stdscr.getmaxyx() instead of curses.LINES and curses.COLS

like image 146
yewang Avatar answered Sep 19 '22 23:09

yewang