PyGetWindow Quick Start Guide | Generated by AI

Home 2025.10

Installation

PyGetWindow is a Python library for manipulating and querying windows on multiple platforms (Windows, macOS, and Linux). Install it via pip:

pip install pygetwindow

Importing the Module

Start by importing the module in your Python script:

import pygetwindow as gw

Getting Window Objects

PyGetWindow represents windows as Window objects. You can retrieve windows by title, process, or other attributes.

Example:

windows = gw.getAllWindows()
active = gw.getActiveWindow()
notepad = gw.getWindowsWithTitle('Notepad')  # List of windows with 'Notepad' in title

Common Methods on Window Objects

Once you have a Window object, you can access properties and methods like:

Example:

if notepad:
    win = notepad[0]
    print(win.title)  # Output: e.g., 'Untitled - Notepad'
    win.activate()
    win.maximize()
    win.move(100, 100)  # Move to position (100, 100)
    win.resize(800, 600)  # Resize to 800x600

Handling Multiple Platforms

Examples

Here are a few practical code snippets:

  1. Print all window titles:
    for win in gw.getAllWindows():
        print(win.title)
    
  2. Close a specific window:
    firefox_wins = gw.getWindowsWithTitle('Mozilla Firefox')
    if firefox_wins:
        firefox_wins[0].close()
    
  3. Move and resize multiple windows:
    windows = gw.getAllWindows()
    for i, win in enumerate(windows[:3]):  # First 3 windows
        win.move(50 * i, 50 * i)
        win.resize(400, 400)
    

For advanced usage, refer to the official documentation. If you encounter platform-specific issues, check system permissions or dependencies like xdotool on Linux.


Back

x-ai/grok-code-fast-1

Donate