issue193:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue193:python [2023/05/29 07:46] – créée d52fr | issue193:python [2023/05/31 12:15] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Welcome back my friends. I hope that May finds you all healthy, wealthy and wise. Or at least healthy. | + | **Welcome back my friends. I hope that May finds you all healthy, wealthy and wise. Or at least healthy. |
This month, I’m going to do another PAGE article, but what I present will be directly relatable to straight Python/ | This month, I’m going to do another PAGE article, but what I present will be directly relatable to straight Python/ | ||
Ligne 9: | Ligne 9: | ||
There is really nothing special about the program at this point. However, in the support module I play all kinds of games with everything there. | There is really nothing special about the program at this point. However, in the support module I play all kinds of games with everything there. | ||
- | Bottom right is shown an idea of what you can expect from the finished code. | + | Bottom right is shown an idea of what you can expect from the finished code.** |
- | I hope that this is already giving you some ideas of how this might be useful to you in your own programs. It’s actually very easy to do and, with the theme that I’ve created, you can customize the theme package to suit your moods. We’ll go into the theme package more next month. For now, let’s look at how to change the position of the Tabs on the TNotebook. | + | Bienvenue, mes amis. J' |
+ | |||
+ | Ce mois-ci, je vais faire un autre article PAGE, mais ce que je vais présenter sera directement lié à la programmation Python/ | ||
+ | |||
+ | Quoi qu'il en soit, commençons. | ||
+ | |||
+ | Je vais commencer par une capture d' | ||
+ | |||
+ | À ce stade, le programme n'a vraiment rien de spécial. Cependant, dans le module de support, je joue à toutes sortes de jeux avec tout ce qui s'y trouve. | ||
+ | |||
+ | En bas à droite se trouve une idée de ce que vous pouvez attendre du code fini. | ||
+ | |||
+ | |||
+ | **I hope that this is already giving you some ideas of how this might be useful to you in your own programs. It’s actually very easy to do and, with the theme that I’ve created, you can customize the theme package to suit your moods. We’ll go into the theme package more next month. For now, let’s look at how to change the position of the Tabs on the TNotebook. | ||
The TNotebook has a little known (well, probably little known to most Tkinter programmers anyway) option that allows the programmer to set the position of the Tab portion of the TNotebook widget. It’s part of the ttk Style options. Unfortunately, | The TNotebook has a little known (well, probably little known to most Tkinter programmers anyway) option that allows the programmer to set the position of the Tab portion of the TNotebook widget. It’s part of the ttk Style options. Unfortunately, | ||
- | So the first thing that needs to happen is to create an object that inherits from the ttk.Style base object. After many hours of struggling to come up with a name for my version of the object, I finally settled on “style”. Pretty cool, huh? Anyway, that joke probably fell flat, so we’ll move on. | + | So the first thing that needs to happen is to create an object that inherits from the ttk.Style base object. After many hours of struggling to come up with a name for my version of the object, I finally settled on “style”. Pretty cool, huh? Anyway, that joke probably fell flat, so we’ll move on.** |
- | The next part of the function is probably not useful to this particular program, since all the decisions as to the position are provided by the TRadiobuttons. However, if you are going to add this function to one of your programs, you might want to keep it in, since mistakes are easy to make. | + | J' |
+ | |||
+ | Le TNotebook dispose d'une option peu connue (enfin, probablement peu connue de la plupart des programmeurs Tkinter) qui permet au programmeur de définir la position de la partie Tab du widget TNotebook. Elle fait partie des options de ttk Style. Malheureusement, | ||
+ | |||
+ | La première chose à faire est donc de créer un objet qui hérite de l' | ||
+ | |||
+ | |||
+ | **The next part of the function is probably not useful to this particular program, since all the decisions as to the position are provided by the TRadiobuttons. However, if you are going to add this function to one of your programs, you might want to keep it in, since mistakes are easy to make. | ||
The positions list is provided to verify that the passed parameter is actually one of the “legal” positions. We check to see if it is a member of the positions list, and if so, we use the Style.configure command. The syntax is: | The positions list is provided to verify that the passed parameter is actually one of the “legal” positions. We check to see if it is a member of the positions list, and if so, we use the Style.configure command. The syntax is: | ||
Ligne 27: | Ligne 47: | ||
That’s the entire magic that has rarely been documented. | That’s the entire magic that has rarely been documented. | ||
- | The next function that we will take a look at is the callback function that is called whenever a TRadiobutton is clicked. Just so you know, all the TRadiobuttons have the same callback and the values of the TRadiobuttons are set 1 to 12. This makes it easy to use an if tree. It would be just as easy to use the switch case tree, but since that is available only from 3.10 forward, I decided to stick with the if tree (see code right). | + | The next function that we will take a look at is the callback function that is called whenever a TRadiobutton is clicked. Just so you know, all the TRadiobuttons have the same callback and the values of the TRadiobuttons are set 1 to 12. This makes it easy to use an if tree. It would be just as easy to use the switch case tree, but since that is available only from 3.10 forward, I decided to stick with the if tree (see code right).** |
- | I deleted many of the elif statements since they are pretty much all the same thing with the only thing changing is the position. | + | La partie suivante de la fonction n'est probablement pas utile pour ce programme-ci, |
+ | |||
+ | La liste des positions est fournie pour vérifier que le paramètre passé est bien l'une des positions « légales ». Nous vérifions si le paramètre fait partie de la liste des positions et, si c'est le cas, nous utilisons la commande Style.configure. La syntaxe est la suivante : | ||
+ | |||
+ | style.configure(TWidget name, StyleOption = Value, …) | ||
+ | |||
+ | Ensuite, la commande elif détecte un paramètre non valide, imprime un message sur le terminal et « réinitialise » le paramètre de position du TNotebook à la valeur par défaut (qui est le coin nord-ouest du widget), juste au cas où il y aurait eu des changements valides avant cet appel. | ||
+ | |||
+ | C'est là toute la magie qui a rarement été documentée. | ||
+ | |||
+ | La fonction suivante que nous allons examiner est la fonction de rappel qui est appelée chaque fois qu'on clique sur TRadiobutton. Pour votre information, | ||
+ | |||
+ | |||
+ | **I deleted many of the elif statements since they are pretty much all the same thing with the only thing changing is the position. | ||
You might also notice that each of the if/elif statements has two set tab statements. The first is to set the position to either “e” or “n” before the actual final position is set. This is because when I was testing the program, if I went from a position on the top or bottom of the TNotebook directly to one on the right or left, the full tab set would not show up. The same if I went from the right or left to one on the right or left. I’m fairly certain that’s a bug in the ttk code. I can understand why it might have been overlooked by the original programmers of the TNotebook widget. How often would ANYONE create a program to walk the tabs around the edges of the widget. Besides me, right? | You might also notice that each of the if/elif statements has two set tab statements. The first is to set the position to either “e” or “n” before the actual final position is set. This is because when I was testing the program, if I went from a position on the top or bottom of the TNotebook directly to one on the right or left, the full tab set would not show up. The same if I went from the right or left to one on the right or left. I’m fairly certain that’s a bug in the ttk code. I can understand why it might have been overlooked by the original programmers of the TNotebook widget. How often would ANYONE create a program to walk the tabs around the edges of the widget. Besides me, right? | ||
- | Another thing you might have noticed is that the tabs are set to an interesting color set. I just put up a new post on my website (thedesignatedgeek.xyz) that explains the process, but I’ll go over it quickly here. I use another function to take care of this and it uses more ttk Styling “magic”. I called the function setup_base_style() | + | Another thing you might have noticed is that the tabs are set to an interesting color set. I just put up a new post on my website (thedesignatedgeek.xyz) that explains the process, but I’ll go over it quickly here. I use another function to take care of this and it uses more ttk Styling “magic”. I called the function setup_base_style() |
- | Again, we create an instance of the ttk.style object and, this time, we apply a map which is used mostly when we want to deal with colors. | + | J'ai supprimé un grand nombre d' |
+ | |||
+ | Vous remarquerez également que chacune des instructions if/elif comporte deux instructions set tab. La première consiste à définir la position sur « e » ou « n » avant que la position finale ne soit définie. En effet, lorsque j'ai testé le programme, si je passais d'une position en haut ou en bas du TNotebook directement à une position à droite ou à gauche, l' | ||
+ | |||
+ | Une autre chose que vous avez peut-être remarquée, c'est que les onglets sont réglés sur un jeu de couleurs intéressant. Je viens de publier un nouvel article sur mon site Web (thedesignatedgeek.xyz) qui explique le processus, mais je vais le passer rapidement en revue ici. J' | ||
+ | |||
+ | |||
+ | **Again, we create an instance of the ttk.style object and, this time, we apply a map which is used mostly when we want to deal with colors. | ||
Basically, we assign a list of tuples to both the background and foreground colors. There are three states that we deal with here. They are selected, active, and !active (or not active). As you might suspect, the selected state is the tab that is currently selected and its page (actually a TFrame) is the one that is currently shown. The active state is triggered when the mouse cursor is hovering over that tab, and the !active state is when any of the other tabs are not in one of the other two states. | Basically, we assign a list of tuples to both the background and foreground colors. There are three states that we deal with here. They are selected, active, and !active (or not active). As you might suspect, the selected state is the tab that is currently selected and its page (actually a TFrame) is the one that is currently shown. The active state is triggered when the mouse cursor is hovering over that tab, and the !active state is when any of the other tabs are not in one of the other two states. | ||
So the actual selected tab is the one in the darker gray, the active tab is the one in the light gray and the !active tabs are the ones in sandybrown color. | So the actual selected tab is the one in the darker gray, the active tab is the one in the light gray and the !active tabs are the ones in sandybrown color. | ||
- | The setup_base_style is called from the startup menu. For those who don’t remember what the startup function is or where it is being called from, this is a function that allows any variables or functions to be called before the program is actually shown to the user and the program goes into the Tkinter mainloop function. | + | The setup_base_style is called from the startup menu. For those who don’t remember what the startup function is or where it is being called from, this is a function that allows any variables or functions to be called before the program is actually shown to the user and the program goes into the Tkinter mainloop function.** |
- | The first few lines simply set up some general styling things (which we’ll discuss next month) and calls an imported Python module that I created and named mystyles_dark. This was an experiment I played around with back in December 2022. The idea was to create a Python based Theme for Tkinter programs. If you remember, a ttk Theme is simply a collection of styles that apply to various ttk widgets. I never got around to finishing that project, since once I got the basics of getting the styles for various widgets going, I then put the Python file on the back burner and dived head first into writing a true Tcl/Tk theme in Tcl. (That was an experience, let me tell you! Well, actually that story is going in a blog on my website.) Anyway, I digress. The Python | + | Nous créons à nouveau une instance de l' |
- | We’ll talk about the Python styling module next month (actually I’m going to start it later this afternoon, but you won’t get to see it until June). One note on the styling module, it requires an empty file named “shared.py” to share information between the modules and to provide the temporary global images for some graphic stuff. Again, more on that next month. | + | Fondamentalement, |
+ | |||
+ | Ainsi, l' | ||
+ | |||
+ | Setup_base_style est appelé depuis le menu de démarrage. Pour ceux qui ne se souviennent pas de ce qu'est la fonction de démarrage ou de l' | ||
+ | |||
+ | |||
+ | **The first few lines simply set up some general styling things (which we’ll discuss next month) and calls an imported Python module that I created and named mystyles_dark. This was an experiment I played around with back in December 2022. The idea was to create a Python based Theme for Tkinter programs. If you remember, a ttk Theme is simply a collection of styles that apply to various ttk widgets. I never got around to finishing that project, since once I got the basics of getting the styles for various widgets going, I then put the Python file on the back burner and dived head first into writing a true Tcl/Tk theme in Tcl. (That was an experience, let me tell you! Well, actually that story is going in a blog on my website.) Anyway, I digress. The Python style module has only one function, which is create_styles() which handles all of the stylings. It requires the style object (in this case sty (actually a better name than style, huh? Less to type!)). By calling it, all the widgets it has code for (most of the ttk library) is now styled to a fairly dark color. I then grab the background color from the Python styling library and assign that to a global variable (background1). Then I call the TNotebook Tab styling routine. The frames that make up the “pages” of the TNotebook, are coloured by calling the function color_notebook_pages(). I then set the background through style.configure to be the same background as the rest of the project. All the labels, since I mix Tk Labels and ttk.TLabels on the pages of the TNotebook need to be set to proper background and foreground colors, which is what the function fix_labels() does. Finally, I make sure that the Tabs positions are set to the default position and set the title for the project and center it into the screen (code shown top right).** | ||
+ | |||
+ | Les premières lignes mettent simplement en place quelques éléments de style généraux (dont nous parlerons le mois prochain) et appellent un module Python importé que j'ai créé et nommé mystyles_dark. Il s'agit d'une expérience avec laquelle j'ai joué en décembre 2022. L' | ||
+ | |||
+ | |||
+ | **We’ll talk about the Python styling module next month (actually I’m going to start it later this afternoon, but you won’t get to see it until June). One note on the styling module, it requires an empty file named “shared.py” to share information between the modules and to provide the temporary global images for some graphic stuff. Again, more on that next month. | ||
LATE NOTE: | LATE NOTE: | ||
- | After I wrote the article, I went back and looked at the mystyles.dark module. I said that pretty much as soon as I had gotten it to its current state, I pushed it onto the back burner in order to work on the tcl theme file. When I looked with a bit of a critical eye, I noticed that the module was missing many ttk widgets and what was there was so very minimal that it was pretty much useless outside of setting background/ | + | After I wrote the article, I went back and looked at the mystyles.dark module. I said that pretty much as soon as I had gotten it to its current state, I pushed it onto the back burner in order to work on the tcl theme file. When I looked with a bit of a critical eye, I noticed that the module was missing many ttk widgets and what was there was so very minimal that it was pretty much useless outside of setting background/ |
- | So, there is a new function that is called add_options(toplevel) and requires the toplevel name. For use with PAGE, simply send in _top1 or whatever your toplevel name is. The other is a simple function called get_version(), | + | Nous parlerons du module de style Python le mois prochain (en fait, je vais le commencer plus tard dans l' |
+ | |||
+ | NOTE TARDIVE : | ||
+ | |||
+ | Après avoir écrit cet article, je suis retourné voir le module mystyles.dark. J'ai dit qu'à peu près dès que je l' | ||
+ | |||
+ | |||
+ | **So, there is a new function that is called add_options(toplevel) and requires the toplevel name. For use with PAGE, simply send in _top1 or whatever your toplevel name is. The other is a simple function called get_version(), | ||
As I usually do, I’ve created a repository on my GitHub page at https:// | As I usually do, I’ve created a repository on my GitHub page at https:// | ||
- | Until next time, as always; stay safe, healthy, positive and creative! | + | Until next time, as always; stay safe, healthy, positive and creative!** |
+ | |||
+ | Il existe donc une nouvelle fonction appelée add_options(toplevel) qui requiert le nom du toplevel. Pour une utilisation avec PAGE, il suffit d' | ||
+ | |||
+ | Comme je le fais habituellement, | ||
+ | |||
+ | Jusqu' |
issue193/python.1685339196.txt.gz · Dernière modification : 2023/05/29 07:46 de d52fr