issue185:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue185:python [2022/10/02 15:14] – créée auntiee | issue185:python [2022/10/04 17:33] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | + | **Sorry for missing last month. Between moving and my health problems, I had to put many things on hold for a while and this, unfortunately, | |
- | Sorry for missing last month. Between moving and my health problems, I had to put many things on hold for a while and this, unfortunately, | + | |
I promised that we would continue with the Styles and Themes series of articles, so that’s what we’ll do. | I promised that we would continue with the Styles and Themes series of articles, so that’s what we’ll do. | ||
Ligne 10: | Ligne 9: | ||
We simply add new colorsets to a global one to keep everything in sync. | We simply add new colorsets to a global one to keep everything in sync. | ||
+ | |||
+ | |||
+ | newset = { | ||
+ | response: { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | ColorSets.update(newset)** | ||
+ | | ||
+ | Désolé pour mon absence du mois dernier. Entre un déménagement et mes problèmes de santé, j'ai dû mettre beaucoup de choses en attente pendant un certain temps et celle-ci, malheureusement, | ||
+ | |||
+ | J'ai promis que nous continuerions la série d' | ||
+ | |||
+ | Les informations relatives à chaque jeu de couleurs sont contenues dans un dictionnaire. Il ressemble à celui illustré en haut à droite. | ||
+ | |||
+ | Vous pouvez voir qu'il y a une clé qui contient le nom du jeu de couleurs, puis un autre dictionnaire qui contient une désignation d' | ||
+ | |||
+ | Il suffit d' | ||
Ligne 24: | Ligne 46: | ||
ColorSets.update(newset) | ColorSets.update(newset) | ||
- | As you can see, we use the dictionary.update method to add the new one. But how do we save the “master” colorset to a file for retrieval later on? We use the pickle library. However, we have to be careful if we are going to be sharing this file with others, since by default newer versions of Python greater than 3.7 use a higher compression algorithm. Python 3.7 uses a level 4 compression. So to support all Python versions from 3.7 forward, we have to state that the level must be “pickle.DEFAULT_PROTOCOL (shown right). | + | |
+ | **As you can see, we use the dictionary.update method to add the new one. But how do we save the “master” colorset to a file for retrieval later on? We use the pickle library. However, we have to be careful if we are going to be sharing this file with others, since by default newer versions of Python greater than 3.7 use a higher compression algorithm. Python 3.7 uses a level 4 compression. So to support all Python versions from 3.7 forward, we have to state that the level must be “pickle.DEFAULT_PROTOCOL (shown right). | ||
The program also has a utility that will display the dictionaries in a “viewer” form (shown right). | The program also has a utility that will display the dictionaries in a “viewer” form (shown right). | ||
Ligne 40: | Ligne 63: | ||
from tkinter.constants import * | from tkinter.constants import * | ||
- | Next, we write out the dictionaries for the colorset with the various sets that were selected. | + | Next, we write out the dictionaries for the colorset with the various sets that were selected.** |
- | newset={" | + | Comme vous pouvez le voir, nous utilisons la méthode dictionary.update pour ajouter le nouveau jeu de couleurs. Mais comment sauvegarder le jeu de couleurs « maître » dans un fichier pour le retrouver plus tard ? Nous utilisons la bibliothèque pickle. Cependant, nous devons faire attention si nous voulons partager ce fichier avec d' |
+ | |||
+ | Le programme dispose également d'un utilitaire qui affiche les dictionnaires sous la forme d'une « visionneuse » ( à droite). | ||
+ | |||
+ | Comme il s'agit d'un dictionnaire, | ||
+ | |||
+ | Le programme permet également de créer un fichier séparé qui peut être importé dans un programme que vous avez écrit et qui vous permettra d' | ||
+ | |||
+ | La première chose que nous faisons est de créer la section d' | ||
+ | |||
+ | import tkinter as tk | ||
+ | |||
+ | import tkinter.ttk as ttk | ||
+ | |||
+ | from tkinter.constants import * | ||
+ | |||
+ | Ensuite, nous écrivons les dictionnaires pour le jeu de couleurs avec les différents jeux qui ont été sélectionnés. | ||
+ | |||
+ | |||
+ | **newset={" | ||
Now, we create the function that will apply the colorsets that will be used to all the widgets that happen to be on your form. | Now, we create the function that will apply the colorsets that will be used to all the widgets that happen to be on your form. | ||
Ligne 48: | Ligne 90: | ||
The first thing we have to do in the function is to apply a ttk style (shown below). In this case, we will be using the “default” style. However, this can be changed to whatever style your system will support. Then we create a number of lists that use the widget classes for all the possible widgets. There is a list for widgets that don’t support the active background property, one that has the widgets that won’t support the foreground property (which is just a Frame, but there could be more in the future) then we create a list of all the ttk widgets and finally a list of all the widgets that can be containers. | The first thing we have to do in the function is to apply a ttk style (shown below). In this case, we will be using the “default” style. However, this can be changed to whatever style your system will support. Then we create a number of lists that use the widget classes for all the possible widgets. There is a list for widgets that don’t support the active background property, one that has the widgets that won’t support the foreground property (which is just a Frame, but there could be more in the future) then we create a list of all the ttk widgets and finally a list of all the widgets that can be containers. | ||
- | In order to support the TNotebook and the TButton, we have to supply a map that will allow the various background colors and foreground colors (shown top right). | + | In order to support the TNotebook and the TButton, we have to supply a map that will allow the various background colors and foreground colors (shown top right).** |
- | At this point, we can get a list of all children of the Toplevel form. Once we have the list, we try to set all the background and foreground colors for those widgets (shown bottom right). | + | newset={" |
+ | |||
+ | Maintenant, nous créons la fonction qui appliquera les jeux de couleurs qui seront utilisés à tous les widgets qui se trouvent sur votre formulaire. | ||
+ | |||
+ | La première chose que nous devons faire dans la fonction est d' | ||
+ | |||
+ | Afin de supporter TNotebook et TButton, nous devons fournir une carte qui permettra différentes couleurs d' | ||
+ | |||
+ | |||
+ | **At this point, we can get a list of all children of the Toplevel form. Once we have the list, we try to set all the background and foreground colors for those widgets (shown bottom right). | ||
While we are working with this widget, we’ll check for any children and try to apply the background and foreground colors of them (shown next page, top right). | While we are working with this widget, we’ll check for any children and try to apply the background and foreground colors of them (shown next page, top right). | ||
Ligne 59: | Ligne 110: | ||
Last but not least, we do an update on the Toplevel form so that all the changes will show up. | Last but not least, we do an update on the Toplevel form so that all the changes will show up. | ||
+ | |||
+ | Toplevel.update()** | ||
+ | |||
+ | À ce stade, nous pouvons obtenir une liste de tous les enfants du formulaire Toplevel. Une fois que nous avons la liste, nous essayons de définir toutes les couleurs d' | ||
+ | |||
+ | Pendant que nous travaillons avec ce widget, nous vérifions s'il y a des enfants et nous essayons d' | ||
+ | |||
+ | Nous vérifions ensuite les widgets spécifiques qui doivent faire l' | ||
+ | |||
+ | Enfin, nous pouvons nous occuper de tous les widgets tk « standard ». Encore une fois, nous devons vérifier si le widget supporte les propriétés foreground et active background (voir page suivante, en bas à droite). | ||
+ | |||
+ | Dernier point important, nous faisons une mise à jour du formulaire Toplevel pour que tous les changements soient visibles. | ||
Toplevel.update() | Toplevel.update() | ||
- | This form may be imported into your project. It is named ColorSetImport.py. Now in your own project, you should import it like this. | + | |
+ | **This form may be imported into your project. It is named ColorSetImport.py. Now in your own project, you should import it like this. | ||
from ColorSetImport import newset, do_tk_widgets | from ColorSetImport import newset, do_tk_widgets | ||
Ligne 80: | Ligne 144: | ||
I’ve put all the source code on my github repository at https:// | I’ve put all the source code on my 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!** |
+ | |||
+ | Ce formulaire peut être importé dans votre projet. Il est nommé ColorSetImport.py. Dans votre propre projet, vous devez l' | ||
+ | |||
+ | from ColorSetImport import newset, do_tk_widgets | ||
+ | |||
+ | Si votre projet prend en charge plus d'un jeu de couleurs dans le dictionnaire newset, vous devrez déterminer le jeu de couleurs à utiliser et l' | ||
+ | |||
+ | colorset=newset[0] | ||
+ | |||
+ | Enfin, pour appliquer le jeu de couleurs, il suffit d' | ||
+ | |||
+ | do_tk_widgets(_top1, | ||
+ | |||
+ | où le premier paramètre est le nom du formulaire et le deuxième le jeu de couleurs sélectionné. | ||
+ | |||
+ | Il y a un programme de démonstration qui montre comment cela fonctionne (à gauche). | ||
+ | |||
+ | J'ai mis tout le code source sur mon dépôt github à https:// | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | |||
+ | // Texte en noir dans l' | ||
+ | |||
+ | **To load the colorset into the program at startup, we read the pickle file and assign the colorset to the global master.** | ||
+ | |||
+ | Pour charger au démarrage le jeu de couleurs dans le programme, nous lisons le fichier pickle et assignons le jeu de couleurs au maître global. | ||
+ | |||
+ | // Texte en noir dans l' | ||
+ | |||
+ | **Next, we set the background for the Toplevel widget** | ||
+ | |||
+ | Ensuite, nous réglons l' | ||
+ | |||
+ | // Texte en noir dans l' | ||
+ | |||
+ | **Now we can set “global” background and foreground colors by using the “.” widget class.** | ||
+ | |||
+ | Maintenant, nous réglons les couleurs « globales » d' |
issue185/python.1664716494.txt.gz · Dernière modification : 2022/10/02 15:14 de auntiee