views:

56

answers:

1

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.

+2  A: 

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 = -12

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)
ars
This works for setting Window Size. Can you also provide code for the Screen Buffer Size and Window Position. My knowledge on ctypes and the win32 API is zero.
LtPinback
@LtPinback: see the update above.
ars