Home | History | Annotate | Download | only in ttk
      1 """Ttk Theme Selector.
      2 
      3 Although it is a theme selector, you won't notice many changes since
      4 there is only a combobox and a frame around.
      5 """
      6 import ttk
      7 
      8 class App(ttk.Frame):
      9     def __init__(self):
     10         ttk.Frame.__init__(self)
     11 
     12         self.style = ttk.Style()
     13         self._setup_widgets()
     14 
     15     def _change_theme(self, event):
     16         if event.widget.current(): # value #0 is not a theme
     17             newtheme = event.widget.get()
     18             # change to the new theme and refresh all the widgets
     19             self.style.theme_use(newtheme)
     20 
     21     def _setup_widgets(self):
     22         themes = list(self.style.theme_names())
     23         themes.insert(0, "Pick a theme")
     24         # Create a readonly Combobox which will display 4 values at max,
     25         # which will cause it to create a scrollbar if there are more
     26         # than 4 values in total.
     27         themes_combo = ttk.Combobox(self, values=themes, state="readonly",
     28                                     height=4)
     29         themes_combo.set(themes[0]) # sets the combobox value to "Pick a theme"
     30         # Combobox widget generates a <<ComboboxSelected>> virtual event
     31         # when the user selects an element. This event is generated after
     32         # the listbox is unposted (after you select an item, the combobox's
     33         # listbox disappears, then it is said that listbox is now unposted).
     34         themes_combo.bind("<<ComboboxSelected>>", self._change_theme)
     35         themes_combo.pack(fill='x')
     36 
     37         self.pack(fill='both', expand=1)
     38 
     39 
     40 def main():
     41     app = App()
     42     app.master.title("Ttk Combobox")
     43     app.mainloop()
     44 
     45 if __name__ == "__main__":
     46     main()
     47