问题
Using pyautogui is there a way to get a handle to a window so that I can ensure that a click is performed on that window only? In other words, if my window isn't in focus, then the click does not occur. Additionally, if my window isn't in focus then I bring it into focus and then perform the actions.
The way to identify a window could be an ID, window title etc similar to this https://autohotkey.com/docs/commands/WinGet.htm
Is there any other Python module that supports this kind of functionality?
回答1:
Is there any other Python module that supports this kind of functionality?
https://pywinauto.github.io
https://pywinauto.readthedocs.io/en/latest/#some-similar-tools-for-comparison
回答2:
PyAutoGui itself says, in its documentation's 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.
Now, if you go on over to PyGetWindow's repo, you'll see there's no code there yet, but there is a random_notes.txt file, with this pointer:
Finding window titles on Windows:
http://stackoverflow.com/questions/37501191/how-to-get-windows-window-names-with-ctypes-in-python
which has some interesting information. (I haven't tried it yet.)
回答3:
This code might help to get what window you want to minimize or maximize. Example: If you want to get a Chrome window titled "Stack Overflow",
pyautogui.getWindowsWithTitle("Stack Overflow")[0].minimize()
Or if you want to minimize or maximize any file explorer window that titled "music", the same thing applies.
pyautogui.getWindowsWithTitle("music")[0].maximize()
If you are not sure about which window you require, you can get a list using this
来源:https://stackoverflow.com/questions/43785927/python-pyautogui-window-handle