Python pyautogui window 句柄

Python pyautogui window handle

使用 pyautogui 是否有办法获取 window 的句柄,以便我可以确保仅对该 window 执行点击?换句话说,如果我的 window 不在焦点上,则不会发生点击。此外,如果我的 window 没有聚焦,那么我会聚焦它然后执行操作。

识别 window 的方法可以是 ID、window 标题等类似于 https://autohotkey.com/docs/commands/WinGet.htm

是否有其他 Python 模块支持这种功能?

Is there any other Python module that supports this kind of functionality?

https://github.com/pywinauto/pywinauto

https://pywinauto.readthedocs.io/en/latest/#some-similar-tools-for-comparison

PyAutoGui 本身在其文档 FAQ section 中说,

Q: Can PyAutoGUI figure out where windows are or which windows are visible? Can it focus, maximize, minimize windows? Can it read the window titles?

A: Unfortunately not, but these are the next features planned for PyAutoGUI. This functionality is being implemented in a Python package named PyGetWindow, which will be included in PyAutoGUI when complete.

现在,如果您使用此指针继续 PyGetWindow's repo, you'll see there's no code there yet, but there is a random_notes.txt 文件:

Finding window titles on Windows:

其中有一些有趣的信息。 (我还没试过。)

此代码可能有助于获得 window 您想要最小化或最大化的内容。示例:如果您想获得标题为“Stack Overflow”的 Chrome window,

    pyautogui.getWindowsWithTitle("Stack Overflow")[0].minimize()

或者,如果您想最小化或最大化任何标题为“音乐”的文件浏览器window,同样适用。

    pyautogui.getWindowsWithTitle("music")[0].maximize()

如果您不确定需要哪个 window,您可以获取列表 using this