3

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
Tom
  • 571
  • 2
  • 11
  • 29
  • 3
    Possible duplicate of [How to control the size of the Windows shell window from within a python script?](https://stackoverflow.com/questions/3646362/how-to-control-the-size-of-the-windows-shell-window-from-within-a-python-script) – kay Jul 20 '18 at 23:13

2 Answers2

4

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

This works:

import os
os.system('mode con lines=25 cols=134')
M--
  • 25,431
  • 8
  • 61
  • 93
etxnight
  • 1
  • 1
  • etxnight, please write your answer in English, as Stack Overflow is an [English-only site](//meta.stackoverflow.com/a/297680). (This translates from Indonesian into meaninglessness..) – Yunnosch Sep 02 '23 at 04:37