issue197:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue197:python [2023/10/01 17:38] – d52fr | issue197:python [2023/10/02 15:38] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 8: | Ligne 8: | ||
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. | ||
+ | |||
+ | 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' | ||
Ligne 17: | Ligne 27: | ||
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.** | ||
+ | |||
+ | 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 | **OptionMenu | ||
Ligne 29: | Ligne 48: | ||
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.** | ||
+ | |||
+ | 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. | **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. | ||
Ligne 37: | Ligne 69: | ||
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).** | ||
+ | |||
+ | 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. | **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. | ||
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 = ttk.Menubutton(_w1.mbFrame, | ||
mnuBtn.pack(expand=True, | 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.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. | ||
Ligne 61: | Ligne 115: | ||
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.** | ||
+ | |||
+ | 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… | **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… | ||
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.** |
+ | |||
+ | 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. | **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. | ||
Ligne 75: | Ligne 151: | ||
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.1696174735.txt.gz · Dernière modification : 2023/10/01 17:38 de d52fr