You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从pyautogui.getActiveWindow()获取单个变量及Win32Window对象属性

解决Win32Window对象属性获取问题

报错原因

pyautogui.getActiveWindow()返回的是单个Win32Window对象,而非列表(getWindowsWithTitle()返回的是匹配窗口的列表),所以你尝试用hwnd2[0]的下标访问方式会触发TypeError。

正确获取属性的方法

直接通过对象.属性名的方式访问即可,和你访问hwnd[0]的属性完全一致,不需要加下标:

# 直接访问hwnd2的属性
print(hwnd2)  # 查看该窗口的所有属性信息
print(hwnd2.top)
print(hwnd2.left)
print(hwnd2.width)
print(hwnd2.height)
print(hwnd2.title)
print(hwnd2.hWnd)  # 窗口句柄

执行上述代码就能正常获取活跃窗口的各项属性了。

内容的提问来源于stack exchange,提问作者jungar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 01:27:42