Skip to content

Python pyautogui Module: Detailed Overview and Examples

The pyautogui module is a Python library that provides functions for automating interactions with the GUI (Graphical User Interface). It allows you to control the mouse and keyboard, take screenshots, and perform various other automation tasks. This is particularly useful for creating scripts that interact with applications and perform repetitive tasks.

Installing pyautogui

You can install pyautogui using pip:

pip install pyautogui

Key Functions and Methods

1. Mouse Control

pyautogui.moveTo(x, y, duration=0)

Moves the mouse to the specified (x, y) coordinates. The duration parameter specifies how long the movement should take.

Example
import pyautogui

# Move the mouse to coordinates (100, 200) in 1 second
pyautogui.moveTo(100, 200, duration=1)

pyautogui.click(x=None, y=None, clicks=1, interval=0, button='left')

Simulates a mouse click at the specified (x, y) coordinates. You can also specify the number of clicks, interval between clicks, and which mouse button to use.

Example
import pyautogui

# Click at the coordinates (300, 400)
pyautogui.click(300, 400)

pyautogui.doubleClick(x=None, y=None)

Simulates a double-click at the specified (x, y) coordinates.

Example
import pyautogui

# Double-click at the coordinates (500, 600)
pyautogui.doubleClick(500, 600)

pyautogui.rightClick(x=None, y=None)

Simulates a right-click at the specified (x, y) coordinates.

Example
import pyautogui

# Right-click at the coordinates (700, 800)
pyautogui.rightClick(700, 800)

2. Keyboard Control

pyautogui.typewrite(message, interval=0.0)

Types the specified message string with an optional delay between each keystroke.

Example
import pyautogui

# Type "Hello, World!" with a 0.1-second interval between keystrokes
pyautogui.typewrite("Hello, World!", interval=0.1)

pyautogui.press(key)

Simulates pressing a single key.

Example
import pyautogui

# Press the "enter" key
pyautogui.press("enter")

pyautogui.hotkey(*keys)

Simulates pressing multiple keys simultaneously.

Example
import pyautogui

# Simulate pressing "ctrl" + "s" (Save command)
pyautogui.hotkey("ctrl", "s")

3. Screenshots

pyautogui.screenshot(imageFilename=None, region=None)

Takes a screenshot of the entire screen or a specified region. If imageFilename is provided, the screenshot is saved to that file.

Example
import pyautogui

# Take a screenshot of the entire screen and save it as "screenshot.png"
pyautogui.screenshot("screenshot.png")

pyautogui.screenshot(region=(x, y, width, height))

Takes a screenshot of a specified region of the screen.

Example
import pyautogui

# Take a screenshot of a region (100, 100, 500, 400)
pyautogui.screenshot("screenshot_region.png", region=(100, 100, 500, 400))

4. Screen Position

pyautogui.position()

Returns the current mouse cursor position as a tuple (x, y).

Example
import pyautogui

# Print the current mouse position
print(pyautogui.position())

5. Screen Size

pyautogui.size()

Returns the screen resolution as a tuple (width, height).

Example
import pyautogui

# Print the screen size
print(pyautogui.size())

6. Image Recognition

pyautogui.locateOnScreen(image)

Searches for the specified image on the screen and returns the position if found.

Example
import pyautogui

# Locate an image on the screen
position = pyautogui.locateOnScreen("button.png")
print(position)

pyautogui.center(region)

Returns the center coordinates of the specified region.

Example
import pyautogui

# Find the center of a region
region = pyautogui.locateOnScreen("button.png")
if region:
    center = pyautogui.center(region)
    print(center)

7. Fail-Safe

pyautogui includes a fail-safe feature that moves the mouse to a corner of the screen to stop the program if needed. This is enabled by default.

Example

import pyautogui

# Enable fail-safe (move mouse to top-left corner to stop)
pyautogui.FAILSAFE = True

Example Automation Script

Here’s a complete example of an automation script that opens a text editor, types some text, and takes a screenshot:

import pyautogui
import time

# Wait for 5 seconds to switch to the text editor
time.sleep(5)

# Type text
pyautogui.typewrite("Hello, this is an automated message!", interval=0.1)

# Press Enter to create a new line
pyautogui.press("enter")

# Type more text
pyautogui.typewrite("This message was typed by PyAutoGUI.", interval=0.1)

# Take a screenshot of the entire screen
pyautogui.screenshot("automation_screenshot.png")

Conclusion

The pyautogui module provides a comprehensive suite of functions for automating GUI interactions, including mouse and keyboard control, screenshots, and image recognition. By leveraging these capabilities, you can automate repetitive tasks, perform testing, and interact with applications programmatically. Understanding and using pyautogui can significantly enhance productivity and streamline workflows that involve graphical user interfaces.