29

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?

5 Answers 5

21

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

4
  • 8
    I should note that these functions are undocumented and the API might change in a future version of PyAutoGUI. Commented Sep 27, 2018 at 20:51
  • 16
    As of 2019, module 'pyautogui' has no attribute 'getWindow'
    – Louie Lee
    Commented Jan 1, 2020 at 4:42
  • 6
    Now it's called pyautogui.getWindowsWithTitle
    – Charleston
    Commented Aug 19, 2020 at 22:04
  • I got error > AttributeError: module 'pyautogui' has no attribute 'getWindowsWithTitle'
    – Nam G VU
    Commented Aug 13, 2023 at 8:35
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.)

1
  • 1
    The mentioned entry in FAQ is't there anymore and 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.'
    – user7711283
    Commented Feb 22, 2021 at 13:28
8

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.
2
0

In order to use pyautogui on app pages that are already maximized but not in focus First Minimize ten Maximize:

pyautogui.getWindowsWithTitle("FileZilla")[0].minimize()
pyautogui.getWindowsWithTitle("FileZilla")[0].maximize()
0

A hybrid pyautogui/win32gui solution for Windows:

  • if window is minimized, restore it
  • then focus on it using a single win32gui call, passing in the window handle
import pyautogui
import win32gui
import time

window = pyautogui.getWindowsWithTitle("Your Window Title")[0]
rect = window._rect
if rect.x == -32000 or rect.y == -32000: # minimized check
    window.restore()
    time.sleep(1)      # if restoring, need to wait briefly to set to foreground
win32gui.SetForegroundWindow(window._hWnd)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.