Tkinter (22) standardized appearance and options database

Time:2021-1-26

Use the options database to set the default option values of the components. In addition, you can use the preset file. xdefaults fileEach part has a category name, which is basically the same as the category constructor, for example, the button isButtonIt can also be named by itself; another name is just like the instance name. The default name is a meaningless numeric name. Therefore, each component will have a similar inherited name, such asFrame.Text.Button(button in text of frame) or.mainFrame.messageText.panicButtonThe first point represents the root window

Create a new class and name itJukebox

class Jukebox(tk.Frame):
    def __init__(self, master):
        '''Constructor for the Jukebox class'''
        tk.Frame.__init__(self, master, class_='Jukebox')
        self.__createWidgets()
        ...

Create a button instance and name itpanicButton

    self.panic = tk.Button(self, name='panicButton', text='Panic', ...)

Preset files

Each line in the file specifies one or more option values in the following format:

app option-pattern: value

option-pattern: value

The first line sets the option named app

The second line sets the options for all applications

xparrot*background: LimeGreen

Set all background options in the application name xparrot to limegreen-nameTo set the app name)

Syntax of option pattern:{{*|.}name}...option

{ }Representatives are optional,...It means repeatable,optionRepresents the name of the option,nameIt can be upper case part class name or lower case instance name

*font: times 24

The options for all fonts are set to times 24,*Represents a loosely bound symbol, indicating that the setting can be applied to any application, any font option anywhere

*Listbox.font: lucidatypewriter 14

.Represents a tight binding symbol, which means that this setting only applies to font options of all parts in the listbox class

xparrot*Jukebox*background: PapayaWhip

All default background values of all components in the jukebox where xparrot is applied are papayawhip

xparrot*Jukebox.background: NavajoWhite

In the application of xparrot, the default background value of jukebox is Navajo white

Resource matching rules

In the following example, the second item more clearly indicates the background of the listbox than the first item. In the case of listbox, the background will take the second item

*background: LimeGreen

*Listbox*background: FloralWhite

The order of resource matching

  1. .Prior to*
  2. examplebe better thancategory(for example, panicbutton is better than button)
  3. Multi level in depthbe better thanShallow(e.g. * button * font is better than * font)
  4. If the two rules have the same number of levels, theEarlier name, better thanLater name(xparrot * font is better than * button * font)

This work adoptsCC agreementReprint must indicate the author and the link of this article


Jason Yang