Set Console Width (Windows, Python)
18,425
Solution 1
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"])
Solution 2
a) To check the size of the Terminal Window
import os
x = os.get_terminal_size().lines
y = os.get_terminal_size().columns
print(x)
print(y)
b) To change the size of the Terminal Window
import os
cmd = 'mode 50,20'
os.system(cmd)
c) To change the color of the Terminal Window
import os
cmd = 'color 5E'
os.system(cmd)
Comments
-
Fourier almost 2 years
I am working on a little text based console game using Python. Because I am using some ASCII art I have to ensure that the width of the console is the same for everyone after launch of my game. Can anyone tell me how to set the console width and height ? :)
Greetz
Flo
-
A.M. D. over 11 yearsThis may help you: How can I change the width of a Windows console window?
-
-
Fourier over 11 yearsOk I imported subprocess but it still does not work :c i get a "FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden" error -.-
-
Fourier over 11 yearsOk got it working by myself ;) I just used os.system("mode con: cols=25 lines=80")
-
Meow almost 8 yearsLOL,
mode
is a cmd command, not a executable,subprocess.Popen(["mode", "con:", "cols=25", "lines=80"])
definitely does not work. -
aschultz over 5 yearsThis answered my next question! A follow-up is that it is for 3.3 or higher, so Python 2 users need to import future.
-
Daniel almost 4 yearsHow can I reset the size?
-
Levon almost 3 yearsThis is super - but I guess as part of the functionality of the mode command the scroll bar disappears :-/
-
knw about 2 yearsNot system independent though, Windows only, for MacOS see: stackoverflow.com/questions/59356799/…