How to Get Linux Console Window Width in Python

How to get Linux console window width in Python

import os
rows, columns = os.popen('stty size', 'r').read().split()

uses the 'stty size' command which according to a thread on the python mailing list is reasonably universal on linux. It opens the 'stty size' command as a file, 'reads' from it, and uses a simple string split to separate the coordinates.

Unlike the os.environ["COLUMNS"] value (which I can't access in spite of using bash as my standard shell) the data will also be up-to-date whereas I believe the os.environ["COLUMNS"] value would only be valid for the time of the launch of the python interpreter (suppose the user resized the window since then).

(See answer by @GringoSuave on how to do this on python 3.3+)

How to get Linux console window width in Python

import os
rows, columns = os.popen('stty size', 'r').read().split()

uses the 'stty size' command which according to a thread on the python mailing list is reasonably universal on linux. It opens the 'stty size' command as a file, 'reads' from it, and uses a simple string split to separate the coordinates.

Unlike the os.environ["COLUMNS"] value (which I can't access in spite of using bash as my standard shell) the data will also be up-to-date whereas I believe the os.environ["COLUMNS"] value would only be valid for the time of the launch of the python interpreter (suppose the user resized the window since then).

(See answer by @GringoSuave on how to do this on python 3.3+)

Here's a way to get window size in python

The best way I can think of to accomplish this is in the following way.
Just think about it: we don't really care about the DPI or the resolution; we just want to adjust our output to our terminal window size. Precisely, the columns or width.

#!/usr/bin/python

import os

size = int(os.popen('tput cols').read().strip())
print('#' * size)
print('Welcome to my App').center(size))
print('#' * size,'\n\n')
i = input('What's your name? ')

We are using popen() to run a shell command 'tput' to get the columns('cols') of our window, getting the answer with read(), striping the trailing new line with strip(), converting it to an integer and then saving it to the variable size.

Next we are printing pound signs(#) based on the number of columns (size)

Next we are using center() to align out text to the middle of the window(size).

The rest I think is self explanatory we just added 2 new lines to the end of our bottom pound signs(#)

I hope this clears some things up for anyone that hasn't figured out how to ask the right question.

Links:
popen(), read(), strip(), center(), tput

How do I find the width & height of a terminal window?

  • tput cols tells you the number of columns.
  • tput lines tells you the number of rows.

Set Console Width (Windows, Python)

The easiest way is to execute the mode command.

e.g. for a 80x25 window:

C:\> mode con: cols=25 lines=80

Or in Python:

subprocess.Popen(["mode", "con:", "cols=25", "lines=80"])

How do I get the terminal height (in lines) in Python IDLE Shell?

You problem is twofold. 1. IDLE was designed, 20 years ago, for developing programs, not for users running them. 2. Where it matters, os and shutil were designed to work with actual text terminals, with a fixed number of lines and columns, or with programs than imitate such. They were were not designed to interact with GUI frameworks. There are separate modules for that.

What you could do.

  1. Have users run your program on the system terminal (the usual default). For development, print something like "\n**clear screen**\n" to signal you, the developer, that the screen would be normally be cleared.

  2. In the IDLE branch, print('\n'*N), where N is, say, 75, which should usually be enough. Or use a smaller number and inform users that you program assumes the window is at most N lines.

  3. Learn enough tkinter to run your program. Clearing the screen is then text.delete('1.0', 'end-1c').



Related Topics



Leave a reply



Submit