issue197:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue197:python [2023/09/30 17:37] – créée auntiee | issue197:python [2023/10/02 15:38] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Hello again, fellow Beings. I come again to you from the remote land of Central Texas. | + | **Hello again, fellow Beings. I come again to you from the remote land of Central Texas. |
As the article title suggests, I’m going to be talking about PAGE again. This time, I will show you how to push the envelope and add Tcl widgets WITHOUT using a Custom widget. | As the article title suggests, I’m going to be talking about PAGE again. This time, I will show you how to push the envelope and add Tcl widgets WITHOUT using a Custom widget. | ||
Ligne 7: | Ligne 7: | ||
What widgets would I be talking about? The Menubutton and the OptionMenu widgets. | What widgets would I be talking about? The Menubutton and the OptionMenu widgets. | ||
- | Let’s take a quick look at each of these to see what benefits they give us. | + | Let’s take a quick look at each of these to see what benefits they give us.** |
+ | Bonjour à nouveau, chers amis. Je viens à nouveau vers vous depuis le pays lointain du Texas central. | ||
- | Menubutton | + | Comme le titre de l' |
+ | |||
+ | Si vous connaissez un tant soit peu PAGE, vous savez peut-être qu'il y a quelques widgets « normaux » que PAGE n' | ||
+ | |||
+ | De quels widgets s' | ||
+ | |||
+ | Jetons un coup d'œil rapide à chacun d' | ||
+ | |||
+ | |||
+ | **Menubutton | ||
The Menubutton widget is like having a normal menubar-type menu but on a button. | The Menubutton widget is like having a normal menubar-type menu but on a button. | ||
Ligne 16: | Ligne 26: | ||
You can use all the “normal” menu items on a Menubutton. This includes cascade, command, separators, checkbuttons, | You can use all the “normal” menu items on a Menubutton. This includes cascade, command, separators, checkbuttons, | ||
- | The image above shows a very long menubutton option set that represents all of the books of the King James Bible version, grouped into the Old testament books and New testament books, split with a column break between, and each column has a “header” which is made up of a disabled menu item. Each of the menu items are Radiobutton items, which immediately return an integer value that can be looked up in the list that created the menu that represents the selection the user makes. | + | The image above shows a very long menubutton option set that represents all of the books of the King James Bible version, grouped into the Old testament books and New testament books, split with a column break between, and each column has a “header” which is made up of a disabled menu item. Each of the menu items are Radiobutton items, which immediately return an integer value that can be looked up in the list that created the menu that represents the selection the user makes.** |
- | OptionMenu | + | Menubutton |
+ | |||
+ | Le widget Menubutton revient à disposer d'un menu normal de type barre de menu, mais sur un bouton. | ||
+ | |||
+ | Vous pouvez utiliser tous les éléments de menu « normaux » sur un Menubutton. Cela inclut la cascade, les commandes, les séparateurs, | ||
+ | |||
+ | L' | ||
+ | |||
+ | |||
+ | **OptionMenu | ||
The OptionMenu widget is like a low-tech combobox widget. | The OptionMenu widget is like a low-tech combobox widget. | ||
Ligne 28: | Ligne 47: | ||
I actually created two fairly similar projects for this month, but I’m going to go over only one of them. (Could you hear Ronnie give out a sigh of relief there? | I actually created two fairly similar projects for this month, but I’m going to go over only one of them. (Could you hear Ronnie give out a sigh of relief there? | ||
- | Basically there are four labels (two static and two dynamic), one button (the Exit button) and two frames. The frames are the empty boxes in between the two sets of labels. I used the “solid” relief for the frames. This way, when you are designing the form, you know exactly where the frames are. I designed the project under PAGE 7.6, so the .tcl file is included. | + | Basically there are four labels (two static and two dynamic), one button (the Exit button) and two frames. The frames are the empty boxes in between the two sets of labels. I used the “solid” relief for the frames. This way, when you are designing the form, you know exactly where the frames are. I designed the project under PAGE 7.6, so the .tcl file is included.** |
- | All of the heavy work is done in the support module, so we don’t risk breaking the project by modifying the GUI module. As I always do, I inserted a line in the main function to call the startup function right before the program is shown on the screen. | + | OptionMenu |
+ | |||
+ | Le widget OptionMenu est comme un widget combobox de faible technicité. | ||
+ | |||
+ | Lorsque vous cliquez sur le tiret sur le côté, vous obtenez un menu déroulant avec vos options. | ||
+ | |||
+ | Le projet | ||
+ | |||
+ | J'ai en fait créé deux projets assez similaires pour ce mois-ci, mais je ne vais passer en revue qu'un seul d' | ||
+ | |||
+ | En gros, il y a quatre étiquettes (deux statiques et deux dynamiques), | ||
+ | |||
+ | |||
+ | **All of the heavy work is done in the support module, so we don’t risk breaking the project by modifying the GUI module. As I always do, I inserted a line in the main function to call the startup function right before the program is shown on the screen. | ||
As you can see (top right) there are two functions for each of our special widgets. One that sets up the items that the widgets will show, and one that actually creates the widget on the toplevel form for us. There are also two additional functions, which are the callback handlers. | As you can see (top right) there are two functions for each of our special widgets. One that sets up the items that the widgets will show, and one that actually creates the widget on the toplevel form for us. There are also two additional functions, which are the callback handlers. | ||
Ligne 36: | Ligne 68: | ||
So, we’ll start with the setup_menubutton_items() function (bottom below). | So, we’ll start with the setup_menubutton_items() function (bottom below). | ||
- | This simple function just creates a list of strings that will be used to populate the Menubutton widget. While this code could easily have been included in the creation function, I decided to break it out into its own function, and to do the same thing for the items for the OptionMenu widget (below). | + | This simple function just creates a list of strings that will be used to populate the Menubutton widget. While this code could easily have been included in the creation function, I decided to break it out into its own function, and to do the same thing for the items for the OptionMenu widget (below).** |
- | One thing to notice here. The items for the Menubutton are in a list. The items for the OptionMenu is a tuple. This is important as you will see in a little bit. | + | Le gros du travail est entièrement fait dans le module de support ; nous ne risquons donc pas de casser le projet en modifiant le module GUI. Comme je le fais toujours, j'ai inséré une ligne dans la fonction principale pour appeler la fonction de démarrage juste avant que le programme ne soit affiché à l' |
+ | |||
+ | Comme vous pouvez le voir (en haut à droite), il y a deux fonctions pour chacun de nos widgets spéciaux. L'une d' | ||
+ | |||
+ | Nous allons donc commencer par la fonction setup_menubutton_items() (voir ci-dessous). | ||
+ | |||
+ | Cette fonction simple crée simplement une liste de chaînes de caractères qui seront utilisées pour remplir le widget Menubutton. Alors que ce code aurait facilement pu être inclus dans la fonction de création, j'ai décidé de le séparer dans sa propre fonction, et de faire la même chose pour les éléments du widget OptionMenu (ci-dessous). | ||
+ | |||
+ | |||
+ | **One thing to notice here. The items for the Menubutton are in a list. The items for the OptionMenu is a tuple. This is important as you will see in a little bit. | ||
Now we will create the Menubutton widget and stick it into the proper Frame(below). | Now we will create the Menubutton widget and stick it into the proper Frame(below). | ||
Ligne 44: | Ligne 85: | ||
The first thing we do to create the Menubutton widget is to define a couple of global variables, one for the carTypes, which we just defined in the setup_menubutton_items function, and the other named selection, which will be a type of tk.IntVar, since our menu items will all be of the radiobutton type. Then we set the frame relief from solid to flat, to make the frame invisible. Finally, we make the actual assignment of the tk.IntVar. Again, we will do this so the user’s selection will immediately be handled and to cut down on the number of callback functions. | The first thing we do to create the Menubutton widget is to define a couple of global variables, one for the carTypes, which we just defined in the setup_menubutton_items function, and the other named selection, which will be a type of tk.IntVar, since our menu items will all be of the radiobutton type. Then we set the frame relief from solid to flat, to make the frame invisible. Finally, we make the actual assignment of the tk.IntVar. Again, we will do this so the user’s selection will immediately be handled and to cut down on the number of callback functions. | ||
- | Next, we create the actual widget. Then we will use the Pack geometry manager to “shove” the widget into the frame and force it to completely fill the frame. The name of the MenuButton | + | Next, we create the actual widget. Then we will use the Pack geometry manager to “shove” the widget into the frame and force it to completely fill the frame. The name of the Menubutton |
+ | |||
+ | mnuBtn = ttk.Menubutton(_w1.mbFrame, | ||
+ | |||
+ | mnuBtn.pack(expand=True, | ||
+ | |||
+ | Une chose à noter ici. Les éléments du Menubutton sont dans une liste. Les éléments de l' | ||
+ | |||
+ | Nous allons maintenant créer le widget Menubutton et le placer dans le cadre approprié (ci-dessous). | ||
+ | |||
+ | La première chose à faire pour créer le widget Menubutton est de définir quelques variables globales, l'une pour les carTypes, que nous venons de définir dans la fonction setup_menubutton_items, | ||
+ | |||
+ | Ensuite, nous créons le widget proprement dit. Nous utiliserons ensuite le gestionnaire de géométrie Pack pour « pousser » le widget dans le cadre et le forcer à remplir le frame complètement. Le nom de l' | ||
mnuBtn = ttk.Menubutton(_w1.mbFrame, | mnuBtn = ttk.Menubutton(_w1.mbFrame, | ||
Ligne 50: | Ligne 103: | ||
mnuBtn.pack(expand=True, | mnuBtn.pack(expand=True, | ||
- | Now (top right) we have to define the menu object that will be attached to the menu button. | + | |
+ | **Now (top right) we have to define the menu object that will be attached to the menu button. | ||
The last thing we do for the Menubutton is to define a counter variable, and walk through the list of carTypes, and then create the radiobutton menu item with the name of the car type, the callback, the value of the counter variable which we set as the value that will be passed back to the callback, and the name of the variable which in our case is selection. Finally, bottom right, we add one to the counter variable. | The last thing we do for the Menubutton is to define a counter variable, and walk through the list of carTypes, and then create the radiobutton menu item with the name of the car type, the callback, the value of the counter variable which we set as the value that will be passed back to the callback, and the name of the variable which in our case is selection. Finally, bottom right, we add one to the counter variable. | ||
Ligne 60: | Ligne 114: | ||
returnval.set(" | returnval.set(" | ||
- | Finally, we do the creation of the instance of the OptionMenu button and finally pack the widget into its frame, just like we did with the Menubutton widget. | + | Finally, we do the creation of the instance of the OptionMenu button and finally pack the widget into its frame, just like we did with the Menubutton widget.** |
- | However, notice that in the options for the widget, we use *optionlist as the “list” of options to display. Remember that I said we need to make this a tuple, not a list. The reason for this is that you can provide each item separately as the syntax line below shows… | + | Maintenant (en haut à droite), nous devons définir l' |
+ | |||
+ | La dernière chose que nous faisons pour le Menubutton est de définir une variable compteur, de parcourir la liste des carTypes, puis de créer l' | ||
+ | |||
+ | Nous commençons par le même type de code que pour le widget Menubutton. Nous définissons les globales, nous définissons le relief du cadre de solid à flat, et nous définissons tk.StringVar pour notre valeur de retour. | ||
+ | |||
+ | La ligne suivante n'est techniquement pas nécessaire, | ||
+ | |||
+ | returnval.set(" | ||
+ | |||
+ | Enfin, nous créons l' | ||
+ | |||
+ | |||
+ | **However, notice that in the options for the widget, we use *optionlist as the “list” of options to display. Remember that I said we need to make this a tuple, not a list. The reason for this is that you can provide each item separately as the syntax line below shows… | ||
w = ttk.OptionMenu(parent, | w = ttk.OptionMenu(parent, | ||
Ligne 68: | Ligne 135: | ||
If we were to use a straight list, the options will all be placed on the same line instead of a dropdown set of items. There might be a reason for you to want to do this, but I can’t imagine one (next page). | If we were to use a straight list, the options will all be placed on the same line instead of a dropdown set of items. There might be a reason for you to want to do this, but I can’t imagine one (next page). | ||
- | That’s it. Now I have to say that the OptionMenu widget is REALLY a whole lot easier to deal with than the MenuButton. If you’ve ever created a menu by hand in Tkinter, you know it is not a painless process and often not worth all the work. You can see what I mean in the second project. If you run back to the first image in the article, you will see the Menubutton example. I really did this as a proof of concept more than for any real need. I would probably use a popup menu created in the PAGE menu editor before I would hand code a menu for a Menubutton. | + | That’s it. Now I have to say that the OptionMenu widget is REALLY a whole lot easier to deal with than the Menubutton. If you’ve ever created a menu by hand in Tkinter, you know it is not a painless process and often not worth all the work. You can see what I mean in the second project. If you run back to the first image in the article, you will see the Menubutton example. I really did this as a proof of concept more than for any real need. I would probably use a popup menu created in the PAGE menu editor before I would hand code a menu for a Menubutton.** |
- | The whole purpose in this project was to show you that with a little work (well, a LOT of work in the case of the Menubutton) and digging, you can use any widget that Tkinter provides even if PAGE doesn’t support it. | + | Cependant, remarquez que dans les options du widget, nous utilisons *optionlist comme la « liste » d' |
+ | |||
+ | w = ttk.OptionMenu(parent, | ||
+ | |||
+ | Si nous devions utiliser une véritable liste, les options seraient toutes placées sur la même ligne au lieu d'un ensemble d' | ||
+ | |||
+ | Voilà, c'est fait. Je dois dire que le widget OptionMenu est VRAIMENT beaucoup plus facile à utiliser que Menubutton. Si vous avez déjà créé un menu à la main dans Tkinter, vous savez que ce n'est pas un processus sans douleur et qu'il ne vaut souvent pas la peine d'y consacrer tout son temps. Vous pouvez voir ce que je veux dire dans le deuxième projet. Si vous revenez à la première image de l' | ||
+ | |||
+ | |||
+ | **The whole purpose in this project was to show you that with a little work (well, a LOT of work in the case of the Menubutton) and digging, you can use any widget that Tkinter provides even if PAGE doesn’t support it. | ||
I’ve put the code for this month’s project in a github repository at https:// | I’ve put the code for this month’s project in a github repository at https:// | ||
- | Until next time, as always; stay safe, healthy, positive and creative! | + | Until next time, as always; stay safe, healthy, positive and creative!** |
+ | |||
+ | Le but de ce projet était de vous montrer qu' | ||
+ | |||
+ | J'ai mis le code du projet de ce mois-ci dans un dépôt github à https:// | ||
+ | |||
+ | Jusqu' |
issue197/python.1696088272.txt.gz · Dernière modification : 2023/09/30 17:37 de auntiee