Key Features of PyAutoGUI Library
Cross-platform Compatibility: PyAutoGUI is compatible with Windows, macOS, and Linux operating systems, making it versatile for automation across different platforms.
- Mouse and Keyboard Control: With PyAutoGUI, developers can programmatically control the mouse cursor movements, clicks, and keyboard inputs, enabling automation of various GUI tasks.
- Screenshot Capture: PyAutoGUI allows capturing screenshots of the entire screen or specific regions, facilitating visual inspection and verification in automated workflows.
- Pixel Detection: PyAutoGUI provides functions to locate specific pixels on the screen, enabling automation based on pixel color and position.
- Window Management: PyAutoGUI offers features for window management, including maximizing, minimizing, and focusing windows, enhancing automation capabilities in GUI-based applications.
- Customizable Hotkeys: Developers can create custom hotkeys to trigger specific actions or functions, providing flexibility and customization in automation scripts.
- Smooth Integration: PyAutoGUI seamlessly integrates with other Python libraries and frameworks, such as OpenCV and Selenium, enhancing its functionality and usability in diverse automation scenarios.
- Open Source and Active Community: Being an open-source library, PyAutoGUI benefits from a vibrant community of developers who contribute to its development, provide support, and share resources and best practices.
Installation of PyAutoGUI Library
Installing the PyAutoGUI library is a simple task.
For Windows
To install the PyAutoGUI library in the Windows system, open the PowerShell by right-clicking the start button or opening the command prompt and then type:
>pip install pyautogui
The pyautogui library will be installed, and we are good to go.
For Linux
Before installing the PyAutoGUI library, there are certain other software that must be installed. Open the terminal and type:
sudo apt-get install scrot
sudo apt-get install python3-tk
sudo apt-get install python3-dev
Once the above software have been installed, type:
pip install pyautogui
The PyAutoGUI library has been installed.
PyAutoGUI Basic Functions
The size() method
The size() method in PyAutoGUI retrieves the screen resolution or the size of the primary display monitor. It returns a tuple containing the width and height of the screen in pixels. This information is useful for determining the boundaries of the screen and for positioning GUI elements or performing mouse movements within the screen's bounds.
The position() method
The position() method in PyAutoGUI returns the current position of the mouse cursor on the screen. It returns a tuple containing the x and y coordinates of the mouse cursor relative to the screen's top-left corner. This function is handy for obtaining the mouse's current location, which can be used to automate tasks based on mouse cursor positions.
The onscreen() method
The onscreen() method in PyAutoGUI checks whether a given point is within the bounds of the screen. It takes x and y coordinates as input and returns a boolean value indicating whether the specified point is within the screen's boundaries. This function is useful for validating mouse cursor positions before performing mouse-related actions, ensuring that actions are performed only within the visible screen area.
Basic Mouse Functions
Some of the import mouse control functions are:
moveTo() Function
The moveTo() function receives two values as the parameters. The first value represents the X coordinate, and the second value represents the Y coordinate. The function will then move the cursor to the position as specified in the values.
We can also pass “none” as the value for either X or Y or for both the coordinates.
Example:
import pyautogui;
pyautogui.moveTo(74,89)
Output:
The cursor is moved to (74,89). Nothing is displayed at the console.
moveRel() Function
The moveRel() function in PyAutoGUI moves the mouse cursor relative to its current position. It takes two integer arguments, xOffset and yOffset, representing the number of pixels to move the cursor horizontally and vertically, respectively.
Example:
import pyautogui
# Move the mouse cursor 100 pixels to the right and 50 pixels down
pyautogui.moveRel(100, 50)
Output:
The mouse cursor moves 100 pixels to the right and 50 pixels down from its current position on the screen.
click() Function
The click() function as the name says, is used to trigger a single left click at the position where the cursor is currently located.
Generally, the click() function is given the coordinates of a specific position in order to trigger a click at that position.
Example 1:
import pyautogui;
pyautogui.click()
Example 2:
import pyautogui;
pyautogui.click(x=23, y=78)
Output :
A left-click is triggered at position (23,78). No output is displayed at the console.
scroll() Function
By invoking the scroll() function and passing an integer number of "clicks" to scroll, the mouse scroll wheel can be simulated.
Example:
import pyautogui;
pyautogui.scroll(5)
Output:
Scrolls up 5 clicks
Common Keyboard Operations
Some of the important keyboard control functions are:
write() Function
The write function() receives a String as the parameter and prints the characters of the String as passed. If you want to print the characters with some delay, you can provide the interval value.
Example:
import pyautogui;
pyautogui.write('Coding Ninjas')
Output:
It prints Coding Ninjas wherever the cursor is pointing.
Example:
import pyautogui;
pyautogui.write('Coding Ninjas', interval=0.5)
Output:
Prints each character of Coding Ninja after the interval of 0.5 seconds.
press() Function
To press any key from the keyboard, we use the press function. We just have to provide the name of the key as a String parameter in the press() function.
Example:
import pyautogui
pyautogui.press('space')
Output:
The cursor moves one unit ahead of its current position.
hotkey() Function
We can pass several key strings, which will be pressed down in order, and then released in reverse order.
Example:
import pyautogui
pyautogui.hotkey('alt','ctrl','delete')
Message Box Functions
Some of the important message box functions are:
alert() Function
The alert message is similar to the one we have in JavaScript. We pass the String that we wish to display in the alert box. The alert box simply displays the text with an OK button which can be clicked.
Example:
import pyautogui
pyautogui.alert('This is an example of alert box!')
Output:

confirm() Function
The confirm() function is very similar to the alert() function. However, apart from the OK button, there is also a cancel button. In case we don't want to click on the OK, we can click the cancel button. It allows us to disagree with the message in the message box.
Example:
import pyautogui
pyautogui.confirm('Do you confirm that you want a course from Coding Ninja?')
Output:

prompt() Function
It Displays a message box with a text input, an OK button, and a Cancel button. It returns the text entered, or None if Cancel was clicked.
Example:
import pyautogui
pyautogui.prompt('How was your experience reading this article from Coding Ninjas?')
Output:

password() Function
It is used to display a message box with text input, an OK button, and a Cancel button. Typed characters appear as asterisks (*). It returns the text entered or None if the Cancel button is clicked.
Example:
import pyautogui
pyautogui.password('Please enter your password.')
Output:

Screenshot Functions
Some of the important screenshot functions are:
screenshot() Function
To use the screenshot() function, you must first install the pillow module. Otherwise, it will throw an error. The screenshot() method is used to take the screenshot of the current screen. We can save the screenshot using the save().
Example:
import pyautogui
im1 = pyautogui.screenshot()
im1.save(r"c:\screenshot.png")
The above code will save the screenshot at the location provided inside the save() function.
Frequently Asked Questions
How to get PyAutoGUI in Python?
You can install PyAutoGUI using pip, the Python package manager, by running the command pip install pyautogui in your terminal or command prompt. Ensure that pip is installed and up-to-date before running the command.
What is the PyAutoGUI module function?
The PyAutoGUI module provides functions for automating tasks involving mouse movements, keyboard inputs, window management, and screenshot capture. These functions enable developers to create scripts for automating repetitive tasks in GUI-based applications.
Why PyAutoGUI is used?
PyAutoGUI is used for automating repetitive tasks in GUI-based applications. It simplifies the process of controlling mouse movements, keyboard inputs, window management, and screenshot capture, thereby improving productivity and efficiency in various domains such as software testing, data entry, and GUI automation.
How to automate using PyAutoGUI?
To automate tasks using PyAutoGUI, import the module in your Python script and use its functions to control mouse movements, simulate keyboard inputs, manage windows, capture screenshots, and perform other GUI automation tasks. Write scripts to automate repetitive tasks, streamline workflows, and increase productivity.
Conclusion
In this article, we learned about one of the important libraries in Python, i.e., the PyAutoGUI library. This library empowers developers to automate GUI tasks efficiently, enhancing productivity across various domains. With its intuitive interface and comprehensive set of functions, PyAutoGUI simplifies automation, streamlines workflows, and enables the creation of robust automation scripts.