PyGetWindow Documentation

repository·master·Indexed 19 days ago

https://github.com/asweigart/pygetwindow

A 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.

Tokens
2.6K
Snippets
6
Records
10
Agent score
61%

What's inside PyGetWindow

  1. Install PyGetWindow via pip

    master

    PyGetWindow can be installed using pip. On macOS and Linux, use pip3 for Python 3 environments. If you encounter permission errors during installation, use the --user flag.

    # Standard installation
    pip install pygetwindow
    
    # macOS and Linux (Python 3)
    pip3 install pygetwindow
    
    # If you encounter permission errors
    pip install --user pygetwindow
  2. Control and inspect Window objects

    master

    Once you have a Window object, 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 by x pixels horizontally and y pixels vertically.
    • .resizeTo(width, height): Sets the window to a specific width and height.
    • .move(x, y): Moves the window x pixels right and y pixels 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()
  3. Obtain Window objects with PyGetWindow

    master

    PyGetWindow provides several functions to retrieve Window objects based on different criteria:

    • getAllTitles(): Returns a tuple of all visible window titles.
    • getAllWindows(): Returns a tuple of all Window objects currently open.
    • getWindowsWithTitle(title): Returns a tuple of Window objects whose titles contain the specified string.
    • getFocusedWindow(): Returns the Window object that currently has focus.
    • getWindowsAt(x, y): Returns a tuple of Window objects 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)
  4. Obtain Window objects

    master

    PyGetWindow provides several functions to retrieve Window objects. Note that on Windows, these objects contain an hWnd attribute.

    • gw.getAllTitles(): Returns a tuple of all visible window titles.
    • gw.getAllWindows(): Returns a tuple of all Window objects.
    • gw.getWindowsWithTitle(title): Returns a tuple of Window objects whose titles contain the specified string.
    • gw.getActiveWindow(): Returns the currently active Window object.
    • gw.getWindowsAt(x, y): Returns a tuple of Window objects 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)
  5. Manipulate and inspect Window objects

    master

    Once you have a Window object, 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 by x pixels horizontally and y pixels vertically.
    • resizeTo(width, height): Sets the window to a specific width and height.
    • move(x, y): Moves the window x pixels right and y pixels 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()
  6. Check if a point is within a rectangle with pointInRect()

    master

    Use the pointInRect function to determine if a specific (x, y) coordinate falls inside a rectangular area defined by left, top, width, and height.

    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)
  7. Find windows by title or location

    master

    On 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.
  8. Manage window state and geometry with Window objects

    master

    The Window class (which resolves to Win32Window on Windows or MacOSWindow on 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 like topleft, bottomright, midtop, etc.