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.
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.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.Learn enough tkinter to run your program. Clearing the screen is then
text.delete('1.0', 'end-1c')
.
Related Topics
Render HTML to Pdf in Django Site
Python: How to Kill Child Process(Es) When Parent Dies
Simulating Key Press Event Using Python For Linux
How to Remove Items from a List While Iterating
How to Pandas Group-By to Get Sum
How to Provide a Reproducible Copy of Your Dataframe With To_Clipboard()
How to Flush the Output of the Print Function
Could Not Open Resource File, Pygame Error: "Filenotfounderror: No Such File or Directory."
How to Remove Accents (Normalize) in a Python Unicode String
Why Do People Write #!/Usr/Bin/Env Python on the First Line of a Python Script
Remove HTML Tags Not on an Allowed List from a Python String
Understanding Python Subprocess.Check_Output'S First Argument and Shell=True
Is There a Difference Between "==" and "Is"
How to Keep Keys/Values in Same Order as Declared
Running Bash Commands in Python
Import Multiple CSV Files into Pandas and Concatenate into One Dataframe