How to get screen size through python curses
26,446
Apparently, the getmaxyx() method is what you need. It returns a (height, width) tuple.
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()
Related videos on Youtube
Author by
daubers
Updated on September 18, 2022Comments
-
daubers almost 2 years
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 :)
-
coversnail about 12 yearsWhilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
-
ArtOfWarfare over 9 yearsWould expect to find this on SO, but nontheless, perfect answer, thanks! Signed up just to +1 this. :)
-
RandomInsano over 7 yearsFunny, the tutorial link is now dead.
-
Timo over 7 years@RandomInsano edited my answer. I think it was just some random tutorial I found through Google.