Change cmd prompt size in Python

3.1k Views Asked by At

I am making a text-based CMD application in Python.

And I'd like it to resize the Command prompt to a more square, Text-based adventure-like, form.

So I found this:

os.system("mode con cols=93 lines=45")

However that's definitive and once set the user can no longer scroll on the CMD or resize the app.

So it makes it impossible for the user to read any bit of long text. Is there anyway to add an auto-scroll to the cmd or to resize the cmd without making it unusable?

2

There are 2 best solutions below

0
Keyan Kazemian On BEST ANSWER

Using the win32 API, you can avoid losing the scroll:

from ctypes import windll, byref
from ctypes.wintypes import SMALL_RECT

STDOUT = -11

hdl = windll.kernel32.GetStdHandle(STDOUT)
rect = wintypes.SMALL_RECT(0, 50, 50, 80) # (left, top, right, bottom)
windll.kernel32.SetConsoleWindowInfo(hdl, True, byref(rect))
1
etxnight On

This works:

import os
os.system('mode con lines=25 cols=134')