Python GUI Automation with PyAutoGUI Library

In this Python article we want to learn about Python GUI Automation with PyAutoGUI Library,

so for this purpose we are going to use third party library that is called PyAutoGUI Library,

PyAutoGUI is a cross-platform GUI automation Python module for human beings. Used to

programmatically control the mouse & keyboard. by using GUI Automation we can create 

programs that can control other applications by sending  them virtual keystrokes and

mouse clicks. With GUI automation, your programs can do anything that a human user

sitting at the computer can do.

 

 

 

Installation

As i have already said that we are going to use PyAutoGUI library, so first of all you need to

install this library, you can simply use pip for the installation.

 

 

OK now let’s create a simple code for moving our mouse.

 

In the above code first we have imported our PyAutoGUI library 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.

 

 

 

Run the code and this is the result.

Python GUI Automation
Python GUI Automation

 

 

 

 

Let’s create another example and move our mouse in a range, so you can use this code.

 

Using this code we can move our mouse clock wise in a square manner. we have used the range function of python for this purpose, and we have used moveTo() function from PyAutoGUI for moving the mouse.

 

 

Sometimes you need to move your mouse relatively, for that you can use pyautogui.moveRel(). this function 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.

 

 

 

Getting Mouse Position 

OK now we are going to get our mouse position using Python GUI Automation, we can find the mouse’s current position by calling the pyautogui.position() function, which will return a tuple of the mouse cursor’s x and y positions at the time of the function call

 

 

 

 

Run the code and this is the result.

Python GUI Automation with PyAutoGUI Library
Python GUI Automation with PyAutoGUI Library

 

 

 

Now we are going to get the mouse coordinates and after that we want to print that.

 

 

If you run the code you will see your mouse coordinates like this.

Python GUI Automation with PyAutoGUI Library
Python GUI Automation with PyAutoGUI Library

 

 

 

How to Click the Mouse in PyAutoGUI

for sending a virtual mouse click to your computer, we can use pyautogui.click() method. By default, this click uses the left mouse button and takes place wherever the mouse cursor is currently located.

 

we can pass x and y coordinates of the click as optional first and second arguments if you want it to take place somewhere other than the mouse’s current position. If you want to specify which mouse button to use, include the button keyword argument, with a value of ‘left’, ‘middle’, or ‘right’. you can do it like this.

 

 

 

Dragging the Mouse 

Dragging means moving the mouse while holding down one of the mouse buttons. For example, you can move files between folders by dragging the folder icons, there are different methods in PyAutoGUI for dragging, we have  pyautogui.dragTo(),  pyautogui.dragRel(). these two functions drag the mouse cursor to new location or a location relative to its current one. 

 

OK now we are going to to do a drawing using paint application, you can add and run this code.

 

 

 

Open your paint application and run the code.

Python GUI Automation with PyAutoGUI Library
Python GUI Automation with PyAutoGUI Library

 

 

Leave a Comment