How to Automate GUI in Python with PyAutoGUI – In this Python article iam going to show you How to Automate GUI in Python with PyAutoGUI. Knowing various python modules for editing spreadsheets, downloading files, and launching programs is useful, but some times there just aren’t any modules for the applications you need to work with. The ultimate tools for automating tasks on your computer are programs you write that directly control the keyboard and mouse. These programs can control other applications by sending them virtual keystrokes and mouse clicks, just as if you were sitting at your computer and interacting with the applications yourself. This technique is known as graphical user interface automation, or GUI automation for short. With GUI automation, your programs can do anything that a human user sitting at the computer can do, except spill coffee on the keyboard. Think of GUI automation as programming a robotic arm. You can program the robotic arm to type at your keyboard and move your mouse for you. This technique is particularly useful for tasks that involve a lot of mindless clicking or filling out of forms.
So in this article we are going to use PyAutoGUI library, first of all whats is PyAutoGUI.
What is PyAutoGUI ?
The purpose of PyAutoGUI is to provide a cross-platform Python module for GUI automation for human beings. The API is designed to be as simple as possible with sensible defaults. PyAutoGUI can simulate moving the mouse, clicking the mouse, dragging with the mouse, pressing keys, pressing and holding keys, and pressing keyboard hotkey combinations.
Installation
You can simply install PyAutoGUI with pip like this pip install pyautogui . Full documentation available at https://pyautogui.readthedocs.org .
So now this is the simple code for moving of our mouse a little bit .
1 2 3 4 5 6 7 |
import pyautogui screenWidth, screenHeight = pyautogui.size() pyautogui.moveTo(screenWidth / 2, screenHeight / 2) print(pyautogui.size()) |
OK in the above code first we have imported PyAutoGUI and after that we have found the screenWidth and screenHeght of our window by using pyautogui.size and after that we have moved our mouse by using pyautogui.move() function. pyautogui.size() returns (1920, 1080) on a computer with a 1920×1080 resolution; depending on your screen’s resolution, your return value may be different. You can store the width and height from pyautogui.size() in variables like width and height for better readability in your programs.
Moving the Mouse
Now that you understand screen coordinates, let’s move the mouse. The pyautogui.moveTo() function will instantly move the mouse cursor to a specified position on the screen. Integer values for the x- and y-coordinates make up
the function’s first and second arguments, respectively. An optional duration integer or float keyword argument specifies the number of seconds it should take to move the mouse to the destination. If you leave it out, the default is 0 for instantaneous movement. (All of the duration keyword arguments in PyAutoGUI functions are optional.)
1 2 3 4 5 6 7 |
import pyautogui for i in range(10): pyautogui.moveTo(100, 100, duration=0.25) pyautogui.moveTo(200, 100, duration=0.25) pyautogui.moveTo(200, 200, duration=0.25) pyautogui.moveTo(100, 200, duration=0.25) |
This example moves the mouse cursor clockwise in a square pattern among the four coordinates provided a total of ten times. Each movement takes a quarter of a second, as specified by the duration=0.25 keyword argument. If you hadn’t passed a third argument to any of the pyautogui.moveTo() calls, the mouse cursor would have instantly teleported from point to point. The pyautogui.moveRel() function moves the mouse cursor relative to
its current position. The following example moves the mouse in the same square pattern, except it begins the square from wherever the mouse hap pens to be on the screen when the code starts running:
1 2 3 4 5 6 7 |
import pyautogui for i in range(10): pyautogui.moveRel(100, 0, duration=0.25) pyautogui.moveRel(0, 100, duration=0.25) pyautogui.moveRel(-100, 0, duration=0.25) pyautogui.moveRel(0, -100, duration=0.25) |
pyautogui.moveRel() also takes three arguments: how many pixels to move horizontally to the right, how many pixels to move vertically downward, and (optionally) how long it should take to complete the movement.
A negative integer for the first or second argument will cause the mouse to move left or upward, respectively.
Also you can watch the complete video for How to Automate GUI in Python with PyAutoGUI
Subscribe and Get Free Video Courses & Articles in your Email