issue183: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 | ||
issue183:python [2022/07/30 08:14] – d52fr | issue183:python [2022/08/02 15:10] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 9: | Ligne 9: | ||
Most of the action happens within the TNotebook. There are three main panels (all TLableframes (ttk:: | Most of the action happens within the TNotebook. There are three main panels (all TLableframes (ttk:: | ||
+ | |||
+ | Désolé de ne pas avoir pu poursuivre le sujet le mois dernier, mais nous sommes de retour. Reprenons le projet là où nous l' | ||
+ | |||
+ | Juste pour vous rappeler qu'il y a trois paramètres de couleurs possibles pour la plupart des widgets. Une couleur de fond sélectionnée, | ||
+ | |||
+ | Pour l' | ||
+ | Couleur d' | ||
+ | Couleur d' | ||
+ | Couleur d' | ||
+ | |||
+ | La plupart des actions se déroulent dans TNotebook. Il y a trois panneaux principaux (tous des TLableframes (ttk:: | ||
+ | |||
**Taking a closer look at the background colors TLableframe (the foreground works almost exactly the same, except for dealing with the foreground colors), we’ll concentrate on the background panel right now. | **Taking a closer look at the background colors TLableframe (the foreground works almost exactly the same, except for dealing with the foreground colors), we’ll concentrate on the background panel right now. | ||
Ligne 22: | Ligne 34: | ||
result = tkColorChooser.askcolor(color, | result = tkColorChooser.askcolor(color, | ||
+ | |||
+ | En regardant de plus près les couleurs d' | ||
+ | |||
+ | Nous avons trois widgets de texte statique (ttk:: | ||
+ | |||
+ | Voici ce dont vous avez besoin pour utiliser la boîte de dialogue ColorSelect. | ||
+ | |||
+ | D' | ||
+ | |||
+ | import tkinter.colorchooser as colorchooser | ||
+ | |||
+ | Ensuite, quand vous voulez que le dialogue colorSelect soit montré, la syntaxe est : | ||
+ | |||
+ | result = tkColorChooser.askcolor(color, | ||
+ | |||
**The parameters are: | **The parameters are: | ||
Ligne 31: | Ligne 58: | ||
When the dialog is shown, the user can use the sliders to create a color they like, enter a hex value (starting with “#”) into the entry box, or enter one of the predefined Tkinter colors as a string (like “sky blue”). Any time you enter something into the entry box of the dialog, you MUST press the Enter key to “set” the information before you click the ok button (top right).** | When the dialog is shown, the user can use the sliders to create a color they like, enter a hex value (starting with “#”) into the entry box, or enter one of the predefined Tkinter colors as a string (like “sky blue”). Any time you enter something into the entry box of the dialog, you MUST press the Enter key to “set” the information before you click the ok button (top right).** | ||
+ | |||
+ | Les paramètres sont les suivants : | ||
+ | Color - La couleur initiale que la boîte de dialogue affichera. Si vous laissez ce paramètre vide, la couleur par défaut sera le gris clair. | ||
+ | Title - Le titre de la fenêtre de dialogue. | ||
+ | Parent - Le formulaire de niveau supérieur sur lequel vous souhaitez que la boîte de dialogue apparaisse. En général, il s'agit de votre formulaire principal. | ||
+ | |||
+ | La valeur de retour est un tuple qui se présente sous la forme (triple, couleur) - en supposant que l' | ||
+ | |||
+ | Lorsque le dialogue est affiché, l' | ||
+ | |||
**The TEntry widgets each have a textvar that allows the program to find the entered data simply and they are TEntry1Data, | **The TEntry widgets each have a textvar that allows the program to find the entered data simply and they are TEntry1Data, | ||
Ligne 39: | Ligne 76: | ||
Next we look at the argument being passed in (which is the number of the button) and get the last color that was assigned to that background color group from the globals. This gives us the starting color for the Color Select dialog based on which of the three background colors we wish to work on. ** | Next we look at the argument being passed in (which is the number of the button) and get the last color that was assigned to that background color group from the globals. This gives us the starting color for the Color Select dialog based on which of the three background colors we wish to work on. ** | ||
+ | |||
+ | Les widgets TEntry possèdent chacun une variable texte qui permet au programme de retrouver simplement les données saisies. Il s'agit de TEntry1Data, | ||
+ | |||
+ | Examinons maintenant une partie du code qui est attaché aux boutons. Rappelez-vous que nous appelons le code on_bgColor(n) où n est la valeur que nous avons donnée au bouton. | ||
+ | |||
+ | Pour l' | ||
+ | |||
+ | Ensuite, nous examinons l' | ||
+ | |||
**We simply use an if statement to check the value passed in. | **We simply use an if statement to check the value passed in. | ||
Ligne 57: | Ligne 103: | ||
We set the starting color, the title of the dialog, and the name of the parent Toplevel so the dialog gets centered properly. The response from the user comes back as the variable result.** | We set the starting color, the title of the dialog, and the name of the parent Toplevel so the dialog gets centered properly. The response from the user comes back as the variable result.** | ||
+ | |||
+ | Nous utilisons simplement une instruction if pour vérifier la valeur transmise : | ||
+ | |||
+ | which = args[0] | ||
+ | if which == 1: | ||
+ | colr = lastbg1 | ||
+ | elif which == 2: | ||
+ | colr = lastbg2 | ||
+ | elif which == 3: | ||
+ | colr = lastbg3 | ||
+ | |||
+ | C'est assez simple, non ? Maintenant que nous avons les données de base, nous pouvons appeler la boîte de dialogue colorchooser : | ||
+ | |||
+ | result = colorchooser.askcolor( | ||
+ | |||
+ | colr, title=f "Enter Color for Background # | ||
+ | |||
+ | Nous définissons la couleur de départ, le titre de la boîte de dialogue, et le nom du parent Toplevel pour que la boîte de dialogue soit centrée correctement. La réponse de l' | ||
+ | |||
**Since the dialog returns two values (the first being a tuple containing R,G and B value, and the second returning the hex value of the color chosen), we need to check at least one of the two values. Tkinter doesn’t allow for the RGB values to be used directly, so we want to check the hex value which comes in as the second value. In addition, the values can be either valid color values or two None values. I opted to check the second value, which is what we will be using to set the color if there is a value there. The first thing we to do is make sure that the user didn’t click the Cancel button (bottom left). | **Since the dialog returns two values (the first being a tuple containing R,G and B value, and the second returning the hex value of the color chosen), we need to check at least one of the two values. Tkinter doesn’t allow for the RGB values to be used directly, so we want to check the hex value which comes in as the second value. In addition, the values can be either valid color values or two None values. I opted to check the second value, which is what we will be using to set the color if there is a value there. The first thing we to do is make sure that the user didn’t click the Cancel button (bottom left). | ||
If the returned value is not None, we then set the hex value into the TEntry widget and set the background color of the button to visually show the color that was chosen. The same code is basically used for the other two possible button/ | If the returned value is not None, we then set the hex value into the TEntry widget and set the background color of the button to visually show the color that was chosen. The same code is basically used for the other two possible button/ | ||
+ | |||
+ | Puisque le dialogue retourne deux valeurs (la première étant un tuple contenant les valeurs R, G et B, et la seconde retournant la valeur hexadécimale de la couleur choisie), nous devons vérifier au moins une des deux valeurs. Tkinter ne permet pas d' | ||
+ | |||
+ | Si la valeur renvoyée n'est pas None, nous plaçons la valeur hexadécimale dans le widget TEntry et définissons la couleur d' | ||
+ | |||
**The Foreground color (the text color) is basically the same process, so we’ll skip it here for this month. So here is what a simple color combination works out to be. Selected Background is Tab 1, Tab 2 is the Active background (where the mouse is hovering), and Tabs 3 through 5 are the Inactive Backgrounds. | **The Foreground color (the text color) is basically the same process, so we’ll skip it here for this month. So here is what a simple color combination works out to be. Selected Background is Tab 1, Tab 2 is the Active background (where the mouse is hovering), and Tabs 3 through 5 are the Inactive Backgrounds. | ||
Ligne 77: | Ligne 147: | ||
In order to manipulate any of the styles for any of the ttk widgets, we have to modify the style.map structure. Since we want to change the TNotebook tabs first, we start there (top right).** | In order to manipulate any of the styles for any of the ttk widgets, we have to modify the style.map structure. Since we want to change the TNotebook tabs first, we start there (top right).** | ||
+ | |||
+ | La couleur d' | ||
+ | |||
+ | Pour que les couleurs soient réellement appliquées aux widgets, nous devons appliquer les jeux de couleurs. | ||
+ | |||
+ | En regardant l' | ||
+ | |||
+ | def set_colors() : | ||
+ | |||
+ | global lastbg1, lastbg2, lastbg3, lastfg1, lastfg2, lastfg3 | ||
+ | |||
+ | # ================================================= | ||
+ | |||
+ | style = ttk.Style() | ||
+ | |||
+ | Afin de manipuler n' | ||
+ | |||
**Each ttk widget has a different set of states that can be set. These can include states like “Active”, | **Each ttk widget has a different set of states that can be set. These can include states like “Active”, | ||
Ligne 83: | Ligne 170: | ||
Many of the ttk widgets also need to be configured, sort of like the “standard” Tk widgets do, to set many of the various attributes available. To do this, we need to create a “generic” style for that widget, then apply it using the style attribute. (Gets somewhat confusing, huh?.** | Many of the ttk widgets also need to be configured, sort of like the “standard” Tk widgets do, to set many of the various attributes available. To do this, we need to create a “generic” style for that widget, then apply it using the style attribute. (Gets somewhat confusing, huh?.** | ||
+ | |||
+ | Chaque widget ttk possède un ensemble différent d' | ||
+ | |||
+ | Comme je l'ai dit, chaque widget est différent quant aux états qu'il supporte. Par exemple, le TButton a un état « Active », un « Disabled » et un « Readonly », qui ont tous des attributs séparés qui peuvent être définis (en bas à gauche). | ||
+ | |||
+ | La plupart des widgets ttk doivent également être configurés, | ||
+ | |||
**We do this by using the style.configure function, starting with our named style along with the widget, then set the attributes as we want them, then finally applying the style via the style attribute (bottom right). | **We do this by using the style.configure function, starting with our named style along with the widget, then set the attributes as we want them, then finally applying the style via the style attribute (bottom right). | ||
Ligne 91: | Ligne 185: | ||
Until next time, as always; stay safe, healthy, positive and creative!** | Until next time, as always; stay safe, healthy, positive and creative!** | ||
+ | |||
+ | Nous le faisons en utilisant la fonction style.configure, | ||
+ | |||
+ | Vous pouvez faire cela avec de nombreux attributs « normaux » que vous ne pouvez pas modifier dans PAGE, puisque n' | ||
+ | |||
+ | Assez de théorie pour ce mois-ci. Le mois prochain, nous verrons comment sauvegarder les jeux de couleurs dans quelque chose que nous pouvons utiliser et appliquer, et comment écrire cette information pour pouvoir l' | ||
+ | |||
+ | Jusqu' | ||
+ |
issue183/python.1659161688.txt.gz · Dernière modification : 2022/07/30 08:14 de d52fr