PyGetWindow
PyGetWindow is a simple, cross-platform Python module for obtaining graphical user interface (GUI) information on application windows. It allows programmatic interaction with windows, such as listing titles, finding specific windows, and controlling their state (minimize, maximize, activate, resize, move, close). The library is currently at version 0.0.9 and is still under active development, with primary functionality implemented for Windows.
Warnings
- gotcha PyGetWindow's cross-platform support is currently limited. While designed to be cross-platform, full functionality is primarily implemented and tested on Windows. Support for macOS is partial, and Linux has known issues or lacks certain features. Some methods might raise exceptions or not work as expected on non-Windows systems.
- gotcha To catch `PyGetWindowException` (e.g., when a window is not found), you must import it directly using `from pygetwindow import PyGetWindowException`. Attempting to catch `pygetwindow.PyGetWindowException` will result in a `NameError` if `pygetwindow` was imported with an alias like `import pygetwindow as gw` and `PyGetWindowException` was not explicitly imported.
- gotcha The library is still under development, and some features may be incomplete or unstable. There are reported issues such as incorrect window location coordinates (sometimes exacerbated when used with PyAutoGUI), inability to retrieve minimized windows, and problems with the `__str__` representation of `Window` objects.
- gotcha Window manipulation methods like `activate()`, `minimize()`, `maximize()`, `moveTo()`, and `resizeTo()` directly interact with the operating system's GUI. They can cause visual changes on the user's screen and might interfere with user input, especially in automated scripts running in the foreground.
Install
-
pip install pygetwindow
Imports
- pygetwindow
import pygetwindow as gw
- PyGetWindowException
from pygetwindow import PyGetWindowException
Quickstart
import pygetwindow as gw
# Get a list of all visible window titles
all_titles = gw.getAllTitles()
print(f"All window titles: {all_titles}")
# Get a list of all Window objects
all_windows = gw.getAllWindows()
print(f"Number of open windows: {len(all_windows)}")
if all_windows:
# Get the currently active (foreground) window
active_window = gw.getActiveWindow()
if active_window:
print(f"\nActive window title: {active_window.title}")
print(f"Active window position: {active_window.topleft}")
print(f"Active window size: {active_window.size}")
# Try to find a specific window (e.g., a browser or editor)
# Replace 'Chrome' with a title from your `all_titles` list for testing
try:
target_window = gw.getWindowsWithTitle('Chrome')[0]
print(f"\nFound target window: {target_window.title}")
# Example actions (commented out as they affect your desktop visually)
# target_window.activate()
# target_window.minimize()
# target_window.maximize()
# target_window.restore()
# target_window.moveTo(100, 100)
# target_window.resizeTo(800, 600)
except IndexError:
print("\n'Chrome' window not found. Try another title from the list.")