27

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?

kronosjt
  • 703
  • 4
  • 10
  • 24

3 Answers3

20

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

Raunak Thomas
  • 1,393
  • 1
  • 12
  • 28
Vasanth Prabakar
  • 431
  • 1
  • 4
  • 9
14

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.)

Tom Hundt
  • 1,694
  • 19
  • 18
  • 1
    The mentioned entry in FAQ is't there anymore and https://github.com/asweigart/pyautogui/blob/master/CHANGES.txt says: 'v0.9.43, 2019/05/27 -- Renamed getFocusedWindow to getActiveWindow to keep it up to date with pygetwindow.' – Claudio Feb 22 '21 at 13:28
9

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 lists as other similar Python tools:

  • PyAutoGui - a popular cross-platform library (has image-based search, no text-based controls manipulation).
  • Lackey - a pure Python replacement for Sikuli (based on image pattern matching).
  • AXUI - one of the wrappers around MS UI Automation API. winGuiAuto - another module using Win32 API.
Claudio
  • 7,474
  • 3
  • 18
  • 48