Outils pour utilisateurs

Outils du site


issue185:python

Ceci est une ancienne révision du document !


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, ended up being one of them. I promised that we would continue with the Styles and Themes series of articles, so that’s what we’ll do. The information for each colorset is held in a dictionary. It looks something like that shown top right. You can see that there is a key that holds the name of the colorset and then another dictionary that holds a background/foreground designation and a hex value of the color. The back/fore color name is the key and the hex value of the color itself is the value of the dictionary entries. We simply add new colorsets to a global one to keep everything in sync. newset = { response: { “BG1”: lastbg1, “BG2”: lastbg2, “BG3”: lastbg3, “FG1”: lastfg1, “FG2”: lastfg2, “FG3”: lastfg3, } } 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). The program also has a utility that will display the dictionaries in a “viewer” form (shown right). Because it is a dictionary, the output with all the curly braces won’t look very pretty when displayed in text. To make this a bit nicer looking, we can use the pretty print library. Once we have formatted the data the way we want it, we can simply use the tk.text.insert method to place the data into the widget for display. The program also provides a way to create a separate file that can be imported into a program that you wrote that will allow you to use a single colorset or multiple colorsets with your own widget list. There is a form to do that from the button menu. When the “Write File” button is clicked, we create the export file. The first thing we do is to create the import section of the file. import tkinter as tk import tkinter.ttk as ttk from tkinter.constants import * Next, we write out the dictionaries for the colorset with the various sets that were selected.

newset={“Greg1”: {“BG1”: “gray54”, “BG2”: “gray86”, “BG3”: “gray64”, “FG1”: “white”, “FG2”: “black”, “FG3”: “black”}, “Vintage1”: {“BG1”: “#8E3200”, “BG2”: “#D7A86E”, “BG3”: “#A64B2A”, “FG1”: “white”, “FG2”: “black”, “FG3”: “black”}, “Vintage2”: {“BG1”: “#362706”, “BG2”: “#464E2E”, “BG3”: “#ACB992”, “FG1”: “white”, “FG2”: “white”, “FG3”: “black”}} 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. 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).

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). We then check for specific widgets that need to have special handling, like the TFrame, Treeview, TScrollbars and the TLabel widgets (shown next page, middle right). Finally, we can do any “standard” tk widgets. Again, we have to check if the widget supports the foreground and active background properties (shown next page, bottom right). Last but not least, we do an update on the Toplevel form so that all the changes will show up. 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. from ColorSetImport import newset, do_tk_widgets If your project will be supporting more than one colorset in the newset dictionary, you will need to determine which colorset to use then assign it to a global colorset. The next line assumes that you will use the first colorset in the dictionary. colorset=newset[0] Finally, to apply the colorset, simply call the do_tk_widgets function. do_tk_widgets(_top1, colorset) Where the first parameter is the form name and passing the selected colorset in as the second parameter. There is a demo program that shows how this works (shown left). I’ve put all the source code on my github repository at https://github.com/gregwa1953/FCM-185 Until next time, as always; stay safe, healthy, positive and creative!

issue185/python.1664773519.txt.gz · Dernière modification : 2022/10/03 07:05 de d52fr