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 :)
Asked
Active
Viewed 2.8k times
1 Answers
12
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()
Timo
- 4,670
- 1
- 16
- 24
-
2Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – coversnail May 08 '12 at 08:07
-
1Would expect to find this on SO, but nontheless, perfect answer, thanks! Signed up just to +1 this. :) – ArtOfWarfare Feb 09 '15 at 20:51
-
Funny, the tutorial link is now dead. – RandomInsano Feb 05 '17 at 17:05
-
1@RandomInsano edited my answer. I think it was just some random tutorial I found through Google. – Timo Feb 06 '17 at 09:56