Outils pour utilisateurs

Outils du site


Ceci est une ancienne révision du document !

Usually, I try to know what I'm going to write about a month or two before the actual article is due to the FCM Staff. However, this month I have to admit, my mind refused to settle and concentrate on picking a topic. It wasn't that I couldn't come up with anything at all. It was that there were so many topics to choose from and I had a difficult time coming up with the substance for any one of them. The deadline started approaching closer and closer and still I couldn't get my mind to settle on anything in particular. A good friend suggested an article on the Google blog API and how to integrate that into Python, and I did spend some time working on that, but the muse just wouldn't settle in and help me out. I quickly became frustrated with that, just like everything that I tried to bring to life.

My mind was spinning when I went to bed last night, and I ended up not really sleeping well. I woke up before my alarm went off and as hard as I tried, I couldn't get back to sleep. Even more frustrated, I started my day, made my coffee, checked my email, tuned into the network news on the television, and settled in for another day of potential disappointment because I still didn't have a viable topic for this month's article. I went through all the mental list of all the things that needed to be added to my todo list for today. Many of them were normal mundane things like decide what I was going to make for dinner tonight, was I going to deal with trying to exercise today, and so on and so on. One of the things that needed to be added to my list was to work on testing the latest development release of Page. Some of you know that I help Don Rozenberg test out new versions of Page before he releases them into the “wild”. I really enjoy doing the testing for him, since it's a challenge for me to try to figure out what happened when something goes wrong. Lately however, I seem to be forgetting a few items when I go back and test older functionality, just to make sure that nothing got broken in the updating process.

My mind started wandering away from the todo list, and on to trying to think of a way to keep from missing the steps for older functionality, making my efficiency better for myself, Don and the users of Page. My mind started to cloud with a misty fog, and I started to get that feeling of the beginnings of an idea started to coalesce in that great empty space I refer to as my brain. Something to automate the testing process of new Page candidates! YES! But wait. How in the world would I do that? I quickly put down the coffee cup and grabbed my mouse. I called up a web search for a python library to automate mouse movements, clicks, double-clicks, and the like. I knew there had to be something that would be quick to learn and would be something that I could use as the basis for an article. Sure enough, I found it.

Amongst all the other links appeared a link to a chapter of a book by one of my all time favorite authors, Al Sweigart. I've reviewed a few of his books for Full Circle magazine. He's written some of the best books on Python, and has a few on the internet that you can read totally free on-line. One of them is a book called “Automate The Boring Stuff With Python”. It's published by No Starch Press, and the link to the book is https://automatetheboringstuff.com/#toc. By the way, he has a new Second Edition of the book coming out just about any time. You REALLY should buy this book! Anyway, chapter 18 of the book is titled “Controlling the Keyboard and Mouse with GUI Automation” (https://automatetheboringstuff.com/chapter18/). It deals with exactly what I was looking for.

I'm going to, as I often do, distill the essence of Mr. Sweigart's chapter down and show you how to use the information that I gleaned from it. We'll be using a library called 'pyautogui'. While the name might seem a bit misleading at first glance, if you read the chapter you'll see that it perfectly fits what it actually can do. Let's get started. The first thing we need to do is to install it and some dependencies. I'm going to give you the instructions as they appear in the chapter, and only the instructions for Linux, so if you want to install the library on Windows or Mac, you can find them in the article. So, step 1 is that you'll need to use pip to install the python3-xlib library. pip3 install python3-xlib

Next, you need to use apt-get to install some additional packages… sudo apt-get install scrot python3-tk python3-dev Scrot is a program that pyautogui uses to take screenshots. You'll see this in action later on in this article. Finally, you can install pyautogui using pip… pip3 install pyautogui Now, just for peace of mind that everything got installed correctly, fire up Python 3.x and in the interactive shell type… import pyautogui If everything went well, you should see the normal prompt »> Let's do some fun things

Now that we have the pyautogui installed, let's (as we say in the U.S.) “kick the tires and see what this puppy can do” (I'm aware, this might not translate well to other languages, so forgive me). Obviously we will want to use the program to move the mouse pointer around and see what happens. First thing, however, we'll want to see what PyAutoGui says about the size of our screen. While we might know what the values are, let's make sure that what we know matches what the library thinks, and adjust our expectations accordingly. In the Python shell, type pyautogui.size() You should see the response back and it should match your screen size. In my case it is… Size(width=1920, height=1080) Now we can get those values assigned to some variables… swidth, sheight = pyautogui.size() print(f“Screen width: {swidth} and height {sheight}”) And python returns with… Screen width: 1920 and height 1080

Before we go any further, one of the things that Mr. Sweigart mentions early in the article is that there is a way to cause the program to abort just in case something goes wrong. He says “PyAutoGUI also has a fail-safe feature. Moving the mouse cursor to the upper-left corner of the screen will cause PyAutoGUI to raise the pyautogui.FailSafeException exception.” This is a very good thing to know. Not only does the exception get raised when the mouse cursor gets moved to the upper-left corner of the screen manually, the same thing happens in any corner, as well as when our program causes it to happen. So when we create our first demonstration, we need to take this into consideration. We'll start by using the 'moveTo()' method of the library. For this, we need to provide the x and y coordinates we wish the mouse cursor to move to as well as a duration. I'm going to assume that you already know that the upper-left corner of your screen would be 0,0 and the lower-right corner would be width, height, or in my case 1920, 1080. Mr. Sweigart suggests a duration of 0.25 seconds, but I want to see it happen a bit more slowly, so I will use a duration of 0.75 seconds.

pyautogui.moveTo(15, 15, duration = 0.75) If you are following along with me in your python shell, you will see your mouse pointer take off on its own and head directly to the upper-left corner, stopping just short of the actual limits. Now, let's move the mouse pointer to each of the other screen corners and then to the center of the screen. Remember that the values I'm using are for my screen. You should modify them to fit yours. Also remember to take a few pixels off of each corner destination to avoid hitting the failsafe. »> pyautogui.moveTo(1905, 15, duration = 0.75) »> pyautogui.moveTo(1905, 1075, duration=0.75) »> pyautogui.moveTo(15, 1075, duration=0.75) »> pyautogui.moveTo(swidth/2, sheight/2, duration=0.75) Pretty neat stuff. Let’s keep going and see what else we can do.

Beyond simple mouse movement Now, let's do something a bit more fun. I have a folder on my Desktop called Cleanup. More times than I like to admit, I save things to my desktop as a memory tickler. The idea is to provide me a constant reminder while I work on something or deal with something. Eventually, I want to take it off the desktop and move it for later use. I often neglect to move it and in an effort to get things cleaner and more logical, I created the cleanup folder. One of the wonderful things that PyAutoGUI does is allow you to send click and double-click (as well as other mouse events) to an x,y position on the screen. But how do you tell PyAutoGUI where a particular item is, if you don't know the x,y coordinates? One of the tools that comes with PyAutoGUI is the ability to provide it an image of something that should be on your screen and have it find the location.

I used Shutter, a screen capture program for Linux, to grab a section of my desktop and save it as a .png file. In this case, it was the icon of the cleanup folder… Then I used the locateOnScreen method to return the x/y position on the screen of the folder icon. bpos = pyautogui.locateOnScreen(“/home/greg/Desktop/poc/autogui/cleanup_folder.png”) Notice that I need to give a fully qualified path to the file as well. In about a second, I got a response. If the locateOnScreen method finds the target, then it returns the x, y, width and height on the screen. If not, it returns None… print(bpos) [83, 456, 69, 72]

So I know that the location of the folder icon begins at 83,456 and has a width of 69 and height of 72 pixels, which is also the size of the image I used. Finally, I can tell PyAutoGUI to send a double click to a location within that bounding box. pyautogui.doubleClick(105,480) And the folder opened just like I had moved my mouse and double-clicked the icon by hand. Next, I used Shutter again to grab a section of the folder window that would be fairly unique to it. I determined that I would need to use a portion of the title bar. I had to make sure that the folder window was not highlighted, since I couldn’t be sure the folder would be highlighted when I asked PyAutoGUI to find it.

Next, I use locateOnScreen again with the new image. »> bpos = pyautogui.locateOnScreen(“/home/greg/Desktop/poc/autogui/cusection.png”) »> print(bpos) Box(left=908, top=89, width=86, height=46) Now that I know where the window title bar is, I wanted to try to move the window some by using the .drag method. Of course, I needed to move the mouse cursor down a small amount, since, as you can see in the above image, I captured a bit of the desktop background when I made the image capture. »> pyautogui.moveTo(908,100, duration=0.75); pyautogui.drag(50,0,0.75,button=“left”) »>

And sure enough, the window slowly moved to the right. To be sure of an easy, uninterrupted set of actions, I used a semicolon to separate the two statements while I was within the interactive shell. If (and when) I create this in an IDE, I would make sure that the two statements are on separate lines. So, I was able to quickly learn (and teach you) the basics of PyAutoGUI and begin to formulate the beginnings of my auto-testing program. I will be able to create a script, within a Python script, that will be able to create a blank Page GUI, resize and move the main form, add widgets, set attributes, and more. This article was meant to whet your appetite to the possibilities that PyAutoGUI can provide you. There are MANY, MANY more things that can be done, far too many to even touch on here. You can find the full documentation for PyAutoGui at https://pyautogui.readthedocs.io/en/latest/index.html Until next time, keep coding!

issue151/python.1575292488.txt.gz · Dernière modification : 2019/12/02 14:14 de d52fr