如何使用Python获取和设置窗口(任何Windows程序)的位置和大小?
pywin32
的win32gui
模块及其EnumWindows
和GetWindowRect
函数。appscript
。import win32gui
def callback(hwnd, extra):
rect = win32gui.GetWindowRect(hwnd)
x = rect[0]
y = rect[1]
w = rect[2] - x
h = rect[3] - y
print("Window %s:" % win32gui.GetWindowText(hwnd))
print("\tLocation: (%d, %d)" % (x, y))
print("\t Size: (%d, %d)" % (w, h))
def main():
win32gui.EnumWindows(callback, None)
if __name__ == '__main__':
main()
GetWindowRect
函数获取窗口坐标。为此,您需要窗口的句柄,可以使用 FindWindow
获取,假设您已经知道有关该窗口的一些信息(例如窗口标题)。pywin32
。如Greg Hewgill所说,如果您知道窗口的名称,可以直接使用win32gui的FindWindow和GetWindowRect。这可能比以前的方法更加清晰和高效。
from win32gui import FindWindow, GetWindowRect
# FindWindow takes the Window Class name (can be None if unknown), and the window's display text.
window_handle = FindWindow(None, "Diablo II")
window_rect = GetWindowRect(window_handle)
print(window_rect)
#(0, 0, 800, 600)
供将来参考:PyWin32GUI现已迁移到Github
(-32000,-32000,-31840,-31972)
。 - David这可以从窗口标题返回窗口矩形
def GetWindowRectFromName(name:str)-> tuple:
hwnd = ctypes.windll.user32.FindWindowW(0, name)
rect = ctypes.wintypes.RECT()
ctypes.windll.user32.GetWindowRect(hwnd, ctypes.pointer(rect))
# print(hwnd)
# print(rect)
return (rect.left, rect.top, rect.right, rect.bottom)
if __name__ == "__main__":
print(GetWindowRectFromName('CALC'))
pass
使用conda-forge打包的Python 3.8.2版本,(默认版本为Apr 24 2020, 07:34:03),在Windows 10 Pro 1909(64位)操作系统上运行。
ctypes.windll.user32
和ctypes.wintypes
。 - ShortArrowsudo apt-get install xdotool xprop xwininfo
git clone https://github.com/Pithikos/winlaunch.git && cd winlaunch
>>> from winlaunch import *
>>> wid, pid = launch('firefox')
>>> win_pos(wid)
[3210, 726]
wid
和pid
分别代表窗口ID和进程ID。
这段代码适用于Windows系统。它返回当前活动窗口的位置和大小。
from win32gui import GetWindowText, GetForegroundWindow
print(GetWindowRect(GetForegroundWindow()))
win32gui.GetWindowRect
和ctypes.windll.user32.GetWindowRect
所接口的内容。dwmapi
提取文章中提到的 DWMWA_EXTENDED_FRAME_BOUNDS
关于使用 dwmapi.DwmGetWindowAttribute
(参见 这里):DWMWA_EXTENDED_FRAME_BOUNDS
在第9个位置上。import ctypes
from ctypes.wintypes import HWND, DWORD, RECT
dwmapi = ctypes.WinDLL("dwmapi")
hwnd = 133116 # refer to the other answers on how to find the hwnd of your window
rect = RECT()
DMWA_EXTENDED_FRAME_BOUNDS = 9
dwmapi.DwmGetWindowAttribute(HWND(hwnd), DWORD(DMWA_EXTENDED_FRAME_BOUNDS),
ctypes.byref(rect), ctypes.sizeof(rect))
print(rect.left, rect.top, rect.right, rect.bottom)
对于我在Mac/OSX上,这些解决方案都没有起作用。目前,pygetwindow
的大多数功能在OSX上尚未实现(请参见pygetwindow/_pygetwindow_macos.py
on Github)。
使用已实现的少数功能,以下代码将按名称列出所有窗口,然后获取每个窗口的几何形状,格式为(左上角X坐标,左上角Y坐标,宽度,高度):
import pygetwindow as gw # pip3 install pygetwindow
windows = gw.getAllTitles()
for win in windows:
geometry = gw.getWindowGeometry(win)
print(f'Window title: {win}')
print(f'> top-left X coordinate: {geometry[0]}')
print(f'> top-left Y coordinate: {geometry[1]}')
print(f'> width: {geometry[2]}')
print(f'> height: {geometry[3]}\n')
示例输出:
Window title: iTerm2 IPython: ~/
> top-left X coordinate: 597.0
> top-left Y coordinate: 178.0
> width: 843.0
> height: 722.0
Window title: Google Chrome Get window position & size with python - Stack Overflow
> top-left X coordinate: 0.0
> top-left Y coordinate: 25.0
> width: 815.0
> height: 875.0
Window title: Finder Macintosh HD
> top-left X coordinate: 260.0
> top-left Y coordinate: 135.0
> width: 920.0
> height: 436.0
pygetwindow
- gw.getActiveWindow()
- gw.getAllTitles()
- gw.getWindowsAt()
- gw.getWindowGeometry(title)
Python 3.11.6
PyGetWindow 0.0.9
OSX Ventura 13.5