How to get screen size through python curses

I'm writing a terminal program and need to get the size of the TTY I'm using. The Internet seems to suggest this can be done with curses, but I can't seem to find out how in python. I'm trying to avoid using the ROWS and COLUMNS environment variables if possible :)


Apparently, the getmaxyx() method is what you need. It returns a (height, width) tuple.

Python reference

Some tutorial

Edit: tutorial link is dead, here's the archived version. And here's the example from that link:

Now lets say we want to draw X's down the diagonal. We will need to know when to stop, so we will need to know the width and the height of the screen. This is done with the stdscr.getmaxyx() function. This function returns a tuple with the (height, width) values.

#!/usr/bin/python

import curses
import time
stdscr = curses.initscr()
curses.cbreak()
curses.noecho()
stdscr.keypad(1)

try:
# Run your code here
    height,width = stdscr.getmaxyx()
    num = min(height,width)
    for x in range(num):
        stdscr.addch(x,x,'X')
    stdscr.refresh()
    time.sleep(3)
finally:
    curses.nocbreak()
    stdscr.keypad(0)
    curses.echo()
    curses.endwin()