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.
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-sampleUsing 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
