issue188:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue188:python [2023/01/03 10:32] – créée auntiee | issue188:python [2023/01/06 14:50] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Back in FCM#181, I talked about Themes and Styles and how I was trying to create a style map for the ttk TNotebook. That was just back in May. Here it is, December, and my “work life” has been busy with writing my PAGE book, trying to do support for PAGE with Don Rozenberg, and trying to live with the medical issues that I’ve been “blessed” with. | + | **Back in FCM#181, I talked about Themes and Styles and how I was trying to create a style map for the ttk TNotebook. That was just back in May. Here it is, December, and my “work life” has been busy with writing my PAGE book, trying to do support for PAGE with Don Rozenberg, and trying to live with the medical issues that I’ve been “blessed” with. |
I have gotten to the chapter in my book about the ttk widget set, and I wanted to try to explain the complicated concept of Themes and Styles. | I have gotten to the chapter in my book about the ttk widget set, and I wanted to try to explain the complicated concept of Themes and Styles. | ||
Ligne 11: | Ligne 11: | ||
• A focus element that changes color when the Button has input focus, | • A focus element that changes color when the Button has input focus, | ||
• A padding element, and | • A padding element, and | ||
- | • A label element. This element contains the label text, image or both. | + | • A label element. This element contains the label text, image or both.** |
- | Every widget has a style that is controlled by the Theme. Now here is where it gets somewhat frustrating. Each Operating System has its own built-in themes, and some Operating Systems have more built-in themes than others. Since we are a Linux-based magazine, I’ll give them to you now, and then give you the ones for the other Operating Systems. | + | Dans le numéro 181 du FCM, j'ai parlé des thèmes et des styles et de la façon dont j' |
+ | |||
+ | J'en suis arrivé au chapitre de mon livre sur l' | ||
+ | |||
+ | Tout commence par un style. Les styles contrôlent l' | ||
+ | |||
+ | Comme tout commence par un style pour les widgets, nous allons nous pencher sur une seule classe de widgets pour commencer. Nous allons commencer par la classe ttk Button. Le style de cette classe de widget s' | ||
+ | |||
+ | Chaque style de widget est composé d'un ou plusieurs éléments. Un bouton typique comporte quatre éléments : | ||
+ | ••la bordure autour de l' | ||
+ | ••un élément de mise en évidence qui change de couleur lorsque le bouton a le focus d' | ||
+ | ••un élément de remplissage, | ||
+ | ••un élément d' | ||
+ | |||
+ | |||
+ | **Every widget has a style that is controlled by the Theme. Now here is where it gets somewhat frustrating. Each Operating System has its own built-in themes, and some Operating Systems have more built-in themes than others. Since we are a Linux-based magazine, I’ll give them to you now, and then give you the ones for the other Operating Systems. | ||
It’s fairly obvious that Linux got the short end of the stick when it comes to the sheer number of themes that come with the OS. You should also notice that the four themes that are “native” to Linux are also available on the other two Operating Systems. This makes it nice – if you are planning on running your application under the three OS systems, you should really stick with one of those four. | It’s fairly obvious that Linux got the short end of the stick when it comes to the sheer number of themes that come with the OS. You should also notice that the four themes that are “native” to Linux are also available on the other two Operating Systems. This makes it nice – if you are planning on running your application under the three OS systems, you should really stick with one of those four. | ||
- | When you decide to start working on your own styles, and get ready to wrap them all up in a theme, you should base everything on one theme. For Linux users like us, the simplest theme to start with would be the default theme. Not only does every OS have the default theme built in, but each OS has a different “default” theme that, if you don’t set a specific theme, it will be used by “default”. A logical person would think that that would be default. And you are right, if you are talking about Linux. For Windows, the default theme is winnative, not default and for Mac OS X it is aqua. Talk about confusion! | + | When you decide to start working on your own styles, and get ready to wrap them all up in a theme, you should base everything on one theme. For Linux users like us, the simplest theme to start with would be the default theme. Not only does every OS have the default theme built in, but each OS has a different “default” theme that, if you don’t set a specific theme, it will be used by “default”. A logical person would think that that would be default. And you are right, if you are talking about Linux. For Windows, the default theme is winnative, not default and for Mac OS X it is aqua. Talk about confusion!** |
+ | |||
+ | Chaque widget a un style qui est contrôlé par le thème. C'est ici que cela devient quelque peu frustrant. Chaque système d' | ||
+ | |||
+ | Il est évident que Linux n'est pas le mieux loti en ce qui concerne le nombre de thèmes fournis avec le système d' | ||
+ | |||
+ | Lorsque vous décidez de commencer à travailler sur vos propres styles et que vous êtes prêt à les intégrer dans un thème, vous devez tout baser sur un seul thème. Pour les utilisateurs de Linux comme nous, le thème le plus simple pour commencer est le thème par défaut. Tous les systèmes d' | ||
- | Anyway, we’ll use the ttk Button – to see what our styles can do. Now, remember back a few paragraphs, we touched on the Elements. So for the ttk Button class, the style name is TButton. The elements are assembled into an “empty box”. For a Button under the default theme, they are assembled from the outside in using this order. Border, focus, padding, and, finally, label. Just like the diagram above. | + | **Anyway, we’ll use the ttk Button – to see what our styles can do. Now, remember back a few paragraphs, we touched on the Elements. So for the ttk Button class, the style name is TButton. The elements are assembled into an “empty box”. For a Button under the default theme, they are assembled from the outside in using this order. Border, focus, padding, and, finally, label. Just like the diagram above. |
Each of the elements has a “sticky” attribute that defines which of the sides of the “box” that it will stick to. If, for example, an element has the sticky attribute of ‘ew’, it would expand and stretch in order to reach the right and left sides of our imaginary box. Since there are no definitions for the north or south, it doesn’t have to stretch to the top and bottom of the box. Any of the four points may be defined, it is not necessary for all four to be defined. | Each of the elements has a “sticky” attribute that defines which of the sides of the “box” that it will stick to. If, for example, an element has the sticky attribute of ‘ew’, it would expand and stretch in order to reach the right and left sides of our imaginary box. Since there are no definitions for the north or south, it doesn’t have to stretch to the top and bottom of the box. Any of the four points may be defined, it is not necessary for all four to be defined. | ||
- | Most of the ttk styles have a layout that organizes the different layers that make up the widget style. Some widget classes don’t have a layout. As long as the widget has a layout, you can use the following commands in the Python REPL to get information on the layout. (shown top right). | + | Most of the ttk styles have a layout that organizes the different layers that make up the widget style. Some widget classes don’t have a layout. As long as the widget has a layout, you can use the following commands in the Python REPL to get information on the layout. (shown top right).** |
- | Now that is as clear as mud, isn’t it? When a widget class has a layout, it will be returned as a list of tuples which have the element name and a dictionary that describes that element. (I just added a bunch more mud, didn’t I?). | + | Quoi qu'il en soit, nous allons utiliser le bouton ttk, pour voir ce que nos styles peuvent faire. Rappelez-vous, |
+ | |||
+ | Chacun de ces éléments possède un attribut « sticky » (collant) qui définit le côté de la « boîte » sur lequel il va se coller. Si, par exemple, un élément a « ew » (est-ouest) pour l' | ||
+ | |||
+ | La plupart des styles ttk ont une disposition qui organise les différentes couches qui composent le style du widget. Certaines classes de widgets n'ont pas de disposition. Tant que le widget a une disposition (layout), vous pouvez utiliser les commandes suivantes dans le REPL Python pour obtenir des informations sur ce layout (en haut à droite). | ||
+ | |||
+ | |||
+ | **Now that is as clear as mud, isn’t it? When a widget class has a layout, it will be returned as a list of tuples which have the element name and a dictionary that describes that element. (I just added a bunch more mud, didn’t I?). | ||
Think of it this way. The outermost element is the border. It has its sticky attribute which is, in this case, north, south, east and west (so the border will expand to all sides of the widget), and a border attribute that has a width of 1 pixel. It also has a child element. In this case, it's the focus element. It has a sticky attribute that also expands north, south, east and west. The focus element doesn’t have a border, but it does have a child, which is the padding element. That has only one attribute – as you might guess, it’s the sticky attribute, and again it’s north, south, east and west, and it ALSO has a child element – the label element. It has a sticky attribute as well and, yet again, it is ‘nsew’. | Think of it this way. The outermost element is the border. It has its sticky attribute which is, in this case, north, south, east and west (so the border will expand to all sides of the widget), and a border attribute that has a width of 1 pixel. It also has a child element. In this case, it's the focus element. It has a sticky attribute that also expands north, south, east and west. The focus element doesn’t have a border, but it does have a child, which is the padding element. That has only one attribute – as you might guess, it’s the sticky attribute, and again it’s north, south, east and west, and it ALSO has a child element – the label element. It has a sticky attribute as well and, yet again, it is ‘nsew’. | ||
- | Now, it’s time for me to pull on my “Wellies” (Rubber Boots for those who aren’t Brits, either in family, or location, or heart), and stomp around a little bit more. Each of those elements can (but don’t have to) have options. I’m just going to dump out the options for each of the elements (below). | + | Now, it’s time for me to pull on my “Wellies” (Rubber Boots for those who aren’t Brits, either in family, or location, or heart), and stomp around a little bit more. Each of those elements can (but don’t have to) have options. I’m just going to dump out the options for each of the elements (below).** |
+ | |||
+ | C'est clair comme de la boue, n' | ||
+ | |||
+ | Pensez-y de cette façon. L' | ||
+ | |||
+ | Maintenant, il est temps pour moi d' | ||
- | These are the easiest things that we can change to make our style look the way we want. Notice that I said the easiest things. There can also be images, but that is a discussion for another article. For now, we’ll go back to the element options. | + | **These are the easiest things that we can change to make our style look the way we want. Notice that I said the easiest things. There can also be images, but that is a discussion for another article. For now, we’ll go back to the element options. |
Before we start changing things, let’s take a look at how the TButton looks on a “normal day” under the default Linux theme. | Before we start changing things, let’s take a look at how the TButton looks on a “normal day” under the default Linux theme. | ||
Ligne 38: | Ligne 74: | ||
Nothing fancy. First we set up a custom style for a specific TButton, then set (in this case) the foreground, background and font. Then, when we place the TButton, we can use the standard configure method to set the text, and the style to our Custom.TButton style. | Nothing fancy. First we set up a custom style for a specific TButton, then set (in this case) the foreground, background and font. Then, when we place the TButton, we can use the standard configure method to set the text, and the style to our Custom.TButton style. | ||
- | Not hard at all. Now let’s customize some TRadiobuttons and TCheckbuttons. Here they are in their “native” state. | + | Not hard at all. Now let’s customize some TRadiobuttons and TCheckbuttons. Here they are in their “native” state.** |
- | You can customize the indicators so that they use your own customized graphics. At a minimum, you’ll need to have three images for each class. One for unselected, one for selected, and one for disabled. | + | Ce sont les choses les plus simples que nous pouvons changer pour que notre style soit comme nous le voulons. Remarquez que j'ai dit les choses les plus faciles. Il peut aussi y avoir des images, mais ce sera le sujet d'un autre article. Pour l' |
+ | |||
+ | Avant de commencer à modifier les choses, regardons à quoi ressemble TButton lors d'une « journée normale » sous le thème Linux par défaut. | ||
+ | |||
+ | Très fade. Il ressemble à ce que l'on pourrait imaginer pour un bouton. Maintenant, pour définir quoi que ce soit, nous utiliserions le schéma général de stylename.configure(option=value) pour définir les choses à notre manière. Définissons donc les options afin d' | ||
+ | |||
+ | Rien d' | ||
+ | |||
+ | Pas difficile du tout. Maintenant, personnalisons quelques TRadiobuttons et TCheckbuttons. Les voici dans leur état « natif ». | ||
+ | |||
+ | |||
+ | **You can customize the indicators so that they use your own customized graphics. At a minimum, you’ll need to have three images for each class. One for unselected, one for selected, and one for disabled. | ||
Here is a quick reference image of all six of the indicators that I used to customize the TRadiobuttons and the TCheckbuttons. Remember, I never presented myself as an artist! | Here is a quick reference image of all six of the indicators that I used to customize the TRadiobuttons and the TCheckbuttons. Remember, I never presented myself as an artist! | ||
Ligne 48: | Ligne 95: | ||
First, let’s create image objects – keeping a global version – so that Python’s garbage collection doesn’t wipe our images out of existence. Here we are setting up the TRadiobuttons (bottom right). | First, let’s create image objects – keeping a global version – so that Python’s garbage collection doesn’t wipe our images out of existence. Here we are setting up the TRadiobuttons (bottom right). | ||
- | I used the shared module scheme in order to keep from having to declare a tonne of global variables. The shared module is an empty file in the project folder – that all modules can read and write to. The file can be named anything, as long as it has the extension of “.py”, and that you import it into any other Python files that will use it. By using the shared module scheme, it is much easier. Now that we have all of the Radiobutton images set into image objects, we then can create the element that we will use (below). | + | I used the shared module scheme in order to keep from having to declare a tonne of global variables. The shared module is an empty file in the project folder – that all modules can read and write to. The file can be named anything, as long as it has the extension of “.py”, and that you import it into any other Python files that will use it. By using the shared module scheme, it is much easier. Now that we have all of the Radiobutton images set into image objects, we then can create the element that we will use (below).** |
- | Once you get used to the idea, it’s not that hard, but it is the first time. So we define a new element with element_create and give it a name ‘custom.RBindicator’. We declare the type of element, which is image, and then we define the image name for the unselected state. Then, we define the selected and disabled states, and assign the images to them. | + | Vous pouvez personnaliser les indicateurs afin qu'ils utilisent vos propres éléments graphiques personnalisés. Au minimum, vous devrez disposer de trois images pour chaque classe. Une pour la non-sélection, |
+ | |||
+ | Voici une image de référence rapide des six indicateurs que j'ai utilisés pour personnaliser les TRadiobuttons et les TCheckbuttons. Rappelez-vous, | ||
+ | |||
+ | Nous devons maintenant coder le style et la disposition des éléments. | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | J'ai utilisé le schéma du module partagé afin de ne pas avoir à déclarer une tonne de variables globales. Le module partagé est un fichier vide dans le dossier du projet, ouvert en lecture et en écriture à tous les modules. Le fichier peut porter n' | ||
+ | |||
+ | |||
+ | **Once you get used to the idea, it’s not that hard, but it is the first time. So we define a new element with element_create and give it a name ‘custom.RBindicator’. We declare the type of element, which is image, and then we define the image name for the unselected state. Then, we define the selected and disabled states, and assign the images to them. | ||
Finally, we need to create the layout and replace the standard indicator element with our custom one (next page, top right). | Finally, we need to create the layout and replace the standard indicator element with our custom one (next page, top right). | ||
Ligne 60: | Ligne 118: | ||
Every widget has different elements and options that you can and probably should look at setting. Once you have a template for the layouts, it starts to become much easier. | Every widget has different elements and options that you can and probably should look at setting. Once you have a template for the layouts, it starts to become much easier. | ||
- | In wrapping up, I thought I’d leave you with a number of resources to help you out, if you become brave and try to create your own style sets. In a later article, I’ll show you how to create your own theme – once you’ve gotten the style sets for all the ttk widgets. | + | In wrapping up, I thought I’d leave you with a number of resources to help you out, if you become brave and try to create your own style sets. In a later article, I’ll show you how to create your own theme – once you’ve gotten the style sets for all the ttk widgets.** |
- | Given that there are so many options that so many don’t know about, I’m going to provide two generic links and a specific one. The first one is to the New Mexico Tech Tkinter 8.5 Manual. | + | Une fois que vous vous serez habitué à l' |
+ | |||
+ | Enfin, nous devons créer le layout et remplacer l' | ||
+ | |||
+ | C'est tout pour les TRadiobuttons. Suivez les mêmes étapes pour les TCheckbuttons, | ||
+ | |||
+ | C'est assez intimidant la première ou la deuxième fois que vous le faites, mais cela devient plus facile au fur et à mesure. | ||
+ | |||
+ | Chaque widget a des éléments et des options différents que vous pouvez et devriez probablement envisager de régler. Une fois que vous avez un modèle pour les mises en page, cela devient beaucoup plus facile. | ||
+ | |||
+ | En conclusion, j'ai pensé vous laisser avec un certain nombre de ressources pour vous aider, si vous devenez courageux et essayez de créer vos propres ensembles de style. Dans un prochain article, je vous montrerai comment créer votre propre thème - une fois que vous aurez obtenu les jeux de styles pour tous les widgets ttk. | ||
+ | |||
+ | |||
+ | **Given that there are so many options that so many don’t know about, I’m going to provide two generic links and a specific one. The first one is to the New Mexico Tech Tkinter 8.5 Manual. | ||
https:// | https:// | ||
Ligne 71: | Ligne 142: | ||
https:// | https:// | ||
- | (One word of warning. If you look back at the Button.label options, you will see one named ‘embossed’. I’ve NEVER been able to find out what this is supposed to do. It’s not in the documentation, | + | (One word of warning. If you look back at the Button.label options, you will see one named ‘embossed’. I’ve NEVER been able to find out what this is supposed to do. It’s not in the documentation, |
- | I will leave you with a theme that is created in Python, not tcl. This is a port of the Plastik theme which is part of the ttkwidgets Python library, which, like most themes, is written in Tcl and is pretty difficult to port to Python unless you are familiar with both languages. You can get the Plastic-python file at | + | Étant donné qu'il y a tant d' |
+ | https:// | ||
+ | |||
+ | Le deuxième est un bon point de départ pour la page du manuel « officiel » de la boîte à outils ttk, directement de source sûre chez le groupe tcl. C'est censé être ainsi : si ce n'est pas ici, c'est que ça ne peut pas être fait. | ||
+ | https:// | ||
+ | |||
+ | Enfin, je vais vous donner le lien vers la page « officielle » du manuel de ttk Button. Pour découvrir toutes les options et paramètres (et ce que beaucoup d' | ||
+ | https:// | ||
+ | |||
+ | (Un mot d' | ||
+ | |||
+ | |||
+ | **I will leave you with a theme that is created in Python, not tcl. This is a port of the Plastik theme which is part of the ttkwidgets Python library, which, like most themes, is written in Tcl and is pretty difficult to port to Python unless you are familiar with both languages. You can get the Plastic-python file at | ||
https:// | https:// | ||
Ligne 81: | Ligne 164: | ||
Follow the documentation on how to install it and how to get them into your own program projects. The docs are at https:// | Follow the documentation on how to install it and how to get them into your own program projects. The docs are at https:// | ||
- | Until next time, as always; stay safe, healthy, positive and creative! | + | Until next time, as always; stay safe, healthy, positive and creative!** |
+ | |||
+ | Je vous laisse avec un thème qui est créé en Python, pas en Tcl. C'est un portage du thème Plastik qui fait partie de la bibliothèque Python ttkwidgets, qui, comme la plupart des thèmes, est écrit en Tcl et est assez difficile à porter en Python à moins d' | ||
+ | https:// | ||
+ | |||
+ | Puisque j'ai mentionné le paquet ttkwidgets, vous pouvez l' | ||
+ | https:// | ||
+ | |||
+ | Suivez la documentation sur la façon de l' | ||
+ | Jusqu' | ||
issue188/python.1672738372.txt.gz · Dernière modification : 2023/01/03 10:32 de auntiee