How to control the size of the Windows shell window from within a python script?

烈酒焚心 提交于 2019-11-28 12:45:36

You can do this using the SetConsoleWindowInfo function from the win32 API. The following should work:

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

UPDATE:

The window position is basically what the rect variable above sets through the left, top, right, bottom arguments. The actual size is derived from these arguments:

width = right - left + 1
height = bottom - top + 1

To set the screen buffer size to, say, 100 rows by 80 columns, you can use the SetConsoleScreenBufferSize API:

bufsize = wintypes._COORD(100, 80) # rows, columns
windll.kernel32.SetConsoleScreenBufferSize(h, bufsize)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!