8

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

Fourier
  • 435
  • 2
  • 5
  • 10
  • This may help you: [How can I change the width of a Windows console window?](http://stackoverflow.com/questions/190543/how-can-i-change-the-width-of-a-windows-console-window) – A.M. D. Nov 01 '12 at 10:36

3 Answers3

16

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)
netotz
  • 193
  • 1
  • 4
  • 12
Johan Conradie
  • 161
  • 1
  • 3
8

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"])
Malte Bublitz
  • 236
  • 1
  • 3
  • 2
    Ok 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 Nov 01 '12 at 16:02
  • 7
    Ok got it working by myself ;) I just used os.system("mode con: cols=25 lines=80") – Fourier Nov 01 '12 at 16:21
  • 1
    LOL, `mode` is a cmd command, not a executable, `subprocess.Popen(["mode", "con:", "cols=25", "lines=80"])` definitely does not work. – Meow Jun 18 '16 at 02:04
  • @Meow There's nothing to "LOL" at. Everyone makes mistakes. You could instead request an edit to the comment... – Tigran Jun 25 '22 at 20:27
1

Kudo's to Malte Bublitz for already explaining why this works (Python 3+):

os.system(f'mode con: cols={cols} lines={lines}')
knw
  • 23
  • 3
  • Not system independent though, Windows only, for MacOS see: https://stackoverflow.com/questions/59356799/how-to-change-terminal-window-size-on-mac-with-command – knw Apr 07 '22 at 15:14