Mei Zhang
Mei Zhang

Reputation: 1714

get updated screen size in 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?

Upvotes: 7

Views: 10417

Answers (1)

yewang
yewang

Reputation: 625

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

Upvotes: 12

Related Questions