Tkinter ttk
Themed Widgets
The ttk module has newer versions of many Tkinter widgets. They look more like the other apps on the computer:
Example
A classic button and a ttk button:
import tkinter as tk
from tkinter import ttk
window = tk.Tk()
window.geometry("300x200")
button1 = tk.Button(window, text="Classic")
button1.pack(pady=10)
button2 = ttk.Button(window, text="Themed")
button2.pack()
window.mainloop()
Result:
You use them the same way as the classic widgets. Just write ttk.Button instead of tk.Button.
ttk has its own Button, Label, Entry, Checkbutton, Radiobutton and Frame, and some widgets that only exist in ttk, like Combobox and Progressbar.
Note: Some options work differently in ttk. For example, ttk.Button has no bg option for the background color.
Combobox
A Combobox is a drop-down list, where the user picks one of the values:
Example
A drop-down list of sizes:
import tkinter as tk
from tkinter import ttk
window = tk.Tk()
window.geometry("300x200")
sizes = ["Small", "Medium", "Large"]
combo = ttk.Combobox(window, values=sizes)
combo.pack(pady=10)
combo.set("Medium")
window.mainloop()
Result:
After clicking the arrow:
values is the list of choices, and set() chooses the value that is shown from the start.
Get the Selected Value
When the user picks a value, the combobox sends the event "<<ComboboxSelected>>". Use get() to read the value:
Example
Show the selected size:
import tkinter as tk
from tkinter import ttk
window = tk.Tk()
window.geometry("300x200")
sizes = ["Small", "Medium", "Large"]
def show(event):
label.config(text=combo.get())
combo = ttk.Combobox(window, values=sizes,
state="readonly")
combo.pack(pady=10)
combo.bind("<<ComboboxSelected>>", show)
label = ttk.Label(window)
label.pack()
window.mainloop()
Result:
After selecting Large:
state="readonly" means that the user can only pick from the list, and not type their own text.
The event name has double angle brackets, because it comes from the widget itself, and not from a key or the mouse.
You will see one more ttk widget, the Progressbar, in the Tkinter Timers chapter.