issue58:tuto_python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue58:tuto_python [2012/03/06 14:29] – créée auntiee | issue58:tuto_python [2012/03/25 15:18] (Version actuelle) – fredphil91 | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ** | ||
This month, we'll explore yet another GUI designer, this time for Tkinter. Many people have an issue with Tkinter because it doesn' | This month, we'll explore yet another GUI designer, this time for Tkinter. Many people have an issue with Tkinter because it doesn' | ||
Ligne 5: | Ligne 6: | ||
You need TCK/TK 8.5.4 or later, Python 2.6 or later, and pyttk - which you can get (if you don't already have it) from http:// | You need TCK/TK 8.5.4 or later, Python 2.6 or later, and pyttk - which you can get (if you don't already have it) from http:// | ||
+ | ** | ||
+ | Ce mois-ci, nous allons explorer encore un autre concepteur graphique, cette fois c'est pour Tkinter. Beaucoup de gens ont un problème avec Tkinter, car il n' | ||
+ | |||
+ | Pré-requis | ||
+ | |||
+ | Vous devez avoir TCK/TK 8.5.4 ou plus, Python 2.6 ou plus et pyttk, que vous pouvez obtenir (si vous ne l'avez pas encore) à partir de http:// | ||
+ | |||
+ | ** | ||
Installation | Installation | ||
Ligne 21: | Ligne 30: | ||
Next, in the launch pad form, click on Window and select Attribute Editor (if it's not already showing). Your single button should be highlighted already, so move it around the form and when you release the mouse button you should see the position change in the attribute editor form under 'x position' | Next, in the launch pad form, click on Window and select Attribute Editor (if it's not already showing). Your single button should be highlighted already, so move it around the form and when you release the mouse button you should see the position change in the attribute editor form under 'x position' | ||
+ | ** | ||
+ | Installation | ||
+ | |||
+ | Vous ne pouvez vraiment pas demander une procédure d' | ||
+ | |||
+ | Apprentissage de Page | ||
+ | |||
+ | Lorsque vous démarrez Page, vous aurez trois fenêtres (formulaires). L'une est une « piste de lancement », l' | ||
+ | |||
+ | Pour démarrer un nouveau projet, cliquez sur le bouton du haut dans la boîte à outils. | ||
+ | |||
+ | Cela crée votre formulaire principal. Vous pouvez le déplacer où vous le souhaitez sur votre écran. Ensuite, et à partir de maintenant, cliquez sur un widget dans la boîte à outils, puis cliquez sur l' | ||
+ | |||
+ | Pour l' | ||
+ | |||
+ | Ensuite, dans le formulaire de lancement, cliquez sur Fenêtre (Window) et sélectionnez l' | ||
+ | |||
+ | ** | ||
Here we can set other attributes such as the text on the button (or most any other widget), the alias for the widget (the name we will refer to in our code), color, the name we will call it and more. Near the bottom of the attribute editor is the text field. This is the text that appears to the user for, in this case, the button widget. Let's change this from “button” to “Exit”. Notice that now the button says “Exit”. Now resize the form to just show the button and recenter the button in the form. | Here we can set other attributes such as the text on the button (or most any other widget), the alias for the widget (the name we will refer to in our code), color, the name we will call it and more. Near the bottom of the attribute editor is the text field. This is the text that appears to the user for, in this case, the button widget. Let's change this from “button” to “Exit”. Notice that now the button says “Exit”. Now resize the form to just show the button and recenter the button in the form. | ||
Next click in the main form someplace where the button isn't. The attribute editor form now shows the attributes for the main form. Find the “title” field and change this from “New Toplevel 1” to “Test Form”. | Next click in the main form someplace where the button isn't. The attribute editor form now shows the attributes for the main form. Find the “title” field and change this from “New Toplevel 1” to “Test Form”. | ||
+ | ** | ||
+ | |||
+ | Ici, nous pouvons définir d' | ||
+ | |||
+ | Ensuite, cliquez quelque part dans le formulaire principal où le bouton n'est pas. Le formulaire éditeur d' | ||
+ | |||
+ | ** | ||
Now, before we save our project, we need to create a folder to hold our project files. Create a folder somewhere on your drive called “PageProjects”. Now, in the launch pad window, select File then Save As. Navigate to your PageProjects folder, and, in the dialog box, type TestForm.tcl and click the Save button. Notice this is saved as a TCL file, not a Python file. We'll create the python file next. | Now, before we save our project, we need to create a folder to hold our project files. Create a folder somewhere on your drive called “PageProjects”. Now, in the launch pad window, select File then Save As. Navigate to your PageProjects folder, and, in the dialog box, type TestForm.tcl and click the Save button. Notice this is saved as a TCL file, not a Python file. We'll create the python file next. | ||
Ligne 33: | Ligne 67: | ||
Click Save. If, at this point, you were to look in your PageProjects folder, you will see the python file (TestForm.py). Now click on the Run button. In a few seconds, you'll see the project start up. The button is not connected to anything yet, so it won't do anything if you click on it. Simply close the form with the “X” in the corner of the window. Now close the Python Console window with the close button at the bottom right. | Click Save. If, at this point, you were to look in your PageProjects folder, you will see the python file (TestForm.py). Now click on the Run button. In a few seconds, you'll see the project start up. The button is not connected to anything yet, so it won't do anything if you click on it. Simply close the form with the “X” in the corner of the window. Now close the Python Console window with the close button at the bottom right. | ||
+ | ** | ||
+ | Maintenant, avant de sauvegarder notre projet, nous avons besoin de créer un dossier pour contenir nos fichiers de projet. Créez un dossier quelque part sur votre disque appelé « PageProjects ». Puis, dans la fenêtre de lancement, sélectionnez File puis Save As. Allez dans votre dossier PageProjects et, dans la boîte de dialogue, tapez TestForm.tclet et cliquez sur le bouton Save. Noter que cela est enregistré comme fichier TCL, pas comme fichier Python. Ensuite, nous allons créer le fichier python. | ||
+ | |||
+ | Dans la fenêtre de lancement, cherchez le menu Gen_Python et cliquez dessus. Sélectionnez Generate Python et un nouveau formulaire apparaît. | ||
+ | |||
+ | Page a généré (comme son nom l' | ||
+ | |||
+ | Cliquez sur Save. Si, à ce stade, vous deviez regarder dans votre dossier PageProjects, | ||
+ | |||
+ | ** | ||
Back at our main form, highlight the Exit button and right click on it. Select “Bindings...”. Under the menu is a set of buttons. | Back at our main form, highlight the Exit button and right click on it. Select “Bindings...”. Under the menu is a set of buttons. | ||
Ligne 43: | Ligne 87: | ||
The first button on the left is the Add button. Click it. In the Function box, type “py: | The first button on the left is the Add button. Click it. In the Function box, type “py: | ||
+ | ** | ||
+ | De retour à notre formulaire principal, sélectionnez le bouton Exit et faites un clic droit dessus. Sélectionnez « Bindings... ». Dans le menu se trouve un ensemble de boutons. | ||
+ | |||
+ | Le premier sur la gauche vous permet de créer une nouvelle liaison. Cliquez sur « Bouton-1 ». Cela nous permet de paramétrer la liaison pour le bouton gauche de la souris. | ||
+ | |||
+ | Enregistrez et générez le code python à nouveau. Faites défiler le code dans la console Python jusqu' | ||
+ | |||
+ | Sur la fenêtre de lancement, cliquez sur Window puis sélectionnez Function List. Ici, nous allons écrire notre méthode pour fermer la fenêtre. | ||
+ | |||
+ | Le premier bouton à gauche est le bouton Add. Cliquez dessus. Dans la zone Function, tapez « py: | ||
+ | |||
+ | ** | ||
def Button1Click(p1): | def Button1Click(p1): | ||
sys.exit() | sys.exit() | ||
Ligne 51: | Ligne 107: | ||
Next we have to bind this routine to the button. Select the button in the form, right click it, and select “Bindings...”. As before, click on the far left button on the toolbar and select Button-1. This is the event for the left mouse button click. In the right text box, enter “Button1Click”. Make sure you use the same case that you did for the Function we just created. Click the checkmark on the right side. | Next we have to bind this routine to the button. Select the button in the form, right click it, and select “Bindings...”. As before, click on the far left button on the toolbar and select Button-1. This is the event for the left mouse button click. In the right text box, enter “Button1Click”. Make sure you use the same case that you did for the Function we just created. Click the checkmark on the right side. | ||
Now save and generate your python code. | Now save and generate your python code. | ||
+ | ** | ||
+ | def Button1Click(p1): | ||
+ | sys.exit() | ||
+ | | ||
+ | Cliquez sur la coche et nous avons terminé avec cela. | ||
+ | |||
+ | Ensuite, nous devons lier cette routine au bouton. Sélectionnez le bouton dans le formulaire, faites un clic droit dessus et sélectionnez « Bindings... ». Comme précédemment, | ||
+ | Maintenant, sauvegardez et générez votre code python. | ||
+ | |||
+ | ** | ||
You should see the following code near the bottom, but OUTSIDE of the Test_Form class... | You should see the following code near the bottom, but OUTSIDE of the Test_Form class... | ||
Ligne 63: | Ligne 129: | ||
Now, if you run your code and click on the Exit button, the form should close properly. | Now, if you run your code and click on the Exit button, the form should close properly. | ||
+ | ** | ||
+ | Vous devriez voir le code suivant vers le bas, mais en dehors de la classe Test_Form... | ||
+ | |||
+ | def Button1Click(p1): | ||
+ | |||
+ | sys.exit() | ||
+ | |||
+ | Et la dernière ligne de la classe devrait être... | ||
+ | |||
+ | self.Button1.bind('< | ||
+ | |||
+ | Maintenant, si vous exécutez votre code et cliquez sur le bouton Exit, le formulaire doit se fermer correctement. | ||
+ | |||
+ | |||
+ | ** | ||
Moving Forward | Moving Forward | ||
Ligne 69: | Ligne 150: | ||
Next, place in the lower frame a label saying “Radio Buttons” and four radio buttons like in the image below. Finally, place an Exit button below the bottom frame. | Next, place in the lower frame a label saying “Radio Buttons” and four radio buttons like in the image below. Finally, place an Exit button below the bottom frame. | ||
+ | ** | ||
+ | |||
+ | Pour aller plus loin | ||
+ | |||
+ | Maintenant, nous allons faire quelque chose de plus compliqué. Nous allons créer une démo montrant quelques-uns des widgets qui sont disponibles. D' | ||
+ | |||
+ | Ensuite, placez dans le cadre inférieur une étiquette de texte « Radio Buttons » et quatre boutons radio comme dans l' | ||
+ | |||
+ | ** | ||
Before we work on the bindings, let's create our click functions. Open the Function List and create two functions. The first should be called btnNormalClicked and the other btnSunkenClicked. Make sure you set the arguments box to include p1. Here's the code you should have for them... | Before we work on the bindings, let's create our click functions. Open the Function List and create two functions. The first should be called btnNormalClicked and the other btnSunkenClicked. Make sure you set the arguments box to include p1. Here's the code you should have for them... | ||
Ligne 81: | Ligne 171: | ||
Let's add our button bindings. For each button, right click it, select “Bindings...”, | Let's add our button bindings. For each button, right click it, select “Bindings...”, | ||
+ | ** | ||
+ | Avant de travailler sur les liaisons, nous allons créer nos fonctions de clic. Ouvrez la liste de fonctions et créez deux fonctions. Le premier devrait être appelé btnNormalClicked et l' | ||
+ | |||
+ | def btnNormalClicked(p1): | ||
+ | |||
+ | print " | ||
+ | |||
+ | def btnSunkenClicked(p1) : | ||
+ | |||
+ | print " | ||
+ | |||
+ | Ajoutons nos liaisons aux boutons. Pour chaque bouton, faites un clic droit dessus, sélectionnez « Bindings... » et ajoutez, comme avant, une liaison aux fonctions que nous avons créées. Pour le bouton normal, cela sera « btnNormalClicked » et pour le bouton creux cela sera « btnSunkenClicked ». Enregistrez et générez votre code. Maintenant, si vous testiez le programme avec l' | ||
+ | |||
+ | |||
+ | ** | ||
Now for our radio buttons. We have grouped them in two “clusters”. The first two (Radio 1 and Radio 2) will be cluster 1 and the other two will be cluster 2. Click on Radio1 and in the Attribute Editor, set the value to 0 and the variable to “rbc1”. Set the variable for Radio 2 to “rbc1” and the value to 1. Do the same thing for Radio 3 and Radio 4 but for both of these set the variable to “rbc2”. If you want, you can deal with the click of the radiobuttons and print something to the terminal, but for now, the important thing is that the clusters work. Clicking Radio1 will deselect Radio2 and not influence Radio3 or Radio4, and the same for Radio2 and so on. | Now for our radio buttons. We have grouped them in two “clusters”. The first two (Radio 1 and Radio 2) will be cluster 1 and the other two will be cluster 2. Click on Radio1 and in the Attribute Editor, set the value to 0 and the variable to “rbc1”. Set the variable for Radio 2 to “rbc1” and the value to 1. Do the same thing for Radio 3 and Radio 4 but for both of these set the variable to “rbc2”. If you want, you can deal with the click of the radiobuttons and print something to the terminal, but for now, the important thing is that the clusters work. Clicking Radio1 will deselect Radio2 and not influence Radio3 or Radio4, and the same for Radio2 and so on. | ||
Ligne 91: | Ligne 196: | ||
The python code can be found on pastebin at http:// | The python code can be found on pastebin at http:// | ||
+ | ** | ||
+ | Maintenant, passons à nos boutons radio. Nous les avons regroupés en deux groupes « clusters ». Les deux premiers (Radio 1 et Radio 2) formeront le groupe 1 et les deux autres seront le groupe 2. Cliquez sur Radio 1 et dans l' | ||
+ | |||
+ | Enfin, vous devez créer une fonction pour le bouton Exit et la lier au bouton, comme nous l' | ||
+ | |||
+ | Si vous avez suivi depuis le début nos autres applications Tkinter, vous devriez être en mesure de comprendre le code montré ci-dessus à droite. Sinon, merci de retourner dans quelques numéros précédents pour lire une présentation complète de ce code. | ||
+ | |||
+ | Vous pouvez voir qu' | ||
+ | |||
+ | Le code python peut être trouvé sur pastebin à http:// | ||
+ | |||
+ | ** | ||
One note before we go for this month. You might have noticed that I've missed a couple of issues. This is due to my wife being diagnosed with cancer last year. As hard as I have tried to keep things from falling through the cracks, a number of things have. One of these things is my old domain/web site at www.thedesignatedgeek.com. I blew it and missed the renewal. Due to this, the domain was sold out from under me. I have set up www.thedesignatedgeek.net with all the old stuff. I will be working hard the next month to bring it all up to date. | One note before we go for this month. You might have noticed that I've missed a couple of issues. This is due to my wife being diagnosed with cancer last year. As hard as I have tried to keep things from falling through the cracks, a number of things have. One of these things is my old domain/web site at www.thedesignatedgeek.com. I blew it and missed the renewal. Due to this, the domain was sold out from under me. I have set up www.thedesignatedgeek.net with all the old stuff. I will be working hard the next month to bring it all up to date. | ||
See you next time. | See you next time. | ||
+ | ** | ||
+ | |||
+ | Une note avant de terminer pour ce mois-ci. Vous avez sans doute remarqué que j'ai manqué quelques numéros. Cela est dû au fait que ma femme a été diagnostiquée avec un cancer l'an dernier. Même si j'ai vraiment essayé d' | ||
+ | |||
+ | Rendez-vous la prochaine fois. |
issue58/tuto_python.1331040573.txt.gz · Dernière modification : 2012/03/06 14:29 de auntiee