PyautoGUI: Three Great Uses

BaldGuyDIY
2 Jan 202112:12

Summary

TLDRIn this 'Bald Guy DIY' tutorial, viewers are introduced to three innovative ways to utilize Python's 'Pi Auto GUI' library for computer automation. The host demonstrates how to automate mouse clicks, keyboard inputs, and screen captures, showcasing the library's capabilities to simplify repetitive online tasks. By following along with the script, viewers can learn to automate web interface interactions, such as navigating YouTube, with precision and ease, all through a user-friendly Python interface.

Takeaways

  • 😀 The video introduces three cool things you can do with the 'Pi Auto GUI' library in Python to automate computer tasks.
  • 📚 The speaker has been using Python for about a year and a half and finds it an incredible experience due to its ease of learning and vast library ecosystem.
  • 🛠️ Python can be used for various tasks including spreadsheet automation, time calculations, web scraping, GUI development, and even controlling microcontrollers.
  • 🔍 The 'Pi Auto GUI' library allows you to control the computer using keyboard and mouse inputs for automation.
  • 💻 The demonstration uses VS Code and assumes Python is already installed; the library can be installed via 'pip install pyautogui'.
  • 🖱️ The first script shown finds the location of the mouse cursor, which is essential for clicking actions in automation scripts.
  • 🔒 To use mouse click functionality on a Mac, you may need to grant permissions to the script through system preferences.
  • ⏱️ Adding a delay in scripts can be necessary to allow time for switching between windows or waiting for web pages to load.
  • 🔑 The 'pyautogui.write' function can be used to simulate keyboard input, such as typing in a search bar.
  • 🔑 The 'pyautogui.press' function can emulate key presses, including special keys like 'Enter'.
  • 📸 'Pi Auto GUI' also includes screenshot functionality, allowing you to capture and work with images of the screen.
  • 🔍 The library can locate the center of an image on the screen and perform actions like clicking based on that reference.

Q & A

  • What is the main focus of the 'Bald Guy DIY' video?

    -The video focuses on demonstrating three cool things that can be done with the 'Pi Auto GUI' library in Python to automate various tasks on a computer.

  • How long has the presenter been experimenting with Python as a coding language?

    -The presenter has been experimenting with Python for about a year and a half.

  • Why is Python considered an incredible experience according to the presenter?

    -Python is considered incredible because it is easy to learn, especially for those with coding background, and it has a vast number of libraries that can be used for various tasks with little experience.

  • What are some of the tasks the presenter has automated using Python?

    -The presenter has automated tasks such as spreadsheets, time calculations, web scraping, graphical user interfaces, and controlling microcontrollers.

  • What is the first script the presenter shows in the video for?

    -The first script is to find the location of the mouse cursor, which is necessary for clicking and double-clicking actions in automation.

  • Why is it necessary to install 'Pi ObjC Core Module' when running the first script?

    -The 'Pi ObjC Core Module' is a dependency required for 'Pi Auto GUI' to function properly, especially for accessing mouse coordinates.

  • What is the solution to the error encountered when trying to install the 'Pi ObjC Core Module'?

    -The solution is to follow the instructions provided in the documentation for the respective operating system, which includes running a specific command in the terminal.

  • How does the presenter handle the permission issue to click the mouse from an automated script on a Mac?

    -The presenter goes to System Preferences, unlocks the settings, grants permission to VS Code for mouse click functionality, and then locks the settings again.

  • What function is used in 'Pi Auto GUI' to simulate typing text on the screen?

    -The 'piautogui.write' function is used to simulate typing text on the screen.

  • What is the purpose of adding a delay (using 'sleep' function) in the script?

    -The delay is added to give the user enough time to switch over to the desired window or page before the script performs the mouse click action.

  • How can 'Pi Auto GUI' be used to take a screenshot of the screen?

    -The 'piautogui.screenshot' function is used, where the user specifies a file name for the screenshot image to be saved.

  • What additional step is required to use the screenshot functionality on a Mac?

    -On a Mac, the user needs to grant permission to VS Code to take screenshots, which is done through the System Preferences.

  • How does the presenter locate the center of an image on the screen using 'Pi Auto GUI'?

    -The presenter uses 'piautogui.locateCenterOnScreen' function to find the center of a specified image on the screen.

  • What is the presenter's suggestion for using the 'Pi Auto GUI' library?

    -The presenter suggests exploring the library, looking through the documentation, and using the provided functions to automate simple tasks or create more personalized tasks.

  • How can viewers provide feedback or suggest content for future videos?

    -Viewers can give the video a thumbs up, subscribe to the channel, send an email to the presenter, or leave a comment with their suggestions.

Outlines

00:00

😀 Introduction to Python Automation with Pi Auto GUI

The speaker introduces the video by welcoming viewers to 'Bald Guy DIY' and expressing enthusiasm for Python as a coding language, highlighting its ease of learning and the vast array of libraries available for various tasks. The speaker has been experimenting with Python for about a year and a half, automating spreadsheets, performing time calculations, web scraping, and creating graphical user interfaces. They emphasize the continuous growth of Python's capabilities due to new libraries and the potential for community contributions. The video's focus is on demonstrating three cool automation tasks using the 'Pi Auto GUI' library, which allows for computer control via keyboard and mouse inputs. The speaker uses VS Code for demonstration and guides viewers through installing 'Pi Auto GUI', troubleshooting installation issues, and showcasing basic mouse position tracking.

05:01

🛠️ Automating Mouse Clicks and Keyboard Inputs

The speaker proceeds to demonstrate how to automate mouse clicks using 'Pi Auto GUI' by first obtaining the mouse's current coordinates and then using these to execute a click at a specific location. They encounter a permission issue on a Mac, which is resolved by adjusting system preferences to allow script control of the mouse. The video also covers adding delays to accommodate window-switching and using the 'write' function to simulate keyboard input into fields like the YouTube search bar. The speaker further explains how to press keys programmatically, such as the 'enter' key, to complete actions like submitting a search query. They encourage viewers to consult the 'Pi Auto GUI' documentation for a comprehensive list of functions and parameters, noting that most tasks rely on specifying x and y coordinates for mouse actions.

10:02

📸 Utilizing Screenshot and Image Recognition Features

In the final part of the video, the speaker explores the screenshot functionality of 'Pi Auto GUI', which allows users to capture and save images of their screen. They discuss the necessity of granting permissions on a Mac to perform this action and demonstrate taking a screenshot of the search results for 'Bald Guy DIY'. The speaker then introduces the concept of using image recognition to automate clicks on specific elements within a screenshot, such as a logo. They guide viewers through cropping an image to focus on a particular element, saving it, and then using 'Pi Auto GUI' to locate and click on that element within the screenshot. The video concludes with the speaker encouraging viewers to explore the library's documentation, experiment with its features, and apply these automation techniques to their own projects. They invite feedback and suggestions for future video topics and remind viewers to subscribe for weekly DIY content.

Mindmap

Keywords

💡Python

Python is a high-level, interpreted programming language known for its readability and efficiency. In the context of the video, it is the coding language used for automation tasks. The script mentions that Python is 'pretty easy to learn' and has a vast library ecosystem, which makes it suitable for beginners and experienced programmers alike.

💡Automation

Automation refers to the process of making a task or system operate automatically. The video's theme revolves around automating computer tasks using Python. Examples include automating web interfaces, spreadsheets, and even controlling microcontrollers with Python scripts, showcasing the broad applications of automation.

💡Pi Auto Gui

Pi Auto Gui is a Python library specifically mentioned in the script for automating GUI interactions. It allows the control of a computer through keyboard and mouse inputs programmatically. The video demonstrates using Pi Auto Gui to automate tasks such as clicking, typing, and taking screenshots.

💡Mouse Cursor

The mouse cursor's location is a critical aspect of GUI automation. The video script explains how to find the cursor's position using Pi Auto Gui, which is essential for tasks like clicking at specific coordinates on the screen.

💡Coordinates

In the context of GUI automation, coordinates refer to the x and y positions on the screen. The script uses the term to describe how to determine the location for mouse clicks, which is fundamental for automating interactions with specific elements on a webpage or application.

💡Script

A script in this video refers to a Python program created to automate tasks. The script is used to demonstrate how to automate mouse clicks, keyboard inputs, and other GUI interactions using Pi Auto Gui.

💡Permissions

Permissions in the script refer to the access rights required for a script to perform certain actions, such as clicking the mouse or taking screenshots. The video explains how to grant these permissions to the Python script in order to execute automation tasks successfully.

💡Delay

Delay in the script is used to introduce a pause in the execution of the automation script. It is implemented to allow the user time to switch windows or for the webpage to load before the script proceeds with the next action, such as clicking the mouse.

💡Screenshot

A screenshot in the video is a digital image captured by the script using Pi Auto Gui. It is used to capture the current state of the screen or specific elements for further processing or analysis, such as locating a particular logo for automated clicking.

💡Documentation

Documentation in the script refers to the official guide or reference material for Pi Auto Gui. It is mentioned as a resource for understanding the different functions and parameters available for use in automation scripts, which is crucial for expanding the capabilities of the automation tasks.

💡Hotkey

A hotkey is a keyboard shortcut that performs a specific action when pressed. The script mentions the 'press' function of Pi Auto Gui, which can emulate hotkeys, such as pressing 'Enter' after typing a search term, to enhance the automation process.

Highlights

Introduction to using 'pi auto gui' in Python for automating computer tasks.

Python's ease of learning and vast library support make it ideal for beginners and experienced coders alike.

Demonstration of automating spreadsheets, time calculations, web scraping, and GUIs using Python.

The potential to control microcontrollers with Python through platforms like Raspberry Pi or Micro Python.

Explanation of how to install 'pi auto gui' using pip in a Python environment.

The necessity of installing additional libraries for 'pi auto gui' on a Mac.

Creating a script to find the location of the mouse cursor using 'pi auto gui'.

How to handle errors related to permissions for mouse click automation on a Mac.

Demonstration of clicking at specific coordinates on the screen using 'pi auto gui'.

Use of the 'sleep' function to delay actions and allow for window switching.

Writing text into input fields using 'pi auto gui' and handling keyboard controls.

Pressing keys programmatically with 'pi auto gui' to enhance automation tasks.

Using 'pi auto gui' to take screenshots and save them as image files.

Handling permissions for screenshot functionality on different operating systems.

Using image recognition to automate clicking on specific UI elements.

Combining text input, key presses, and image recognition for complex automation tasks.

Encouragement to explore 'pi auto gui' documentation for a wide range of automation capabilities.

Invitation for viewers to subscribe and provide feedback for future video content.

Transcripts

play00:00

welcome to bald guy diy in this video

play00:02

i'm going to show you three cool things

play00:04

that you can do with pi

play00:05

auto gui in python in order to automate

play00:08

a lot of tasks

play00:09

on your computer

play00:11

[Music]

play00:12

[Applause]

play00:15

it's been about a year and a half now

play00:17

since i started playing with python as a

play00:19

coding language

play00:20

and i must tell you it is a really

play00:23

incredible experience

play00:25

not only because python is pretty easy

play00:26

to learn if you have any coding

play00:28

background in any other language or

play00:30

even if you're just starting fresh but

play00:32

it's also incredible how many libraries

play00:34

exist for it and different things that

play00:35

you can start playing with

play00:37

very quickly without having a lot of

play00:39

experience in them i've done all sorts

play00:41

of things

play00:41

including automation of spreadsheets

play00:45

different time calculations web scraping

play00:48

i've used it for graphical user

play00:51

interfaces as you've seen some of my

play00:53

videos before

play00:54

and even putting them out to things like

play00:55

the raspberry pi or even micro python

play00:58

to control microcontrollers as well so

play01:00

there's just an incredible amount of

play01:02

things that you can do

play01:04

and the list is always growing because

play01:06

there's always new libraries being

play01:08

created

play01:09

if there isn't something already

play01:10

existing for python there probably is

play01:12

something already in development

play01:14

or with a little bit of time and effort

play01:16

you could create your own modules

play01:18

that could benefit the whole community

play01:20

one of the things that i like to

play01:22

automate

play01:22

is web interfaces to get through some of

play01:25

those login processes

play01:27

and functionalities that you do over and

play01:29

over again in the same website

play01:31

in this video i'm going to show you

play01:33

three different things that are pretty

play01:34

cool that you can do with a library

play01:36

called

play01:36

pi auto gui which lets you control your

play01:39

computer the way that you would through

play01:40

a keyboard or mouse

play01:42

in order to automate a lot of processes

play01:44

so without further ado

play01:46

let's get into it for the sake of this

play01:48

demonstration i'm using vs

play01:50

code and i'm assuming you already have

play01:51

python installed you're just going to

play01:53

need to do pip

play01:54

install pi auto gui and let that run

play01:57

through the installation process

play01:58

i'm using a mac to demonstrate here

play02:00

today but on a windows machine it's even

play02:02

easier because there's not usually any

play02:04

other additional libraries you need to

play02:06

install

play02:06

on the first script i'm going to show

play02:08

you how to find the location of your

play02:10

mouse

play02:10

cursor because you're going to need to

play02:12

be able to do that in order to

play02:14

click and double click so as we look

play02:15

through mouse functions i'm just going

play02:17

to create a little true loop

play02:19

after importing piyo gui and in that

play02:21

true loop i'm going to print

play02:23

pi auto gui dot position which is the

play02:26

location

play02:27

of the mouse at any given time now by

play02:29

using the true loop it's going to

play02:31

constantly refresh

play02:32

and constantly print an updated position

play02:35

when i try to run that you see here it

play02:37

says you must first install the pi

play02:39

objc core module and if you try to do

play02:43

that from the terminal prompt here

play02:44

you will find there's some problems with

play02:46

it it says that it's already installed

play02:48

but yet it'll still give you the same

play02:50

error over and over again

play02:52

so the solution is to click on the link

play02:54

there that it refers you to the

play02:55

documentation

play02:57

and then scroll down here there's

play02:58

instructions that can be used for

play03:00

windows

play03:01

and linux and also mac os which is what

play03:03

i'm using here so i'm going to find the

play03:05

command that it

play03:06

suggests here and going to copy that

play03:08

over and open a terminal window

play03:10

and then just paste it in there

play03:15

and as you can see again it's going to

play03:17

run through the installation process

play03:19

and there's quite a bit pyro gui is

play03:21

doing a lot of

play03:23

conversion to other commands and so it

play03:25

does take a

play03:26

minute or so to install and then when we

play03:28

run it again

play03:29

now you can see in this bottom left hand

play03:31

corner that as i move the mouse around

play03:33

you get new x and y coordinates as you

play03:36

move it around the screen

play03:37

now this is good for the entire desktop

play03:39

area so depending on your resolution

play03:41

it will be different so now if i was to

play03:43

try to find

play03:44

a particular spot like a search box on

play03:47

youtube

play03:47

i'd simply have to put my mouse in that

play03:50

spot and then

play03:51

option tab over or on a windows machine

play03:54

alt tab over

play03:55

and you'd be able to see the current x

play03:56

and y coordinates of your mouse position

play04:04

now that we have those coordinates it's

play04:05

easy to create a little script that will

play04:07

actually click the mouse at that

play04:09

location

play04:10

you will also need to stop that mouse

play04:13

position script from running because

play04:14

it'll just run forever because of the

play04:16

while true loop so you just need to

play04:18

click the kill button which is the trash

play04:20

can

play04:21

and then it will stop the script and let

play04:23

you know that you're back to the command

play04:24

prompt ready to run again

play04:26

on my new script i'm going to import pi

play04:28

auto gui again and now i'm just going to

play04:30

use pi auto gui dot click

play04:32

and put in the coordinates that we found

play04:34

now the coordinates we had was 6 13 and

play04:36

93

play04:36

but i'm just going to round them to the

play04:37

nearest 10 which should be just fine

play04:40

now if i was to test it out you can see

play04:42

that once again we get an error

play04:44

and that's telling us that we don't yet

play04:46

have the permission to click the mouse

play04:48

from an automated script so if you click

play04:50

the preferences and then click the lock

play04:52

here to unlock it

play04:53

on a mac you'll find that you'll now

play04:55

have access to give vs code permission

play04:58

in order to be able to use the mouse

play05:01

click function

play05:02

so after giving it the permission and

play05:04

then locking it up again i can close

play05:05

that

play05:06

re-run it

play05:12

and now you can see it does click at the

play05:15

top where the search bar was but as you

play05:17

can tell

play05:18

if i was trying to do this with the

play05:19

youtube page that i had previously

play05:21

loaded

play05:21

by the time i ran this file and then

play05:23

clicked it would be too late

play05:25

because it would have already moved the

play05:26

mouse to that position and clicked

play05:28

so i'm going to add some delay that

play05:29

gives me time to switch over the windows

play05:31

i'm simply going to from the time

play05:33

library import the sleep function

play05:35

and then i'm going to add five seconds

play05:36

of sleep which will be giving me enough

play05:38

time to switch over to the youtube page

play05:40

so if we run this again and then i do my

play05:42

command tab or on windows

play05:44

alt tab it gives it time to pop over and

play05:46

then click the mouse

play05:48

in the search bar as you can see there

play05:59

to do double click right click and lots

play06:02

of other little things so if you consult

play06:04

the documentation you'll find all of the

play06:06

different parameters that are required

play06:08

for those things

play06:09

mostly though they work on the same idea

play06:11

of x and y coordinates

play06:15

so let's add the ability to type

play06:17

something in that search bar

play06:18

well all we need to do is use the

play06:21

piotogui.write function

play06:22

and then within quotations put what we

play06:25

want to write out in a shameless plug

play06:26

for my channel let's just put in bald

play06:28

guy diy

play06:29

and then run it again and see what we

play06:32

get that's really all it takes to do

play06:34

keyboard control and on the mac you

play06:36

don't have to do anything extra for that

play06:38

it doesn't generate any error messages

play06:40

so we'll simply tab back to our youtube

play06:42

page

play06:42

and we'll let it click in the box it

play06:44

also types in our search term

play06:45

you can of course click it manually or

play06:47

you could find the location of the mouse

play06:49

on the search button and click into the

play06:52

same function we already explored

play06:53

now if you look through the

play06:54

documentation on the pi auto gui

play06:57

reference pages you'll see here a lot of

play07:00

different functions things that you can

play07:01

do

play07:02

there's a function called press which

play07:04

emulates pressing a key

play07:06

and if you choose the keyboard key you

play07:07

can see all of the keys that are

play07:08

available to be pressed

play07:10

and also what to call them in the case

play07:12

of the enter button if we want to be

play07:13

able to press

play07:14

enter after our search criteria it's

play07:16

just enter within quotes all lowercase

play07:26

to make the functionality of our search

play07:27

a little bit better i'm just going to do

play07:29

a pile of gui dot press

play07:30

and that's going to press the enter key

play07:32

when we're done

play07:36

now you can see it types in there and

play07:38

presses enter which brings us to the

play07:39

results

play07:40

of my channel there are a bunch of other

play07:42

things that you can do there's hotkey

play07:43

functionality so you can program

play07:45

control and shift together or control

play07:48

shift escape or

play07:49

as their as the example is here or even

play07:52

do other sequences where you would hold

play07:55

one key down

play07:56

press some other keys and then let that

play07:58

key up as you can see here

play08:00

that's using the key down function and

play08:02

then while holding that key down

play08:04

you're gonna do key presses and then at

play08:06

the end you'll do a key up which is

play08:08

simulating releasing

play08:10

that key the documentation gives you so

play08:12

many different cases

play08:13

you can find pretty much anything you'd

play08:15

need the last thing i want to show off

play08:16

is how you can use the screenshot

play08:18

functionality in order to take snapshots

play08:20

of your screen

play08:21

and then locate different elements of it

play08:24

so the first thing i'm going to do is

play08:25

the piotogui.screenshot function

play08:27

and then all you need to do is specify a

play08:30

file name

play08:31

and that's going to be what you want to

play08:32

call the screenshot image that you're

play08:34

taking

play08:35

in this case i'm just going to call it

play08:36

screen.png it's going to save in the

play08:38

same folder as your python

play08:40

script now you can see immediately it

play08:42

also gives you one more error message

play08:44

saying that you don't have the rights to

play08:45

do that yet

play08:46

so just like before we're going to

play08:48

select it and give permission for vs

play08:50

code to be able to do that

play08:51

on the mac that's required on windows

play08:54

it's not

play08:54

and then we're going to run it again

play08:56

after reloading vs code because it tells

play08:58

you there you need to quit it first

play09:00

after running that script you can see it

play09:02

creates a screenshot on my desktop

play09:04

called screen.png and now i can open

play09:07

that up and what i'm left with here is

play09:09

actually an

play09:09

image of what i had not the actual vs

play09:12

code interface

play09:13

this image can be edited so if we were

play09:16

to take our previous look here

play09:18

at our youtube page that we loaded and

play09:20

searched for bald guy diy

play09:22

we can now add a screenshot function

play09:24

here and

play09:25

save that image to a png file so i'm

play09:28

going to call it bald guy diy channel

play09:30

dot png

play09:32

i'm going to add a couple more seconds

play09:33

of sleep here in order to let the search

play09:36

results load after enter is pressed now

play09:38

if we run it

play09:39

and clear that up again we're going to

play09:41

see after five seconds

play09:42

it types in the name and then it does a

play09:45

search and then with the search results

play09:47

it creates a screenshot now if we were

play09:48

to open that in an image editor i'm

play09:50

using here

play09:52

which is a free software and then select

play09:54

the part of it that we want to recognize

play09:57

we can now give pyo auto gui an image to

play09:59

look for

play10:00

something to reference i'm going to crop

play10:02

the whole image to the selection that i

play10:04

made

play10:04

and then export just that as a new file

play10:08

which is going to be the clue that the

play10:10

software looks for

play10:11

here i'm going to just call it

play10:12

boldguylogo.png save it to the desktop

play10:14

as well

play10:18

and once the exports done i can now

play10:20

reference it in the code and it'll be

play10:22

able to find it

play10:23

in order to do this we're going to use

play10:24

something called pi auto gui dot

play10:26

locate center on screen which is going

play10:28

to load locate the center of this image

play10:30

and you create two new variables x and y

play10:33

and make them equal to the location of

play10:36

that logo once pi auto gui can find it

play10:39

it's going to save those values of the x

play10:42

and y

play10:42

and now all we need to do is use them

play10:45

with the click function

play10:47

in order to click on the location of

play10:48

that logo right in the middle now if we

play10:50

were to run it and load our youtube page

play10:52

again

play10:53

after clearing the box five seconds

play10:54

later of course it will type in

play10:56

the name of the channel it'll hit enter

play10:58

when the results load it now

play11:00

also clicks on the image which you can

play11:02

tell it worked because it loads my first

play11:04

video in queue

play11:07

there are so many other functions in the

play11:08

documentation but this is a great start

play11:10

and i hope you'll check it out so there

play11:13

you have

play11:14

three simple ideas but also incredibly

play11:17

powerful

play11:18

to automate your computer with pi auto

play11:21

gui

play11:21

as usual the ideas i've shared are

play11:24

building blocks so that you can take

play11:25

them

play11:26

and turn them into more creative and

play11:28

personalized tasks

play11:29

that you can do on your own computer so

play11:32

play with this library

play11:33

look through the documentation and see

play11:35

all of the features that are available

play11:36

and i'm sure you'll find some use for it

play11:38

on even simple little things that you

play11:40

need automated

play11:42

in the future if you like this kind of

play11:44

content please give the video a thumbs

play11:45

up

play11:46

and subscribe to the channel as i post a

play11:48

new video every weekend

play11:49

on all sorts of diy interests if you

play11:52

want to send me an email

play11:53

my information is in the description

play11:54

below or leave a comment

play11:56

tell me what kind of videos you'd like

play11:57

to see in the next weeks and months

play12:00

and i'll use them as inspiration for

play12:02

future content

play12:04

until next time in all your diy projects

play12:07

hopefully those that you can automate a

play12:09

little don't be afraid

play12:10

to be balder

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
Python AutomationPi Auto GUITask ManagementCoding TutorialDIY TechScripting TipsGUI ControlMouse AutomationKeyboard ControlWeb Scraping
هل تحتاج إلى تلخيص باللغة الإنجليزية؟