在Python中更改cmd提示大小



我正在用Python制作一个基于文本的CMD应用程序。

我希望它能将命令提示符调整为更方形的、基于文本的冒险形式。

所以我发现了这个:

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

然而,这是决定性的,一旦设置,用户就不能再在CMD上滚动或调整应用程序的大小。

因此,用户不可能阅读任何长文本。有没有办法在cmd中添加自动滚动,或者在不使其不可用的情况下调整cmd的大小?

使用win32 API,您可以避免丢失滚动:

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

这是有效的:

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

最新更新