简体   繁体   English

如何从python脚本中控制Windows shell窗口的大小?

[英]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. 从Windows启动脚本类型的python文件时,会得到一个运行脚本的Windows shell类型窗口。 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. 我怀疑这可以用pywin32模块完成,但我找不到如何。

You can do this using the SetConsoleWindowInfo function from the win32 API. 您可以使用win32 API中的SetConsoleWindowInfo函数执行此操作。 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. 窗口位置基本上是上面的rect变量通过left, top, right, bottom参数设置的。 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: 要将屏幕缓冲区大小设置为100行乘80列,可以使用SetConsoleScreenBufferSize API:

bufsize = wintypes._COORD(100, 80) # rows, columns
windll.kernel32.SetConsoleScreenBufferSize(h, bufsize)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM