Tkinter Themes

Salman Mehmood Mar 16, 2022
Tkinter Themes

The current Tk widget theme feature is one of the new widget set’s most powerful and exciting features. However, it is also one of the most confusing things.

This tutorial demonstrates the theme’s style, which controls what the widget should look like in the Tkinter GUI. In other words, we can say what the button appearance and other widgets should look like.

Themes in Tkinter GUI

This is a set of styles that define every widget in your GUI looks like. The themes modification enhances the GUI appearance, which gives a different look than the normal Tkinter GUI.

However, before it is taken down, very few apps will benefit from changing themes like sports, gaming or educational programs that may be different.

The theme may be different for different platforms, especially macOS and Windows. Tkinter lets you change the current theme to another.

If you change the current theme to a new one, Tkinter will apply the style to all ttk widgets. We need to create an instance of the ttk.Style class to get themes.

style = ttk.Style(window)

Style helps the user to avoid redundant and repetitive code.

If you have 20 button widgets in your app, create an object of style, then you do not need to repeat appearance details whenever you create a widget. This way, the widget looks stylish.

Since the style gathers all information about appearance in one place, other widgets’ button styles and styles can share common features, improving consistency and reuse.

We need to get available themes using the theme_use() method and print the available themes.

print(style.theme_names())

Output:

('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')

If we need to get a current theme, we can use it using the theme_use() method.

style.theme_use()

We can select other themes using the selected_theme class to use the different themes; the get() method will help get that theme.

style.theme_use(window.selected_theme.get())

We have generated multiple Radiobutton in this loop that stores theme names and gets individual values every iteration.

for theme_name in style.theme_names():
    # Create a bulk of radio buttons using loop
    radio_buttons = ttk.Radiobutton(
        theme_frame,
        text=theme_name,
        value=theme_name,
        variable=window.selected_theme,
        command=theme_changer,
    )
    radio_buttons.pack(expand=True, fill="both")

The command option held the theme_changer function to call when the user selects a radio button.

This function will change the theme using the StringVar() class that e passed in the Radiobutton widget option.

def theme_changer():
    # Change  theme
    style.theme_use(window.selected_theme.get())

Full source code:

from tkinter import *

from tkinter import ttk

# Create GUI window
window = Tk()

window.title("Delftstack")
window.geometry("500x400")
# create style object
style = ttk.Style(window)


def theme_changer():
    # Change  theme
    style.theme_use(window.selected_theme.get())


label = ttk.Label(window, text="Name:")
label.grid(column=0, row=0, padx=10, pady=10, sticky="w")

entry = ttk.Entry(window)
entry.grid(column=1, row=0, padx=10, pady=10, sticky="w")

button = ttk.Button(window, text="press")
button.grid(column=2, row=0, padx=10, pady=10, sticky="w")

window.selected_theme = StringVar()
theme_frame = ttk.LabelFrame(window, text="Themes")
theme_frame.grid(padx=10, pady=10, ipadx=20, ipady=20, sticky="w")

for theme_name in style.theme_names():
    # Create a bulk of radio buttons using loop
    radio_buttons = ttk.Radiobutton(
        theme_frame,
        text=theme_name,
        value=theme_name,
        variable=window.selected_theme,
        command=theme_changer,
    )
    radio_buttons.pack(expand=True, fill="both")

window.mainloop()

print(style.theme_names())

Output:

Select a Theme in Tkinter GUI

Click here to read more about Tkinter themes.

Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn