1"""Ttk Theme Selector v2. 2 3This is an improvement from the other theme selector (themes_combo.py) 4since now you can notice theme changes in Ttk Combobox, Ttk Frame, 5Ttk Label and Ttk Button. 6""" 7import Tkinter 8import ttk 9 10class App(ttk.Frame): 11 def __init__(self): 12 ttk.Frame.__init__(self, borderwidth=3) 13 14 self.style = ttk.Style() 15 16 # XXX Ideally I wouldn't want to create a Tkinter.IntVar to make 17 # it works with Checkbutton variable option. 18 self.theme_autochange = Tkinter.IntVar(self, 0) 19 self._setup_widgets() 20 21 def _change_theme(self): 22 self.style.theme_use(self.themes_combo.get()) 23 24 def _theme_sel_changed(self, widget): 25 if self.theme_autochange.get(): 26 self._change_theme() 27 28 def _setup_widgets(self): 29 themes_lbl = ttk.Label(self, text="Themes") 30 31 themes = self.style.theme_names() 32 self.themes_combo = ttk.Combobox(self, values=themes, state="readonly") 33 self.themes_combo.set(themes[0]) 34 self.themes_combo.bind("<<ComboboxSelected>>", self._theme_sel_changed) 35 36 change_btn = ttk.Button(self, text='Change Theme', 37 command=self._change_theme) 38 39 theme_change_checkbtn = ttk.Checkbutton(self, 40 text="Change themes when combobox item is activated", 41 variable=self.theme_autochange) 42 43 themes_lbl.grid(ipadx=6, sticky="w") 44 self.themes_combo.grid(row=0, column=1, padx=6, sticky="ew") 45 change_btn.grid(row=0, column=2, padx=6, sticky="e") 46 theme_change_checkbtn.grid(row=1, columnspan=3, sticky="w", pady=6) 47 48 top = self.winfo_toplevel() 49 top.rowconfigure(0, weight=1) 50 top.columnconfigure(0, weight=1) 51 self.columnconfigure(1, weight=1) 52 self.grid(row=0, column=0, sticky="nsew", columnspan=3, rowspan=2) 53 54 55def main(): 56 app = App() 57 app.master.title("Theme Selector") 58 app.mainloop() 59 60if __name__ == "__main__": 61 main() 62