issue193: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 | ||
issue193:python [2023/05/29 08:14] – d52fr | issue193:python [2023/05/31 12:15] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 10: | Ligne 10: | ||
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.** | ||
+ | |||
+ | 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. | **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. | ||
Ligne 16: | Ligne 29: | ||
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.** | ||
+ | |||
+ | 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 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. | ||
Ligne 28: | Ligne 48: | ||
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).** | ||
+ | |||
+ | 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. | **I deleted many of the elif statements since they are pretty much all the same thing with the only thing changing is the position. | ||
Ligne 35: | Ligne 68: | ||
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. | ||
Ligne 41: | Ligne 81: | ||
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.** | ||
+ | |||
+ | Nous créons à nouveau une instance de l' | ||
+ | |||
+ | 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).** | **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. | **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. | ||
Ligne 49: | Ligne 101: | ||
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/ | ||
+ | |||
+ | 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(), | **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(), | ||
Ligne 55: | Ligne 114: | ||
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.1685340880.txt.gz · Dernière modification : 2023/05/29 08:14 de d52fr