pythonpython-3.xcmdcommand-line-interfacepython-cmd

Change cmd prompt size in Python


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?


Solution

  • 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))