issue183:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue183:python [2022/07/29 21:15] – créée d52fr | issue183:python [2022/08/02 15:10] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Sorry about not being able to continue the topic last month, but we are back. Let’s jump right into the project where we left on in issue #181. Here is what the latest version of the program looks like (image shown bottom right) | + | **Sorry about not being able to continue the topic last month, but we are back. Let’s jump right into the project where we left on in issue #181. Here is what the latest version of the program looks like (image shown bottom right) |
Just to remind you, there are three possible color settings for most widgets. A Selected Background color, an Active Background color, and an Inactive Background color. Here is how they work… | Just to remind you, there are three possible color settings for most widgets. A Selected Background color, an Active Background color, and an Inactive Background color. Here is how they work… | ||
Ligne 8: | Ligne 8: | ||
Inactive Background Color - The background of all other tabs (widgets) | Inactive Background Color - The background of all other tabs (widgets) | ||
- | 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:: |
- | 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. | + | 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. | ||
We have three static text widgets (ttk:: | We have three static text widgets (ttk:: | ||
Ligne 20: | Ligne 32: | ||
Then, when you want the colorchooser dialog to be shown, the syntax is: | Then, when you want the colorchooser dialog to be shown, the syntax is: | ||
+ | |||
+ | 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, | result = tkColorChooser.askcolor(color, | ||
- | The parameters are: | + | |
+ | **The parameters are: | ||
Color - The initial color that the dialog will show. If you leave it blank, it will default to light gray. | Color - The initial color that the dialog will show. If you leave it blank, it will default to light gray. | ||
Title - The title for the dialog window | Title - The title for the dialog window | ||
Ligne 30: | Ligne 57: | ||
The return value comes back as a tuple that comes in as (triple, color) – assuming the user clicks the Ok button. Triple is itself a tuple of (Red, Green, Blue) values (in the range of [0, 255]) and color is the regular Tkinter hex value object. If the user clicks the Cancel button, the return value will be (None, | The return value comes back as a tuple that comes in as (triple, color) – assuming the user clicks the Ok button. Triple is itself a tuple of (Red, Green, Blue) values (in the range of [0, 255]) and color is the regular Tkinter hex value object. If the user clicks the Cancel button, the return value will be (None, | ||
- | 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).** |
- | The TEntry widgets each have a textvar that allows the program to find the entered data simply and they are TEntry1Data, | + | 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, | ||
Now let’s look at some of the code that is attached to the buttons. Remember, we call the code as on_bgColor(n) where n is the value we gave the button. | Now let’s look at some of the code that is attached to the buttons. Remember, we call the code as on_bgColor(n) where n is the value we gave the button. | ||
Ligne 38: | Ligne 75: | ||
For the most part, the first portion of the code is generated by PAGE, showing each of the values passed into the function. I added the global statement and the if debug: portion of the code, so I can hide the value printout when I’m done with debugging. | For the most part, the first portion of the code is generated by PAGE, showing each of the values passed into the function. I added the global statement and the if debug: portion of the code, so I can hide the value printout when I’m done with debugging. | ||
- | 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. ** |
- | We simply use an if statement to check the value passed in. | + | 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. | ||
which = args[0] | which = args[0] | ||
Ligne 56: | Ligne 102: | ||
colr, title=f" | colr, title=f" | ||
- | 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.** |
- | 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). | + | 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). | ||
+ | |||
+ | 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' | ||
- | 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/ | ||
- | 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. |
In order to get the colors to actually be applied to the widgets, we need to apply the colorsets. | In order to get the colors to actually be applied to the widgets, we need to apply the colorsets. | ||
Ligne 76: | Ligne 146: | ||
style = ttk.Style() | style = ttk.Style() | ||
- | 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).** |
- | Each ttk widget has a different set of states that can be set. These can include states like “Active”, | + | 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”, | ||
As I said, every widget is different as to the states that it will support. For example, the TButton has an “Active”, | As I said, every widget is different as to the states that it will support. For example, the TButton has an “Active”, | ||
- | 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?.** |
- | 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). | + | 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). | ||
You can do this with many of the “normal” attributes that you don’t get to change within PAGE, since any ttk widget can have any style available. Imagine having 100 ttk widgets each with its own style. It can be done! | You can do this with many of the “normal” attributes that you don’t get to change within PAGE, since any ttk widget can have any style available. Imagine having 100 ttk widgets each with its own style. It can be done! | ||
Ligne 90: | Ligne 184: | ||
Enough theory for this month. Next month, we’ll look at saving the colorsets into something that we can use and apply, and a way to write this information out so we can use it within other programs in a very simple way. | Enough theory for this month. Next month, we’ll look at saving the colorsets into something that we can use and apply, and a way to write this information out so we can use it within other programs in a very simple way. | ||
- | 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.1659122153.txt.gz · Dernière modification : 2022/07/29 21:15 de d52fr