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

无人久伴 提交于 2019-11-27 07:15:14

问题


When launching a script-type python file from Windows you get a windows shell type window where the script runs. How can the script determine and also set/control the Window Size, Screen Buffer Size and Window Position of said window?. I suspect this can be done with the pywin32 module but I can't find how.


回答1:


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)


来源:https://stackoverflow.com/questions/3646362/how-to-control-the-size-of-the-windows-shell-window-from-within-a-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!