Questions about using ttk.Style ()?

To provide an instance of the ttk.Style () class, this tkinter guide shows that the syntax is:

import ttk s=ttk.Style() 

When entering this command in IDLE, I noticed that ttk.Style () actually has a predefined argument, i.e.

 s=ttk.Style(master=None) 

I wrote the following test script:

 import tkinter as tk import tkinter.ttk as ttk class App(ttk.Frame): def __init__(self, parent): ttk.Frame.__init__(self, parent, style='App.TFrame', relief=tk.SUNKEN, border=10) self.parent = parent self.__createStyle() self.__createWidgets() def __createStyle(self): self.s = ttk.Style() self.s.configure('.', background='orange', border=100) self.s.configure('App.TFrame', background='yellow') self.s.configure('Btn.TButton', background='light blue', border=10) def __createWidgets(self): self._label = ttk.Label(self.parent, text='Label packed in root.') self._label.pack() self._btn = ttk.Button(self, style='Btn.TButton', command=self.__click, text='Button packed inside self or class App, which is a ttk.Frame') self._btn.pack() def __click(self): return print('Left Button Clicked!') class myWidget(ttk.Frame): def __init__(self, parent): ttk.Frame.__init__(self, parent, style='my.TFrame', relief=tk.GROOVE, border=10) self.parent = parent self.__createStyle() self.__createWidgets() def __createStyle(self): self.s = ttk.Style() self.s.configure('my.TFrame', background='purple') self.s.configure('my.TLabel', background='pink', border=10) self.s.configure('my.TEntry', foreground='red', border=10) def __createWidgets(self): self._label = ttk.Label(self, style='my.TLabel', text='myWidget Label packed in self or class myWidget, which is a ttk.Frame.') self._label.pack() self._entry = ttk.Entry(self, style='my.TEntry') self._entry.pack() if __name__ == "__main__": root = tk.Tk() root.title('Test Style') root.geometry('500x150') a = App(root) a.pack(fill='both', expand=1) b = myWidget(a) b.pack() root.mainloop() 

Question 1: When do I need to declare a master argument in ttk.Style() ? For instance. in the above script, if I write self.s = ttk.Style() and self.s = ttk.Style(master=self.parent) in class myWidget , I get the same result (see Figure 1).

Question 2: Is there a prefix s=ttk.Style() with self ? I get the same result as shown in fig. 1 with and without self prefix.

Question 3: If I rename 'my.TFrame' to the myWidget class as 'App.TFrame' (this name was used in the class App ), the background color of the class App also changes to purple (the same color as the class myWidget . Why did this happen given that the variable name is unique in different classes?

Question 4: The names 'App.TFrame' and 'my.TFrame' were called before they were declared. Why didn't python or tkinter complain or give an error but allow script execution?

Fig. 1

Picture 1

Fig2

Figure 2

+5
source share
2 answers

When do I need to declare a master argument in ttk.Style() ?

Probably never, except when tkinter does not support default root . When you pass None as the master, the wizard becomes the current root instance of the Tk class.

The main purpose of master ( root or any Tk widget) is to delegate an instance of Tk to Style , so that Style can execute Tcl > -related commands.

More, not less.


Is there a s=ttk.Style() prefix with self ?

It depends on your requirements. In the context of your code, self does not make sense because you are customizing styles in the __createStyle .

Otherwise, if you want to keep the link, the prefix with self. makes sense self.


If I rename my.TFrame to my.TFrame class as App.TFrame (this name was used in the class App ), the background color of the class App also changed to purple (the same color as class myWidget . This happened, given that the variable name unique in different classes?

Because both classes have the same frame style, therefore the same color. The created style is a global thing, it can be launched at runtime, and all relevant widgets will respond to these changes.


The names App.TFrame and my.TFrame were called before they were declared. Why didn't python or tkinter complain or give an error, but allow script execution?

Why do you think you should? When you pass something like <any_sensible_name>.<any_relevant_and_existing_basestyle> , ttk knows that you want to change the base style, so it implicitly creates one that inherits all the base properties.

Try this trick with something more meaningless, for example, your current style name without a dot ( ttk.Frame.__init__(..., style='AppTFrame', ...) ), which gives you the desired error:

 _tkinter.TclError: Layout AppTFrame not found 
+4
source

Only a partial answer, but I believe that @Bryan Oakley will illuminate us sooner or later.

Question 3:
If you use "App.TFrame" instead of "my.TFrame" inside your MyWidget class, you override the predefined style properties.

A brief example:
If you create a "TFrame" style, all instances of "TFrame" (== Tkinter.Frame / ttk.Frame) will be affected.
It is also sometimes called the "root-style".
If you define another "somename.TFrame" and set it for one object type frame, these will be styles according to "somename.TFrame" .

Question 4:
Search names only override the default styles.
If they do not have properties, they do not redefine the thing.
This β€œassignment” leads to a tcl call and does not have specific error handling inside Tkinter / ttk sources (used in the BaseWidget class).

I can only say that tcl does not cause an error here, but I myself am not a tcl expert.

Hope this helps a little.

0
source

Source: https://habr.com/ru/post/1275034/


All Articles