PyGetWindow Documentation
repository·master·Indexed 19 days ago
https://github.com/asweigart/pygetwindowA cross-platform Python module for obtaining GUI information and controlling application windows. It allows developers to list windows, find them by title or screen coordinates, and manipulate their size, position, and state (maximize, minimize, restore, activate, and close). While designed to be cross-platform, only the Windows platform is currently fully implemented.
What's inside PyGetWindow
- PyGetWindow is designed to be cross-platform, but it is currently under development. As of the current version, only the Windows platform is fully implemented.
Install PyGetWindow via pip
masterPyGetWindow can be installed using
pip. On macOS and Linux, usepip3for Python 3 environments. If you encounter permission errors during installation, use the--userflag.# Standard installation pip install pygetwindow # macOS and Linux (Python 3) pip3 install pygetwindow # If you encounter permission errors pip install --user pygetwindowControl and inspect Window objects
masterOnce you have a
Windowobject, you can manipulate its state, size, position, and visibility, or inspect its current attributes.Window State and Visibility
.maximize(): Maximizes the window..minimize(): Minimizes the window..restore(): Restores the window from a minimized or maximized state..activate(): Brings the window to the foreground..close(): Closes the window..isMaximized: Boolean attribute indicating if the window is currently maximized.
Resizing and Moving
.resize(x, y): Increases the window size byxpixels horizontally andypixels vertically..resizeTo(width, height): Sets the window to a specific width and height..move(x, y): Moves the windowxpixels right andypixels down from its current position..moveTo(x, y): Moves the window to the specific screen coordinates(x, y).
Attributes
.title: The title of the window..size: A tuple of(width, height)..width: The current width..height: The current height..topleft: A tuple of(left, top)coordinates..top: The Y coordinate of the top edge..left: The X coordinate of the left edge..bottomright: A tuple of(right, bottom)coordinates.
import pygetwindow as gw # Example: Manipulating a Notepad window windows = gw.getWindowsWithTitle('Untitled') if windows: notepad = windows[0] # State control notepad.maximize() notepad.restore() notepad.minimize() notepad.activate() # Resizing and Moving notepad.resize(10, 10) # Increase size notepad.resizeTo(100, 100) # Set specific size notepad.move(10, 10) # Relative move notepad.moveTo(10, 10) # Absolute move # Inspection print(f"Size: {notepad.size}") print(f"Top-Left: {notepad.topleft}") # Close notepad.close()Obtain Window objects with PyGetWindow
masterPyGetWindow provides several functions to retrieve
Windowobjects based on different criteria:getAllTitles(): Returns a tuple of all visible window titles.getAllWindows(): Returns a tuple of allWindowobjects currently open.getWindowsWithTitle(title): Returns a tuple ofWindowobjects whose titles contain the specified string.getFocusedWindow(): Returns theWindowobject that currently has focus.getWindowsAt(x, y): Returns a tuple ofWindowobjects located at the specified screen coordinates(x, y).
import pygetwindow as gw # Get all window titles titles = gw.getAllTitles() # Get all window objects windows = gw.getAllWindows() # Find windows by title substring windows_with_title = gw.getWindowsWithTitle('Untitled') # Get the currently focused window focused = gw.getFocusedWindow() # Get windows at specific coordinates windows_at_pos = gw.getWindowsAt(10, 10)Obtain Window objects
masterPyGetWindow provides several functions to retrieve
Windowobjects. Note that on Windows, these objects contain anhWndattribute.gw.getAllTitles(): Returns a tuple of all visible window titles.gw.getAllWindows(): Returns a tuple of allWindowobjects.gw.getWindowsWithTitle(title): Returns a tuple ofWindowobjects whose titles contain the specified string.gw.getActiveWindow(): Returns the currently activeWindowobject.gw.getWindowsAt(x, y): Returns a tuple ofWindowobjects located at the specified screen coordinates.
import pygetwindow as gw # Get all window titles titles = gw.getAllTitles() # Get all window objects windows = gw.getAllWindows() # Find windows by title substring windows_with_title = gw.getWindowsWithTitle('Untitled') # Get the active window active_win = gw.getActiveWindow() # Get windows at specific coordinates windows_at_pos = gw.getWindowsAt(10, 10)Manipulate and inspect Window objects
masterOnce you have a
Windowobject, you can inspect its properties or perform actions to change its state, position, or size.Window State Actions
maximize(): Maximizes the window.minimize(): Minimizes the window.restore(): Restores the window to its original size/position.focus(): Brings the window to the foreground.close(): Closes the window.
Resizing and Moving
resize(x, y): Increases the window size byxpixels horizontally andypixels vertically.resizeTo(width, height): Sets the window to a specific width and height.move(x, y): Moves the windowxpixels right andypixels down.moveTo(x, y): Moves the window to the specific screen coordinates(x, y).
Window Attributes
isMaximized: Boolean indicating if the window is maximized.title: The title of the window.size: A tuple of(width, height).width/height: The current width and height.topleft: A tuple of(left, top)coordinates.top/left: The current top and left coordinate values.bottomright: A tuple of(right, bottom)coordinates.
import pygetwindow as gw # Get a window object win = gw.getWindowsWithTitle('Untitled')[0] # Change state win.maximize() win.restore() win.minimize() win.focus() # Resize and Move win.resize(10, 10) # Increase size by 10, 10 win.resizeTo(100, 100) # Set size to 100x100 win.move(10, 10) # Move 10px right and 10px down win.moveTo(10, 10) # Move to absolute position 10, 10 # Inspect attributes print(win.width, win.height) print(win.topleft) # Close the window win.close()Check if a point is within a rectangle with pointInRect()
masterUse the
pointInRectfunction to determine if a specific(x, y)coordinate falls inside a rectangular area defined byleft,top,width, andheight.from pygetwindow import pointInRect # Returns True if (50, 50) is inside the box at (0, 0) with size 100x100 is_inside = pointInRect(50, 50, 0, 0, 100, 100)Find windows by title or location
masterOn Windows, the module provides several utility functions to locate windows:
getAllWindows(): Returns a list of all open windows.getAllTitles(): Returns a list of all window titles.getWindowsWithTitle(title): Returns a list of windows whose titles contain the specified string.getWindowsAt(x, y): Returns a list of windows located at the specified coordinates.getActiveWindow(): Returns the currently active window object.getActiveWindowTitle(): Returns the title of the currently active window.
Manage window state and geometry with Window objects
masterThe
Windowclass (which resolves toWin32Windowon Windows orMacOSWindowon macOS) provides an interface to control and query window properties.Window State Methods:
activate(): Brings the window to the foreground.close(): Closes the window (simulates clicking the 'X' button).minimize(): Minimizes the window.maximize(): Maximizes the window.restore(): Restores the window from a minimized or maximized state.
Window Geometry Methods:
moveTo(newLeft, newTop): Moves the window to specific screen coordinates.resizeTo(newWidth, newHeight): Resizes the window to specific dimensions.moveRel(xOffset, yOffset): Moves the window relative to its current position.resizeRel(widthOffset, heightOffset): Resizes the window relative to its current size.
Window Properties:
title: The window's title string.isActive: Boolean indicating if the window is the current foreground window.isMinimized: Boolean indicating if the window is minimized.isMaximized: Boolean indicating if the window is maximized.visible: Boolean indicating if the window is visible.- Geometry properties (getters/setters):
left,top,right,bottom,width,height,center,size,area,box, and corner/edge points liketopleft,bottomright,midtop, etc.
Handle PyGetWindow errors with PyGetWindowException
masterAll errors specific to thepygetwindowmodule are raised asPyGetWindowException. If an error is raised that does not inherit from this class, it is considered a bug in the module itself.