Introduction
In this tutorial, we're going to learn how to use pyautogui library in Python 3. The PyAutoGUI library provides cross-platform support for managing mouse and keyboard operations through code to enable automation of tasks. The pyautogui library is also available for Python 2; however, we will be using Python 3 throughout the course of this tutorial.
A tool like this has many applications, a few of which include taking screenshots, automating GUI testing (like Selenium), automating tasks that can only be done with a GUI, etc.
Before you go ahead with this tutorial, please note that there are a few prerequisites. You should have a basic understanding of Python's syntax, and/or have done at least beginner level programming in some other language. Other than that, the tutorial is quite simple and easy to follow for beginners.
The installation process for PyAutoGUI is fairly simple for all Operating Systems. However, there are a few dependencies for Mac and Linux that need to be installed before the PyAutoGUI library can be installed and used in programs.
For Windows, PyAutoGUI has no dependencies. Simply run the following command in your command prompt and the installation will be done.
For Mac, pyobjc-core and pyobjc modules are needed to be installed in sequence first. Below are the commands that you need to run in sequence in your terminal for successful installation:
For Linux, the only dependency is python3-xlib (for Python 3). To install that, followed by pyautogui, run the two commands mentioned below in your terminal:
In this section, we are going to cover some of the most commonly used functions from the PyAutoGUI library.
Before we can use PyAutoGUI functions, we need to import it into our program:
This position() function tells us the current position of the mouse on our screen:
Output:
The onScreen() function tells us whether the point with coordinates x and y exists on the screen:
Output:
Here we can see that the first point exists on the screen, but the second point falls beyond the screen's dimensions.
The size() function finds the height and width (resolution) of a screen.
Output:
Your output may be different and will depend on your screen's size.
In this section, we are going to cover PyAutoGUI functions for mouse manipulation, which includes both moving the position of the cursor as well as clicking buttons automatically through code.
The syntax of the moveTo() function is as follows:
The value of x_coordinate increases from left to right on the screen, and the value of y_coordinate increases from top to bottom. The value of both x_coordinate and y_coordinate at the top left corner of the screen is 0.
Look at the following script:
In the code above, the main focus is the moveTo() function that moves the mouse cursor on the screen based on the coordinates we provide as parameters. The first parameter is the x-coordinate and the second parameter is the y-coordinate. It is important to note that these coordinates represent the absolute position of the cursor.
One more thing that has been introduced in the code above is the PAUSE property; it basically pauses the execution of the script for the given amount of time. The PAUSE property has been added in the above code so that you can see the function execution; otherwise, the functions would execute in a split second and you wont be able to actually see the cursor moving from one location to the other on the screen.
Another workaround for this would be to indicate the time for each moveTo() operation as the third parameter in the function, e.g. moveTo(x, y, time_in_seconds).
Executing the above script may result in the following error:
Note: Possible Error
If the execution of the moveTo() function generates an error similar to the one shown above, it means that your computer's fail-safe is enabled. To disable the fail-safe, add the following line at the start of your code:
This feature is enabled by default so that you can easily stop execution of your pyautogui program by manually moving the mouse to the upper left corner of the screen. Once the mouse is in this location, pyautogui will throw an exception and exit.
The coordinates of the moveTo() function are absolute. However, if you want to move the mouse position relative to the current mouse position, you can use the moveRel() function.
What this means is that the reference point for this function, when moving the cursor, would not be the top left point on the screen (0, 0), but the current position of the mouse cursor. So, if your mouse cursor is currently at point (100, 100) on the screen and you call the moveRel() function with the parameters (100, 100, 2) the new position of your move cursor would be (200, 200).
You can use the moveRel() function as shown below:
The above script will move the cursor 100 points to the right and 100 points down in 2 seconds, with respect to the current cursor position.
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
The click() function is used to imitate mouse click operations. The syntax for the click() function is as follows:
The parameters are explained as follows:
Here is an example:
You can also execute specific click functions as follows:
Here the x and y represent the x and y coordinates, just like in the previous functions.
You can also have more fine-grained control over mouse clicks by specifying when to press the mouse down, and when to release it up. This is done using the mouseDown and mouseUp functions, respectively.
Here is a short example:
The above code is equivalent to just doing a pag.click(x, y) call.
The last mouse function we are going to cover is scroll. As expected, it has two options: scroll up and scroll down. The syntax for the scroll() function is as follows:
To scroll up, specify a positive value for amount_to_scroll parameter, and to scroll down, specify a negative value. Here is an example:
Alright, this was it for the mouse functions. By now, you should be able to control your mouse's buttons as well as movements through code. Let's now move to keyboard functions. There are plenty, but we will cover only those that are most frequently used.
Before we move to the functions, it is important that we know which keys can be pressed through code in pyautogui, as well as their exact naming convention. To do so, run the following script:
Output:
The typewrite() function is used to type something in a text field. Syntax for the function is as follows:
Here text is what you wish to type in the field and interval is time in seconds between each key stroke. Here is an example:
Executing the script above will enter the text "Junaid Khalid" in the field that is currently selected with a pause of 1 second between each key press.
Another way this function can be used is by passing in a list of keys that you'd like to press in a sequence. To do that through code, see the example below:
In the above example, the text junaide would be entered, followed by the removal of the trailing e. The input in the text field will be submitted by pressing the Enter key.
If you haven't noticed this so far, the keys we've shown above have no mention for combined operations like Control + C for the copy command. In case you're thinking you could do that by passing the list ['ctrl', 'c'] to the typewrite() function, you are wrong. The typewrite() function would press both those buttons in a sequence, not simultaneously. And as you probably already know, to execute the copy command, you need to press the C key while holding the ctrl key.
To press two or more keys simultaneously, you can use the hotkey() function, as shown here:
If you would like to take a screenshot of the screen at any instance, the screenshot() function is the one you are looking for. Let's see how we can implement that using PyAutoGUI:
This will store a PIL object containing the image in a variable.
If, however, you want to store the screenshot directly to your computer, you can call the screenshot function like this instead:
This will save the screenshot in a file, with the filename given, on your computer.
The last set of functions that we are going to cover in this tutorial are the message box functions. Here is a list of the message box functions available in PyAutoGUI:
Now that we have seen the types, let's see how we can display these buttons on the screen in the same sequence as above:
In the output, you will see the following sequence of message boxes.
Confirm:
Alert:
Prompt:
In this tutorial, we learned how to use PyAutoGUI automation library in Python. We started off by talking about pre-requisites for this tutorial, its installation process for different operating systems, followed by learning about some of its general function. After that we studied the functions specific to mouse movements, mouse control, and keyboard control.
After following this tutorial, you should be able to use PyAutoGUI to automate GUI operations for repetitive tasks in your own application.
Go here to see the original:
Getting Started with Python PyAutoGUI
- A Clockwork Greenshirt Introducing the Alt Left ... [Last Updated On: February 6th, 2018] [Originally Added On: February 6th, 2018]
- Jordan Towers 70news [Last Updated On: March 7th, 2018] [Originally Added On: March 7th, 2018]
- Robert Stark Talks to Ashley Messinger About ... - altleft.com [Last Updated On: March 18th, 2018] [Originally Added On: March 18th, 2018]
- KNOPPIX 8.1.0 - Linux Live System [Last Updated On: March 19th, 2018] [Originally Added On: March 19th, 2018]
- Protesters Try to Shut Down Jordan Peterson at Queen's ... [Last Updated On: April 27th, 2018] [Originally Added On: April 27th, 2018]
- KNOPPIX 8.0.0 - Linux Live System [Last Updated On: May 25th, 2018] [Originally Added On: May 25th, 2018]
- John McCain: Dying Without Dignity - committedconservative.com [Last Updated On: June 10th, 2018] [Originally Added On: June 10th, 2018]
- What is the Alt Left? - American Greatness [Last Updated On: June 29th, 2018] [Originally Added On: June 29th, 2018]
- event object JavaScript - Dottoro [Last Updated On: July 28th, 2018] [Originally Added On: July 28th, 2018]
- Virginia Election Predictions: Will Weather Rain on ... [Last Updated On: August 24th, 2018] [Originally Added On: August 24th, 2018]
- Alt-Left Melts Down Over "Unmasking Antifa" Act | Zero Hedge [Last Updated On: December 19th, 2018] [Originally Added On: December 19th, 2018]
- KNOPPIX 7.4.2 / ADRIANE 1.7 - Live CD / DVD [Last Updated On: December 19th, 2018] [Originally Added On: December 19th, 2018]
- Jacobs Email - IT - Teamwork at Jacobs University [Last Updated On: January 4th, 2019] [Originally Added On: January 4th, 2019]
- KNOPPIX 7.7.1 - Linux Live System [Last Updated On: January 4th, 2019] [Originally Added On: January 4th, 2019]
- KNOPPIX 8.1.0 - Linux Live System - knopper.net [Last Updated On: March 5th, 2019] [Originally Added On: March 5th, 2019]
- The Politically Incorrect Australian [Last Updated On: April 22nd, 2019] [Originally Added On: April 22nd, 2019]
- Lionel Nation - YouTube [Last Updated On: April 22nd, 2019] [Originally Added On: April 22nd, 2019]
- Andrew Yang Rejects Support From White Nationalists ... [Last Updated On: May 1st, 2019] [Originally Added On: May 1st, 2019]
- AltLeft.com "The Left of the AltRight" [Last Updated On: May 2nd, 2019] [Originally Added On: May 2nd, 2019]
- Automate the Boring Stuff with Python [Last Updated On: June 3rd, 2019] [Originally Added On: June 3rd, 2019]
- UI Events KeyboardEvent code Values - World Wide Web ... [Last Updated On: June 3rd, 2019] [Originally Added On: June 3rd, 2019]
- Dont like the facts? Just Schiff the narrative - Boston Herald [Last Updated On: October 1st, 2019] [Originally Added On: October 1st, 2019]
- Netflix vows crackdown on users who share logins with pals or family and could make you pay EXTRA - The Sun [Last Updated On: October 24th, 2019] [Originally Added On: October 24th, 2019]
- Everything Paradox Interactive Announced At PDXCON 2019 - Kotaku Australia [Last Updated On: October 24th, 2019] [Originally Added On: October 24th, 2019]
- The psychology and physiology of propaganda: A study of the radicalization of women - University of Virginia The Cavalier Daily [Last Updated On: January 30th, 2020] [Originally Added On: January 30th, 2020]
- One of the Vail Valley's fur-baby favorites, Dr. Julie Alt, is calling it a career - Vail Daily News [Last Updated On: January 30th, 2020] [Originally Added On: January 30th, 2020]
- Netflix FINALLY lets you turn those annoying autoplaying trailers off heres how - The Sun [Last Updated On: February 12th, 2020] [Originally Added On: February 12th, 2020]
- Netflix FORCED to remove nine movies after government objections including boozy Jesus party film - The Sun [Last Updated On: February 12th, 2020] [Originally Added On: February 12th, 2020]
- Netflix trialling 2.99 cheapest ever plan but it only works on smartphones or tablets - The Sun [Last Updated On: February 12th, 2020] [Originally Added On: February 12th, 2020]
- Netflix offers first-ever FREE movie that you dont need account to watch - The Sun [Last Updated On: February 12th, 2020] [Originally Added On: February 12th, 2020]
- The Death Throes of the Failed Sandernista Revolution - Real Context News [Last Updated On: March 16th, 2020] [Originally Added On: March 16th, 2020]
- Life under lockdown: Italy is being consumed by panic - Spectator.co.uk [Last Updated On: March 16th, 2020] [Originally Added On: March 16th, 2020]
- Netflix is testing cheap new mobile-only plan that costs just 2.40 a month - The Sun [Last Updated On: March 16th, 2020] [Originally Added On: March 16th, 2020]
- Howie Carr: Its time to wake up sleepy Joe Biden - Boston Herald [Last Updated On: March 16th, 2020] [Originally Added On: March 16th, 2020]
- Living in an Italy consumed by fear - The Spectator USA [Last Updated On: March 27th, 2020] [Originally Added On: March 27th, 2020]
- Hungarian Press Roundup: Weeklies on the State of Emergency - Hungary Today [Last Updated On: March 27th, 2020] [Originally Added On: March 27th, 2020]
- Essential Mac keyboard shortcuts and key combinations - Macworld UK [Last Updated On: May 3rd, 2020] [Originally Added On: May 3rd, 2020]
- AltLeft Shuts Down Toronto Comedy Show Over Words 'Free ... [Last Updated On: May 3rd, 2020] [Originally Added On: May 3rd, 2020]
- COVID-19 UPDATE: East Hampton Testing Site To Open May 15 - 27east.com [Last Updated On: May 9th, 2020] [Originally Added On: May 9th, 2020]
- Conservative Jews: Do you care about Israel? Then vote Trump out - Forward [Last Updated On: June 21st, 2020] [Originally Added On: June 21st, 2020]
- As shul reopens, can we make it more welcoming to Orthodox women? - Forward [Last Updated On: June 22nd, 2020] [Originally Added On: June 22nd, 2020]
- End the Civil War Before It Starts: Vote Engel and Schleifer - The Jewish Press - JewishPress.com [Last Updated On: June 22nd, 2020] [Originally Added On: June 22nd, 2020]
- The Rising Blood Tide of The Alt-Left & the Real Threat of Antifa - AmmoLand Shooting Sports News [Last Updated On: July 12th, 2020] [Originally Added On: July 12th, 2020]
- The Shot Heard Roundand Round and Roundthe World - The Dispatch [Last Updated On: July 12th, 2020] [Originally Added On: July 12th, 2020]
- Netflix launches shuffle button that plays a random show for viewers who dont know what to watch - The Sun [Last Updated On: July 23rd, 2020] [Originally Added On: July 23rd, 2020]
- Why the Alt-Left Is a Problem, Too | Vanity Fair [Last Updated On: July 23rd, 2020] [Originally Added On: July 23rd, 2020]
- What Is Alt-Left? Here's What You Really Need to Know [Last Updated On: July 23rd, 2020] [Originally Added On: July 23rd, 2020]
- What Is the 'Alt-Left'? For Starters, Not a Thing | WIRED [Last Updated On: July 23rd, 2020] [Originally Added On: July 23rd, 2020]
- Howie Carr: Alt-left media still cling to their Big (Russia) Lie - Boston Herald [Last Updated On: July 28th, 2020] [Originally Added On: July 28th, 2020]
- McCarthyite witch-hunt over alleged anti-Semitism explodes in British Labour Party - WSWS [Last Updated On: August 3rd, 2020] [Originally Added On: August 3rd, 2020]
- Antifa Berkeley: Why the 'Alt-Left' Is a Problem | Time [Last Updated On: August 7th, 2020] [Originally Added On: August 7th, 2020]
- Alt-Right, Alt-Left, Antifa: A Glossary of Extremist ... [Last Updated On: August 7th, 2020] [Originally Added On: August 7th, 2020]
- What is the "alt-left" Trump was talking about? - CBS News [Last Updated On: August 7th, 2020] [Originally Added On: August 7th, 2020]
- Is There an 'Alt-Left'? - Snopes.com [Last Updated On: August 7th, 2020] [Originally Added On: August 7th, 2020]
- Say Goodbye to IITs, IIMs Et Al? NEP 2020 Will Convert Indias Top Institutions Into JNU Copies - Swarajya [Last Updated On: August 10th, 2020] [Originally Added On: August 10th, 2020]
- Checking In With the Newsletter Economy - New York Magazine [Last Updated On: September 2nd, 2020] [Originally Added On: September 2nd, 2020]
- Resident Evil survival horror TV series is coming to Netflix and fans think its the next Stranger Things - The Sun [Last Updated On: September 2nd, 2020] [Originally Added On: September 2nd, 2020]
- With Republicans like these, who needs Ds? - The Highland County Press [Last Updated On: September 2nd, 2020] [Originally Added On: September 2nd, 2020]
- Here's Why You Should Never Use the Term "Alt-Left" - POPSUGAR [Last Updated On: September 8th, 2020] [Originally Added On: September 8th, 2020]
- [OPINION] Law and order president? The chaos and violence Trump wants you to be fearful and hateful of is happening now under his watch - Asian... [Last Updated On: September 8th, 2020] [Originally Added On: September 8th, 2020]
- Hungarian Press Roundup: No Compromise in Sight over Theatre and Film University - Hungary Today [Last Updated On: September 8th, 2020] [Originally Added On: September 8th, 2020]
- Slow Joe Biden is a few fries short of a Happy Meal: Howie Carr - Boston Herald [Last Updated On: September 8th, 2020] [Originally Added On: September 8th, 2020]
- Netflix launches its first TV channel that streams shows just like regular telly - The Sun [Last Updated On: November 13th, 2020] [Originally Added On: November 13th, 2020]
- Bellingcat's Eliot Higgins on messaging Alexei Navalny to say 'we know who tried to kill you' - iNews [Last Updated On: February 4th, 2021] [Originally Added On: February 4th, 2021]
- Hungarian Press Roundup: Two Opposing Views on the Culture War - Hungary Today [Last Updated On: February 7th, 2021] [Originally Added On: February 7th, 2021]
- Sample Macros - Jitbit [Last Updated On: November 29th, 2021] [Originally Added On: November 29th, 2021]
- Mario Draghis fall and the death of Italian left-wing populism - The Spectator [Last Updated On: July 27th, 2022] [Originally Added On: July 27th, 2022]
- I tracked down my Corbynite troll and now he's in prison - The Telegraph [Last Updated On: July 27th, 2022] [Originally Added On: July 27th, 2022]
- Helping Ukraine Not Provoking Russia But a Proper Response to Years of Global Russian Aggression - smallwarsjournal [Last Updated On: July 27th, 2022] [Originally Added On: July 27th, 2022]
- US Gov to Crack Down on "Bossware" That Spies On Employees' Computers [Last Updated On: November 4th, 2022] [Originally Added On: November 4th, 2022]
- Chinese Spaceplane Releases Mystery Object Into Orbit [Last Updated On: November 4th, 2022] [Originally Added On: November 4th, 2022]
- Hackers Just Took Down One of the World's Most Advanced Telescopes [Last Updated On: November 4th, 2022] [Originally Added On: November 4th, 2022]
- AOC Says Her Twitter Account Broke After She Made Fun of Elon Musk [Last Updated On: November 4th, 2022] [Originally Added On: November 4th, 2022]
- Huge Drone Swarm to Form Giant Advertisement Over NYC Skyline [Last Updated On: November 4th, 2022] [Originally Added On: November 4th, 2022]
- That "Research" About How Smartphones Are Causing Deformed Human Bodies Is SEO Spam, You Idiots [Last Updated On: November 4th, 2022] [Originally Added On: November 4th, 2022]
- Jeff Bezos' Housekeeper Says She Had to Climb Out the Window to Use the Bathroom [Last Updated On: November 4th, 2022] [Originally Added On: November 4th, 2022]
- Twitter Working on Plan to Charge Users to Watch Videos [Last Updated On: November 4th, 2022] [Originally Added On: November 4th, 2022]
- Scientists Use Actual Lunar Soil Sample to Create Rocket Fuel [Last Updated On: November 4th, 2022] [Originally Added On: November 4th, 2022]
- NASA Sets Launch Date for Mission to $10 Quintillion Asteroid [Last Updated On: November 4th, 2022] [Originally Added On: November 4th, 2022]
- Elon Musk Meeting With Advertisers, Begging Them Not to Leave Twitter [Last Updated On: November 4th, 2022] [Originally Added On: November 4th, 2022]