How to control the size of the Windows shell window from within a python script?
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.
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)
精彩评论