issue176:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue176:python [2022/01/02 14:25] – créée auntiee | issue176:python [2022/01/06 15:28] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | It’s unusual for me to get questions more than once a month. However, today, while I was thinking about what I was going to do for my Python article for December, I got a question from a friend about my suggestions for doing automated testing of his program. Immediately, | + | **It’s unusual for me to get questions more than once a month. However, today, while I was thinking about what I was going to do for my Python article for December, I got a question from a friend about my suggestions for doing automated testing of his program. Immediately, |
- | Once things quieted down a bit around the old homestead, I realized that the Cosmic Muffin was trying to tell me something. So I started looking at the latest documentation for pyautogui and found that Al Sweigart has really updated things quite a bit. You can find that documentation at https:// | + | Once things quieted down a bit around the old homestead, I realized that the Cosmic Muffin was trying to tell me something. So I started looking at the latest documentation for pyautogui and found that Al Sweigart has really updated things quite a bit. You can find that documentation at https:// |
- | The first thing to do is to install or update pyautogui. Some of my friends and readers seem to have more than one version of Python (and pip) installed. So let’s use some simple commands from the terminal to determine the proper commands for your system. Open a terminal and type python -V and pip --version | + | Il est inhabituel pour moi de recevoir des questions plus d'une fois par mois. Cependant, aujourd' |
+ | |||
+ | Une fois que les choses se sont calmées un peu autour de la maison, j'ai réalisé que le Cosmic Muffin essayait de me dire quelque chose. J'ai donc commencé à consulter la dernière documentation de pyautogui et j'ai découvert qu'Al Sweigart avait vraiment mis les choses à jour. Vous pouvez trouver cette documentation à l' | ||
+ | |||
+ | |||
+ | **The first thing to do is to install or update pyautogui. Some of my friends and readers seem to have more than one version of Python (and pip) installed. So let’s use some simple commands from the terminal to determine the proper commands for your system. Open a terminal and type python -V and pip --version | ||
greg@earth: | greg@earth: | ||
Ligne 15: | Ligne 20: | ||
Python 3.8.10 | Python 3.8.10 | ||
greg@earth: | greg@earth: | ||
+ | pip 20.0.2 from / | ||
+ | |||
+ | La première chose à faire est d' | ||
+ | |||
+ | greg@earth: | ||
+ | Python 3.8.10 | ||
+ | greg@earth: | ||
pip 20.0.2 from / | pip 20.0.2 from / | ||
- | If the versions of python and pip are not the same, pick one of the versions and use the proper python and pip combination that you wish to install pyautogui into. Since mine are the same, I just used the pip command. | + | Cela vous donnera les versions actuelles de Python et de pip, qui devraient être les mêmes. Vous pouvez également essayer python3 -V et pip3 --version pour vérifier que les versions sont les mêmes. |
+ | |||
+ | greg@earth: | ||
+ | Python 3.8.10 | ||
+ | greg@earth: | ||
+ | pip 20.0.2 from / | ||
+ | |||
+ | |||
+ | **If the versions of python and pip are not the same, pick one of the versions and use the proper python and pip combination that you wish to install pyautogui into. Since mine are the same, I just used the pip command. | ||
Next page, on the right, is an abbreviated version of the terminal output from my system when I installed pyautogui on my system (your terminal output will probably be a bit different)... | Next page, on the right, is an abbreviated version of the terminal output from my system when I installed pyautogui on my system (your terminal output will probably be a bit different)... | ||
Ligne 25: | Ligne 45: | ||
$ sudo apt install scrot | $ sudo apt install scrot | ||
- | Scrot stands for SCReenshOT. If you are running a Windows system, you might have to use a different screenshot package. Pyautogui will tell you what else is needed. Assuming everything is installed correctly, we can move forward. | + | Scrot stands for SCReenshOT. If you are running a Windows system, you might have to use a different screenshot package. Pyautogui will tell you what else is needed. Assuming everything is installed correctly, we can move forward.** |
+ | |||
+ | Si les versions de python et pip ne sont pas les mêmes, choisissez l'une des versions et utilisez la bonne combinaison python et pip dans laquelle vous souhaitez installer pyautogui. Comme les miennes sont les mêmes, j'ai juste utilisé la commande pip. | ||
+ | |||
+ | Sur la page suivante, à droite, se trouve une version abrégée de la sortie du terminal de mon système lorsque j'y ai installé pyautogui (la sortie de votre terminal sera probablement un peu différente). | ||
+ | |||
+ | Maintenant que pyautogui est installé, vous devrez peut-être installer encore une chose. Si vous essayez d' | ||
+ | |||
+ | $ sudo apt install scrot | ||
+ | |||
+ | Scrot est l' | ||
- | With the terminal still open, try the following command… | + | **With the terminal still open, try the following command… |
$ python -m mouseinfo | $ python -m mouseinfo | ||
Ligne 38: | Ligne 69: | ||
Save the program as “test1.py”, | Save the program as “test1.py”, | ||
+ | |||
+ | sw, sh = pyautogui.size()** | ||
+ | |||
+ | Le terminal étant toujours ouvert, essayez la commande suivante : | ||
+ | |||
+ | $ python -m mouseinfo | ||
+ | |||
+ | Si tout fonctionne, vous devriez voir une fenêtre ouverte qui ressemble à quelque chose comme ceci : | ||
+ | |||
+ | Il s'agit d'un nouvel utilitaire fourni avec pyautogui qui rend l' | ||
+ | |||
+ | Maintenant, écrivons un programme simple pour déplacer la souris sur l' | ||
+ | |||
+ | Enregistrez le programme sous le nom de « test1.py », mais avant d' | ||
sw, sh = pyautogui.size() | sw, sh = pyautogui.size() | ||
- | This will return the width and height of your screen in pixels. I have two monitors hooked to my system, so Linux combines the width of both and reports that as the width. It takes the height of the primary monitor. Be sure to take these values into consideration when you use the moveTo functions. | + | |
+ | **This will return the width and height of your screen in pixels. I have two monitors hooked to my system, so Linux combines the width of both and reports that as the width. It takes the height of the primary monitor. Be sure to take these values into consideration when you use the moveTo functions. | ||
Speaking of the moveTo function, the next line we need to look at is the first moveTo line. | Speaking of the moveTo function, the next line we need to look at is the first moveTo line. | ||
Ligne 47: | Ligne 93: | ||
pyautogui.moveTo(10, | pyautogui.moveTo(10, | ||
- | The parameters are (Xposition, Yposition, Duration). Be sure you use proper values for your system. Remember that Xposition is the horizontal or right/left position on the screen and the Yposition is the vertical or top/bottom position on your screen. The Duration setting is how many milliseconds the movement will take. It’s completely optional, but by setting the value at 0.50, we can actually watch the mouse cursor move on the screen. In your future use, you might want to not include the duration parameter, to speed things up. | + | The parameters are (Xposition, Yposition, Duration). Be sure you use proper values for your system. Remember that Xposition is the horizontal or right/left position on the screen and the Yposition is the vertical or top/bottom position on your screen. The Duration setting is how many milliseconds the movement will take. It’s completely optional, but by setting the value at 0.50, we can actually watch the mouse cursor move on the screen. In your future use, you might want to not include the duration parameter, to speed things up.** |
+ | |||
+ | Celle-ci renvoie la largeur et la hauteur de votre écran en pixels. J'ai deux moniteurs connectés à mon système, donc Linux combine la largeur des deux et la rapporte comme largeur. Il prend la hauteur du moniteur principal. Veillez à prendre ces valeurs en considération lorsque vous utilisez des fonctions moveTo. | ||
+ | |||
+ | À propos de la fonction moveTo, la prochaine ligne que nous devons examiner est la première ligne moveTo : | ||
+ | |||
+ | pyautogui.moveTo(10, | ||
+ | |||
+ | Les paramètres sont (Xposition, Yposition, Duration). Assurez-vous d' | ||
- | We will move to near the upper left of the monitor, then the upper right of the monitor, then to near the lower right of the monitor then to somewhere around the center of the monitor. | + | **We will move to near the upper left of the monitor, then the upper right of the monitor, then to near the lower right of the monitor then to somewhere around the center of the monitor. |
Next, the mouse will move to position 139, 250 and perform a mouse click in the text editor to make sure it’s selected as the target for the text we will eventually send. | Next, the mouse will move to position 139, 250 and perform a mouse click in the text editor to make sure it’s selected as the target for the text we will eventually send. | ||
Ligne 60: | Ligne 115: | ||
As you might guess, being the observant reader that you are, the write function emulates “standard” keyboard keypresses, and writes whatever is set as the parameter to the function to the current mouse position. The final thing that gets done in our test program is that we show a message box from pyautogui saying the program has finished and will then wait for you to manually click the OK button. | As you might guess, being the observant reader that you are, the write function emulates “standard” keyboard keypresses, and writes whatever is set as the parameter to the function to the current mouse position. The final thing that gets done in our test program is that we show a message box from pyautogui saying the program has finished and will then wait for you to manually click the OK button. | ||
+ | |||
+ | pyautogui.alert(text=' | ||
+ | |||
+ | Nous nous déplacerons vers la partie supérieure gauche du moniteur, puis vers la partie supérieure droite du moniteur, puis vers la partie inférieure droite du moniteur et enfin vers le centre du moniteur. | ||
+ | |||
+ | Ensuite, la souris se déplacera à la position 139, 250 et effectuera un clic de souris dans l' | ||
+ | |||
+ | pyautogui.click(139, | ||
+ | |||
+ | Ouvrez un éditeur de texte et vous assurer que la zone de saisie de l' | ||
+ | |||
+ | pyautogui.write(' | ||
+ | |||
+ | Comme vous pouvez le deviner, en tant que lecteur observateur, | ||
pyautogui.alert(text=' | pyautogui.alert(text=' | ||
- | Hopefully, you have the mouseinfo program still running. You can move the mouse to the positions that are coded in the demo program, just to make sure that they fit for your system. If not, all you have to do is modify the positions in the moveTo or click functions to make sure everything works. | + | |
+ | **Hopefully, you have the mouseinfo program still running. You can move the mouse to the positions that are coded in the demo program, just to make sure that they fit for your system. If not, all you have to do is modify the positions in the moveTo or click functions to make sure everything works. | ||
Now, making sure that the text editor is open and in the proper place, you can simply open another terminal in the folder where you saved your python test1.py program, and run it. | Now, making sure that the text editor is open and in the proper place, you can simply open another terminal in the folder where you saved your python test1.py program, and run it. | ||
Ligne 71: | Ligne 141: | ||
If everything worked correctly, you can watch the mouse pointer move around the screen, eventually moving into the text editor and sending the message. | If everything worked correctly, you can watch the mouse pointer move around the screen, eventually moving into the text editor and sending the message. | ||
- | Now, while this isn’t the most useful example of the capabilities of pyautogui, our next project should give you a really good one. | + | Now, while this isn’t the most useful example of the capabilities of pyautogui, our next project should give you a really good one.** |
- | I’ve been working on a new tutorial for the next version of PAGE for Don Rozenberg. The first project in the tutorial is to create a login form which has an entry box for Username and Password, and a button to “submit” the information. I won’t go into the code very much and the actual project can be found on the repository (mentioned at the end of this article). The program looks something like this… | + | Avec un peu de chance, le programme mouseinfo est toujours en cours d' |
+ | |||
+ | Maintenant, en vous assurant que l' | ||
+ | |||
+ | $python test1.py | ||
+ | |||
+ | Si tout a fonctionné correctement, | ||
+ | |||
+ | Si cet exemple n'est pas le plus utile pour montrer les capacités de pyautogui, notre prochain projet devrait vous en donner un bien meilleur. | ||
+ | |||
+ | |||
+ | **I’ve been working on a new tutorial for the next version of PAGE for Don Rozenberg. The first project in the tutorial is to create a login form which has an entry box for Username and Password, and a button to “submit” the information. I won’t go into the code very much and the actual project can be found on the repository (mentioned at the end of this article). The program looks something like this… | ||
The reason I chose this was that I know exactly where the form will appear on the screen, every time it runs, so it will make an easy demo for you to follow. | The reason I chose this was that I know exactly where the form will appear on the screen, every time it runs, so it will make an easy demo for you to follow. | ||
- | You can use the MouseInfo program to verify the positions of the Entry widgets on your system and the button by running the login.py program, then watching the XY Position as you hover over the widgets. If they are off a great deal, you can modify it in our test2 program, which is presented next page, top right. | + | You can use the MouseInfo program to verify the positions of the Entry widgets on your system and the button by running the login.py program, then watching the XY Position as you hover over the widgets. If they are off a great deal, you can modify it in our test2 program, which is presented next page, top right.** |
+ | |||
+ | Je travaille pour Don Rozenberg sur un nouveau tutoriel pour la prochaine version de PAGE. Le premier projet du tutoriel est de créer un formulaire de connexion qui a une boîte de saisie pour le nom d' | ||
+ | |||
+ | La raison pour laquelle j'ai choisi ce programme est que je sais exactement où le formulaire apparaîtra à l' | ||
+ | |||
+ | Vous pouvez utiliser le programme MouseInfo pour vérifier les positions des widgets Entry sur votre système et du bouton en exécutant le programme login.py, puis en observant la position XY lorsque vous survolez les widgets. S'ils sont très décalés, vous pouvez les modifier dans notre programme test2, qui est présenté à la page suivante, en haut à droite. | ||
- | You can see that everything in the code of the test program has already been talked about from our test1 program. I set the time.sleep() call to 10 seconds, so I could take screenshots for the article. You can set it to a value that works for you. | + | **You can see that everything in the code of the test program has already been talked about from our test1 program. I set the time.sleep() call to 10 seconds, so I could take screenshots for the article. You can set it to a value that works for you. |
Ligne 92: | Ligne 180: | ||
$python test2.py | $python test2.py | ||
- | The first thing you should see is the cursor jump to the first Entry widget of the login form and the username Greg should appear. | + | The first thing you should see is the cursor jump to the first Entry widget of the login form and the username Greg should appear.** |
- | After the sleep time has elapsed, you should see the word “password” appear, followed by another sleep time. | + | Vous pouvez voir que tout dans le code du programme de test a déjà été abordé dans notre programme test1. J'ai fixé l' |
+ | |||
+ | Maintenant, vous aurez, je l' | ||
+ | |||
+ | Bon. Ouvrez votre premier terminal et tapez : | ||
+ | |||
+ | $python login.py | ||
+ | |||
+ | Dans le second terminal, en vous assurant que rien n' | ||
+ | |||
+ | $python test2.py | ||
+ | |||
+ | La première chose que vous devriez voir est le curseur qui saute au premier widget d' | ||
+ | |||
+ | |||
+ | **After the sleep time has elapsed, you should see the word “password” appear, followed by another sleep time. | ||
Finally, another sleep period should expire and the Continue button should get “pressed”, | Finally, another sleep period should expire and the Continue button should get “pressed”, | ||
Ligne 108: | Ligne 211: | ||
I hope that you found this revisitation of pyautogui helpful, and that it encourages you to try automated testing for your future projects. | I hope that you found this revisitation of pyautogui helpful, and that it encourages you to try automated testing for your future projects. | ||
- | Until next time, as always; stay safe, healthy, positive and creative! | + | Until next time, as always; stay safe, healthy, positive and creative!** |
+ | |||
+ | |||
+ | Une fois le temps d' | ||
+ | |||
+ | Enfin, une autre période de sommeil devrait expirer et le bouton Continuer devrait être « enfoncé », ce qui fait apparaître une boîte de message du programme. | ||
+ | |||
+ | Immédiatement après, la boîte de message de pyautogui devrait également apparaître. | ||
+ | |||
+ | Cliquez sur les boutons OK des deux boîtes de message, et les programmes devraient se terminer. | ||
+ | |||
+ | Pyautogui peut faire beaucoup plus ; nous n' | ||
+ | |||
+ | Le dépôt pour le code est à https:// | ||
+ | |||
+ | J' | ||
+ | |||
+ | Jusqu' |
issue176/python.1641129901.txt.gz · Dernière modification : 2022/01/02 14:25 de auntiee