Outils pour utilisateurs

Outils du site


issue151:python

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.

En général, j'essaie de prévoir ce que je vais écrire environ un mois ou deux avant que l'article véritable soit à livrer à l'équipe du FCM. Cependant, ce mois-ci, je dois admettre que mon esprit refusait de se concentrer sur le choix du sujet. Ce n'était pas parce que je ne trouvais rien du tout. C'était parce que j'avais tellement de sujets entre lesquels choisir et que j'avais de la difficulté à leur donner du contenu. La date limite commençait à approcher de plus en plus et je n'arrivais toujours pas à décider quoi que ce soit. Un de mes bons amis m'a suggéré un article sur l'API du blog de Google et la manière de l'intégrer dans Python ; j'ai passé un peu de temps à travailler dessus, mais mon inspiration refusait de se concentrer pour m'aider. Ça m'a rapidement contrarié, comme pour toutes ces choses auxquelles j'essaie de donner vie.

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.

Les idées se bousculaient dans ma tête quand je suis allé me coucher hier soir et j'ai assez mal dormi, en fait. Je me suis réveillé avant que mon réveil ne sonne et j'ai eu beau tout essayer, je n'ai pas réussi à me rendormir. Encore plus contrarié, j'ai commencé ma journée, fait mon café, vérifié mes mails, regardé les nouvelles à la télévision et je me suis préparé pour une journée supplémentaire de déception, car je n'avais pas de sujet viable pour l'article.

J'ai repassé en revue dans ma tête la liste de tout ce que je devais ajouter dans mon catalogue des choses à faire de la journée. Nombreuses étaient les choses normales et prosaïques comme de décider ce que j'allais faire pour le dîner, qu'est-ce que j'allais essayer de faire comme exercices physiques aujourd'hui, et ainsi de suite. L'une des choses que je devais ajouter à ma liste était de travailler sur les tests d'une nouvelle publication de développement de Page. Certains d'entre vous savent que j'aide Don Rozenberg à tester les nouvelles versions de Page avant qu'il ne les diffuse. J'aime vraiment faire ces tests, car essayer d'imaginer ce qui se passe quand quelque chose va de travers est un véritable défi. Cependant, dernièrement, il m'a semblé que je négligeais quelques points quand je suis revenu en arrière pour tester une fonctionnalité plus ancienne, juste pour m'assurer que rien n'était cassé dans le processus de mise à jour.

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.

Mon esprit commençait à s'évader de ma liste « à-faire » pour essayer de penser à une façon de ne pas oublier les étapes de la fonctionnalité ancienne, améliorant mon efficacité non seulement pour moi, mais pour Don et les utilisateurs de Page. Mon esprit partit bien haut dans un brouillard dense et j'ai commencé à sentir les prémices d'une idée qui commençait à prendre forme dans ce grand espace vide que j'appelle mon cerveau. Quelque chose pour automatiser le processus de test des nouvelles pré-versions de Page ! OUI ! Mais, attendez. Comment pourrais-je faire cela ?

J'ai rapidement bu ma tasse de café et attrapé ma souris. J'ai lancé une recherche sur le Web pour une bibliothèque en Python pour l'automatisation des mouvements de la souris, les clics, double-clics, et consorts. Je savais qu'il devait y avoir quelque chose qui serait rapide à apprendre et que je pourrais utiliser comme base pour mon article. Et bien sûr, je l'ai trouvé.

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.

Parmi tous les autres liens, en est apparu un vers un chapitre d'un livre d'un de mes auteurs favoris de tous les temps, Al Sweigart. J'ai fait la critique de quelques-uns de ses livres pour le Full Circle Magazine. Il a écrit certains des meilleurs livres sur Python et quelques-uns d'entre eux peuvent être lus en ligne gratuitement. Parmi eux, un livre appelé « Automate The Boring Stuff With Python » (Automatiser les choses fastidieuses avec Python). Il est publié par No Starch Press, et le lien vers le livre est https://automatetheboringstuff.com/#toc. D'ailleurs, une nouvelle seconde édition du livre va sortir à peu près n'importe quand. Vous devriez VRAIMENT acheter ce livre ! De toute façon, le chapitre 18 du livre est intitulé « Controlling the Keyboard and Mouse with GUI Automation » (Piloter le clavier et la souris en automatisant l'interface graphique de l'utilisateur (GUI)) - https://automatetheboringstuff.com/chapter18/). Il parle exactement de ce que nous cherchons.

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

Je vais, comme je le fais souvent, vous distiller l'essentiel du chapitre de M. Sweigart et vous montrer comment utiliser les informations que j'en tire. Nous utiliserons la bibliothèque appelée « pyautogui ». Alors que le nom pourrait sembler un peu trompeur à première vue ; si vous lisez le chapitre, vous verrez qu'il correspond parfaitement à ce qu'il peut faire. Allons-y.

La première chose que nous devons faire est de l'installer ainsi que quelques dépendances. Je vais vous donner les indications telles qu'elles apparaissent dans le chapitre, et seulement celles pour Linux ; si vous voulez installer la bibliothèque sur Windows ou Mac, vous pourrez trouvez les instructions dans l'article. Ainsi, comme première étape, nous avons besoin d'utiliser pip pour installer la bibliothèque python3-xlib.

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

Ensuite, vos devez utiliser apt-get pour installer quelques paquets supplémentaires :

sudo apt-get install scrot python3-tk python3-dev

Scrot est un programme qu'utilise pyautogui pour prendre des copies d'écran. Vous le verrez à l'œuvre un peu tard dans cet article.

Enfin, vous pouvez installer pyautogui en utilisant pip :

pip3 install pyautogui

Si tout s'est bien déroulé, vous devriez voir l'invite classique :

Faisons quelques trucs amusants.

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

Maintenant que pyautogui est installée, voyons voir concrètement ce qu'elle peut faire, ou, comme on le dit aux États-Unis, « kick the tires and see what this puppy can do » (Je sais très bien qu'il sera difficile de le traduire en d'autres langues et vous prie de me pardonner d'avance.) Évidemment, nous voudrions utiliser le programme pour déplacer le curseur de la souris et voir ce qui se passe. Cependant, en premier, nous regarderons ce que dit PyAutoGui sur la taille de notre écran. Bien que nous puissions connaître ce que sont ces valeurs, assurons-nous que ce que nous savons colle avec ce que la bibliothèque pense et ajustons nos estimations en conséquence.

Dans le shell Python, saisissez

pyautogui.size()

Vous devriez voir revenir la réponse et elle devrait corespondre à la taille de votre écran. Dans mon cas, c'est :

Size(width=1920, height=1080) # Taille (largeur=1920, hauteur=1080)

Maintenant, nous pouvons affecter ces valeurs à des variables :

swidth, sheight = pyautogui.size()

print(f“Screen width: {swidth} and height {sheight}”)

Et python nous retourne :

Screen width: 1920 and height 1080 # Écran largeur 1920 et hauteur 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.

Avant d'aller plus loin, l'une des choses que M. Sweigart mentionne dès le début de l'article, c'est qu'il existe une manière de faire avorter le programme juste au cas où quelque chose se passe mal. Il dit « PyAutoGUI a aussi une fonctionnalité de sauvetage en cas de défaut. Le déplacement du curseur de la souris dans le coin en haut à gauche entraînera le déclenchement de l'exception pyautogui.FailSafeException. » C'est une excellente chose à savoir. Non seulement l'exception est activée quand le curseur est manuellement déplacé dans le coin en haut à gauche, mais la même chose arrive dans n'importe quel angle, et aussi par une demande du programme. Aussi, quand nous créerons notre première démonstration, nous devrons la prendre en considération. Nous commencerons en utilisant la méthode « moveTo() » de la bibliothèque. Pour cela, nous avons besoin de fournir les coordonnées x et y d'où nous voulons que le curseur de la souris aille se placer, ainsi que la durée. Je vais partir du principe que vous savez déjà que le coin en haut à gauche de votre écran est 0,0 et que le coin en bas à droite est width, height (ou dans mon cas, 1920, 1080). M Sweigart suggère une durée de 0,25 secondes, mais je voudrais voir l'action se dérouler un peu plus lentement ; aussi, j'utiliserai une durée de 0,75 secondes.

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.

pyautogui.moveTo(15, 15, duration = 0.75)

si vous me suivez dans le shell Python, vous verrez que votre curseur de souris décolle tout seul et file directement vers le coin en haut à gauche, s'arrêtant juste aux limites physiques.

Maintenant, déplaçons le curseur de la souris vers chacun des autres angles de l'écran, puis vers le centre. Souvenez-vous que les valeurs que j'utilise sont pour mon écran. Vous devrez les modifier pour les adapter au vôtre. Souvenez-vous aussi de retrancher quelques pixels dans chaque coin pour éviter de déclencher la procédure de sauvegarde.

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)

Assez épatant. Continuons pour voir ce que nous pouvons faire d'autre.

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.

Au-delà d'un simple mouvement de souris

Maintenant, faisons quelque chose d'encore plus amusant. J'ai un dossier sur mon Bureau qui s'appelle Cleanup (Ménage). Plus souvent que je ne veux l'admettre, je sauvegarde des choses sur mon bureau pour m'en souvenir. L'idée est que je dispose d'un rappel constant alors que je travaille sur une chose ou que je m'occupe de quelque chose. Un jour ou l'autre, je veux l'enlever du bureau et le placer ailleurs pour un usage ultérieur. Je néglige souvent de le déplacer et, dans un effort pour que ce soit plus propre et plus logique, j'ai créé le dossier « cleanup ».

Une des merveilleuses choses que fait PyAutoGUI, c'est qu'il vous autorise à envoyer des clics et des doubles-clics (ainsi que d'autres événements de la souris) vers une position x,y de l'écran. Mais comment dites-vous à PyAutoGUI où se trouve un élément précis, si vous ne connaissez pas les coordonnées x,y ? L'un des outils qu'on trouve dans PyAutoGUI donne la possibilité de lui fournir une image de quelque chose qui devrait être sur l'écran, pour qu'il trouve sa position.

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]

J'ai utilisé Shutter, un programme de capture d'écran pour Linux, pour récupérer un secteur de mon bureau et le sauver dans un fichier .png. Dans ce cas-là, c'était l'icône du dossier cleanup…

Puis, j'ai utilisé la méthode locateOnScreen pour me renvoyer la position x/y de l'icône du dossier sur l'écran.

bpos = pyautogui.locateOnScreen(“/home/greg/Desktop/poc/autogui/cleanup_folder.png”)

Notez aussi que j'ai donné le chemin complet vers le fichier.

En une seconde environ, j'ai eu la réponse. Si la méthode locateOnScreen trouve la cible, elle retourne ensuite les x, y, largeur et hauteur sur l'écran. Sinon, elle renvoie None (Aucun) :

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.

Ainsi, je sais que la position de l'icône du dossier commence à 83.456 et a un largeur de 69 et une hauteur de 72 pixels, ce qui est aussi la taille de l'image que j'ai utilisée.

Enfin, je dis à PyAutoGUI d'envoyer un double-clic à un emplacement à l'intérieur de la boîte englobante :

pyautogui.doubleClick(105,480)

Et le dossier s'ouvre exactement comme si j'avais déplacé manuellement la souris vers l'icône, puis double-cliqué dessus.

Ensuite, j'ai utilisé Shutter pour récupérer un secteur de la fenêtre du dossier qui lui était plutôt propre. J'ai constaté que je devrais utiliser une partie de la barre de titre. Je devais m'assurer que la fenêtre du dossier n'était pas surlignée, car je n'aurais pas pu être sûr que le dossier avait été mis en avant quand j'aurai demandé à PyAutoGUI de le trouver.

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”) »>

Ensuite, j'ai, à nouveau, utilisé locateOnScreen avec la nouvelle image :

bpos = pyautogui.locateOnScreen(“/home/greg/Desktop/poc/autogui/cusection.png”)
print(bpos)

Box(left=908, top=89, width=86, height=46)

Maintenant que je savais où se situe la barre de titre de la fenêtre, je voulais essayer de déplacer un peu la fenêtre en utilisant la méthode .drag. Bien sûr, j'avais besoin de déplacer le curseur de la souris un peu vers le bas, car, comme vous pouvez le voir dans l'image ci-dessus, j'ai capturé un peu de l'arrière-plan du bureau quand j'ai réalisé ma capture d'image :

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!

Et bien sûr, la fenêtre s'est déplacée lentement vers la droite. Pour faciliter à coup sûr un ensemble d'actions ininterrompu, j'ai utilisé un point-virgule pour séparer les deux déclarations puisque j'étais dans le shell interactif. Si (et seulement si) j'avais créé ceci dans un IDE, je me serais assuré que les deux déclarations étaient sur des lignes distinctes.

Ainsi, j'ai été capable d'apprendre rapidement les bases de PyAutoGUI (et de vous les enseigner) et de commencer à formuler les débuts de mon programme de tests automatiques. Je pourrai créer un script, dans un script Python, qui pourra créer un GUI Page vide, redimmensionner et déplacer la partie principale, ajouter des gadgets, paramétrer des attributs et plus encore.

Cet article avait pour objectif d'aiguiser votre appétit sur les possibilités que peut vous fournir PyAutoGUI. Il y a BEAUCOUP, BEAUCOUP d'autres choses faisables, beaucoup trop pour même les effleurer ici. Vous pouvez trouver la documentation complète de PyAutoGui sur https://pyautogui.readthedocs.io/en/latest/index.html

Jusqu'à la prochaine fois, continuez à coder !

issue151/python.txt · Dernière modification : 2019/12/07 16:05 de andre_domenech