0

Is there a way within Python, perhaps using sys and os, to determine the size of the window in which you are running Python. I can do this in emacs using keyboard to run an emacs function, but not in general.

I did not work too hard at it, but the other answers (for me) failed in Windows using Anaconda3 Python. Perhaps someone else could try them in an emacs environment, which is where I tried them. They do return answers that appear to be defaults because they do not match my window size.

Clifford
  • 11
  • 3
  • https://stackoverflow.com/questions/566746/how-to-get-linux-console-window-width-in-python – Daniel Aug 29 '18 at 15:04
  • Possible duplicate of [How to get Linux console window width in Python](https://stackoverflow.com/questions/566746/how-to-get-linux-console-window-width-in-python) – Daniel Aug 29 '18 at 15:05

2 Answers2

1

You can use os.get_terminal_size:

import os
os.get_terminal_size()
#os.terminal_size(columns=80, lines=24)

and this class returned supports unpacking:

columns, lines = os.get_terminal_size()
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
0

If you are running Python in a graphics window, at least on Windows, there is, in general, no way to get the window size from within the Python program. If, for example, one starts IDLE from a Windows console, shutil.get_window_size() reports the size of the parent console (rows and columns), not the size of the tk text widget that IDLE's shell is running in. If one starts IDLE otherwise, from an icon or File Explorer, one gets the default 24 x 80.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52