Operations 8 min read

Using pywinauto for Windows GUI Automation with Python

This article introduces the pywinauto library, explains how to install it, create Application objects, locate windows and controls, and perform mouse and keyboard automation on Windows applications using Python, with detailed code examples and usage tips.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using pywinauto for Windows GUI Automation with Python

Today we share a Python library for automating mouse and keyboard actions on Windows GUI applications: pywinauto .

1. Installation

Install the library via pip:

pip install pywinauto

Official documentation: https://pywinauto.readthedocs.io/en/latest/

2. Application

To control a Windows program, create an Application object. The optional backend parameter can be win32 (default) or uia . win32 works with MFC, VB6, VCL, simple WinForms, and legacy apps; uia works with WinForms, WPF, Store apps, Qt5, browsers, etc.

If you are unsure which backend to use, inspect the target with Inspect (for uia ) or Spy++ (for win32 ) and choose the one that shows more information.

Typical methods of the Application object are shown in the following diagram:

Example: start the WeChat application by connecting to its process ID (PID) shown in Task Manager.

3. WindowSpecification

Each window is represented by a WindowSpecification object, obtained via Application.window() using parameters such as title , classname , or best_match . Note that the name shown in Inspect corresponds to the title argument.

Common methods of WindowSpecification are illustrated in the following image:

4. Element Controls

Windows contain many control types (Button, Edit, TreeView, CheckBox, Dialog, Toolbar, StatusBar, ListBox, Pane, Menu, Header, etc.). Each control has an element_info attribute that returns an ElementInfo derived object (e.g., UIAElementInfo or HwndElementInfo ) with useful properties and methods.

Common wrapper methods are listed in the following tables:

Two particularly useful methods are click_input() (mouse click) and type_keys() (keyboard input).

Mouse Operations

The mouse module provides the core private method _perform_click_input() , wrapped by higher‑level functions such as click_input() . Important parameters include:

button : default "left"; options are left, right, middle, move, wheel, x.

coords : a tuple (x, y) specifying screen coordinates; default (0, 0).

wheel_dist : scroll distance; positive for up, negative for down.

Example screenshot of mouse click usage:

Keyboard Operations

The keyboard module’s main function is send_keys() . The first argument keys specifies the key sequence. Optional boolean flags (e.g., with_spaces , with_tabs , turn_off_numlock ) control behavior.

Keys are expressed inside curly braces, e.g., {ENTER} . By default, keys are sent as virtual‑key codes prefixed with VK_ . To send literal characters, set vk_packet=False .

Modifiers can be written with down and up inside braces, or using shorthand symbols: + for Shift, ^ for Control, % for Alt.

Example of modifier usage:

For a full list of supported keys, refer to the official documentation: pywinauto.keyboard .

In summary, pywinauto offers a comprehensive set of APIs to launch applications, locate windows and controls, and simulate mouse and keyboard actions on Windows, making it a powerful tool for GUI testing and automation.

PythonkeyboardWindowspywinautogui-automationmouse
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.