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
🌐
PyPI
pypi.org › project › PyAutoGUI
PyAutoGUI · PyPI
PyAutoGUI lets Python control the mouse and keyboard, and other GUI automation tasks. For Windows, macOS, and Linux, on Python 3 and 2.
      » pip install PyAutoGUI
    
Published   May 24, 2023
Version   0.9.54
Discussions

Can I help with pyautogui.getWindows() ?
Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. ... I was looking at Roadmap and I would like to help with the getWindows() method. pyautogui.getWindows() # returns a dict of window titles mapped to window IDs More on github.com
🌐 github.com
1
October 13, 2015
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?
Are you on Windows by any chance? As then you could just use pywinauto instead which allows to easily bind to a specific application via process name, window title etc. More on reddit.com
🌐 r/learnpython
12
2
February 1, 2022
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.com
🌐 r/learnpython
1
4
July 14, 2016
python - I can't find an example of pyautogui.locateOnWindow - Stack Overflow
I also have the same problem. Did you find any solution to focus on a specific program/window? ... 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). More on stackoverflow.com
🌐 stackoverflow.com
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest › roadmap.html
Roadmap — PyAutoGUI documentation - Read the Docs
PyAutoGUI is planned as a replacement for other Python GUI automation scripts, such as PyUserInput, PyKeyboard, PyMouse, pykey, etc.
🌐
Read the Docs
pyautogui.readthedocs.io
Welcome to PyAutoGUI’s documentation! — PyAutoGUI documentation
PyAutoGUI lets your Python scripts control the mouse and keyboard to automate interactions with other applications. The API is designed to be simple. PyAutoGUI works on Windows, macOS, and Linux, and runs on Python 2 and 3.
🌐
Automate the Boring Stuff
automatetheboringstuff.com › 2e › chapter20
20 controlling the keyboard and mouse with gui automation
The pyautogui module can send virtual keypresses and mouse clicks to Windows, macOS, and Linux. Windows and macOS users can simply use pip to install PyAutoGUI. However, Linux users will first have to install some software that PyAutoGUI depends on.
🌐
4sysops
4sysops.com › home › blog › articles › install pyautogui for windows gui automation
Install PyAutoGUI for Windows GUI automation – 4sysops
July 28, 2023 - PyAutoGUI is a Python module that enables interaction with a GUI operating system. With PyAutoGUI, we can "drive" the keyboard and mouse as if a user were running the PC. PyAutoGUI works with Windows, Linux, and macOS.
Find elsewhere
🌐
GitHub
github.com › asweigart › pyautogui › issues › 58
Can I help with pyautogui.getWindows() ? · Issue #58 · asweigart/pyautogui
October 13, 2015 - Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. ... I was looking at Roadmap and I would like to help with the getWindows() method. pyautogui.getWindows() # returns a dict of window titles mapped to window IDs
Author   emilopez
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest › install.html
Installation — PyAutoGUI documentation - Read the Docs
To install PyAutoGUI, install the pyautogui package from PyPI by running pip install pyautogui (on Windows) or pip3 install pyautogui (on macOS and Linux).
🌐
GitHub
github.com › asweigart › pyautogui › pull › 139 › files
Cookbook example: start an app and get its window on Windows by butla · Pull Request #139 · asweigart/pyautogui
Start an app and focus on its window (Windows) ---------------------------------------------- This can be used for functional tests of an application with a GUI. .. code-block:: python · import subprocess · import sys · import pyautogui · # start the app in a separate process using the same interpreter as this script ·
Author   asweigart
🌐
Reddit
reddit.com › r/learnpython › 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?
r/learnpython on Reddit: 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?
February 1, 2022 -

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?

🌐
GitHub
github.com › asweigart › pyautogui
GitHub - asweigart/pyautogui: A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard. · GitHub
A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard. - asweigart/pyautogui
Starred by 12.4K users
Forked by 1.4K users
Languages   Python
🌐
Medium
medium.com › @henilshah2004 › automating-repetitive-tasks-with-pyautogui-a-comprehensive-guide-0d539503f8f7
Automating Repetitive Tasks with PyAutoGUI: A Comprehensive Guide | by Henil Shah | Medium
October 20, 2023 - PyAutoGUI provides functionality to interact with windows and applications, allowing for seamless automation of tasks that involve opening, closing, and managing different software.
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest › mouse.html
Mouse Control Functions — PyAutoGUI documentation
#! python3 import pyautogui, sys print('Press Ctrl-C to quit.') try: while True: x, y = pyautogui.position() positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4) print(positionStr, end='') print('\b' * len(positionStr), end='', flush=True) except KeyboardInterrupt: print('\n')
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest › screenshot.html
Screenshot Functions — PyAutoGUI documentation
>>> import pyautogui >>> button7location = pyautogui.locateOnScreen('calc7key.png') >>> button7location Box(left=1416, top=562, width=50, height=41) >>> button7location[0] 1416 >>> button7location.left 1416 >>> button7point = pyautogui.center(button7location) >>> button7point Point(x=1441, y=582) >>> button7point[0] 1441 >>> button7point.x 1441 >>> button7x, button7y = button7point >>> pyautogui.click(button7x, button7y) # clicks the center of where the 7 button was found >>> pyautogui.click('calc7key.png') # a shortcut version to click on the center of where the 7 button was found
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest › keyboard.html
Keyboard Control Functions — PyAutoGUI documentation
>>> pyautogui.press('enter') # press the Enter key >>> pyautogui.press('f1') # press the F1 key >>> pyautogui.press('left') # press the left arrow key
Top answer
1 of 2
1

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)
2 of 2
0

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