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
Note, must call pyautogui.getWindowsWithTitle(), dont call as
from pyautogui import getWindowsWithTitle
getWindowsWithTitle() # will raise error
Answer from Vasanth Prabakar on Stack Overflow
» pip install PyAutoGUI
Can I help with pyautogui.getWindows() ?
Selenium and PyAutoGui. How to bring webdriver to focus/foreground/activewindow so I can make sure that PyAutoGui will be interacting with the correct application window?
pyautogui ,how to finding the active window?
I think pyautogui only simulates mouse and keyboard, so it doesn't handle windows at all.
If it's for Windows what you are looking for is pywinauto, it has an inspector and code generator SWAPY which comes in handy for what you want.
If it's for Linux or OS X, I don't know any alternative to pywinauto.
Edit: I found a Cross Platform (Linux, Mac, Windows) GUI Test Automation tool ldpt (Linux) - cobra (Windows) - pyAtom (Mac) but I never used it.
More on reddit.compython - I can't find an example of pyautogui.locateOnWindow - Stack Overflow
Videos
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
Note, must call pyautogui.getWindowsWithTitle(), dont call as
from pyautogui import getWindowsWithTitle
getWindowsWithTitle() # will raise error
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.)
Howdy!
I'm working with selenium on a webpage that doesn't interact too well with the standard selenium code (html, xpath, element id, etc). But since the webpage is pretty much static and I can query via URL, I decided to simulate an user with pyautogui.
The issue is that pyautogui will click and type on what is on screen, and since I only have one monitor I have to run my code (from pycharm) then quickly alt+tab or select the chromedriver window for the code to run properly, with the risk of the pyautogui interacting and messing with the code on pycharm.
What can I do to simply bring the chrome driver to the foregroud/active window so that pyautogui will always interact with it?
i need to send key strokes to an application . But so far i couldnt find how check the name of the active window and how to activate the windows i need . is there a way to do this? This was simple with autohotkey.
Also is there a better alternative to pyautogui?
From pyautogui documentation, as Screenshot Functions says: you can call the locateOnScreen() function to get the screen coordinates. The return value is a 4-integer tuple: (left, top, width, height). This tuple can be passed to center() function to get the X and Y coordinates at the center of this region.
So I saved a discord icon image from my laptop and found the coordinates correctly as below:
Image:

import pyautogui
image = 'discord.png'
locate = pyautogui.locateOnScreen(image, grayscale=True, confidence=0.8)
position = pyautogui.center(pyautogui.locateOnScreen(image, grayscale=True, confidence=0.8))
If you print(locate):
Box(left=91, top=288, width=23, height=26)
If you print(position):
Point(x=102, y=301)
I had the same problem. After searching through the documentation I found this on their roadmap:
- Click coordinates relative to a window, instead of the entire screen.
Since the function 'locateOnWindow' also does not get commented on in the documentation, and I found no examples nor solutions - just as OP - I wonder whether this feature is simply not yet implemented even though the command is there.
Alternate solution: While not exactly the answer you wished for, try another module such as pywinauto that allows for control of windows and use it to put your window in the foreground in a reproductible way (same position and size).
Edit: you can use pyautogui for that...
active_window = pyautogui.getWindowsWithTitle('title')[0]
active_window.activate()
active_window.maximize()