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?

Picture 1

Figure 2