Operations 5 min read

Automate Your Desktop with Python: A Complete PyAutoGUI Guide

This article introduces PyAutoGUI, a cross‑platform Python library for controlling mouse, keyboard, dialogs and screenshots, shows how to install it, explains its coordinate system, outlines core functions, and provides practical code examples for drawing and image‑based automation.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Automate Your Desktop with Python: A Complete PyAutoGUI Guide

Some users may have tried tools like AutoHotkey for automating keyboard and mouse actions, but with Python you can achieve the same results using the cross‑platform library PyAutoGUI , which works on Windows, Linux and macOS.

Installation and Usage

Installing PyAutoGUI is straightforward via pip: pip install pyautogui The library uses a screen coordinate system where the top‑left corner is (0,0); the x‑axis increases to the right and the y‑axis increases downward. For a 1920×1080 screen, the bottom‑right corner is (1919, 1079).

Function Overview

PyAutoGUI provides simple functions for mouse actions (click, double‑click, drag, scroll), keyboard actions (press, hotkey, type), alert dialogs, and screenshot handling. Parameters are typically limited to click intervals or button names, making the API easy to use.

Practical Examples

Drawing Example – The code below opens a drawing program, maximizes it, waits three seconds, and then uses pyautogui.drag to draw concentric squares:

import pyautogui

distance = 100
pyautogui.moveTo(400, 300)
while distance > 0:
    pyautogui.drag(distance, 0, duration=0.1)
    distance -= 5
    pyautogui.drag(0, distance, duration=0.1)
    pyautogui.drag(-distance, 0, duration=0.1)
    distance -= 5
    pyautogui.drag(0, -distance, duration=0.1)

The result is a simple pattern created by repeatedly dragging the mouse.

Image‑Recognition Example – After installing opencv-python, the script below locates button images on the screen and clicks them to perform a calculation (5 × 8 =):

import pyautogui
import time

time.sleep(3)

# Button screenshots
five = '5.png'
eight = '8.png'
multiply = 'multiply.png'
equals = 'equals.png'

def find_and_click(image):
    x, y = pyautogui.locateCenterOnScreen(image, confidence=0.9)
    pyautogui.click(x, y)

find_and_click(five)
find_and_click(multiply)
find_and_click(eight)
find_and_click(equals)

Full examples and additional resources are available on the author's GitHub:

https://github.com/techstay/python-study/tree/master/pyautogui-sample

Using PyAutoGUI, you can easily automate graphical user interfaces by positioning the mouse or recognizing images, then controlling input programmatically. While it offers similar capabilities to traditional macro tools, Python’s extensive ecosystem enables far more advanced automation scenarios.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonCode Examplescross‑platformDesktop Automationpyautoguigui scripting
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

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.