How to switch between Themes TTK

Tkinter is a popular GUI library in Python used to create GUI applications. TTK is an extension of Tkinter which provides additional widgets and other cool features such as Styling and Themes. In this tutorial, we will discuss how to switch between various themes in our Tkinter ttk application.


TTK Themes

First lets take a look at what pre-built themes are offered to us by TTK. We can do so by using the theme_names() method on a Style object.

import tkinter as tk
import tkinter.ttk as ttk

class Window:
    def __init__(self, master):
        style = ttk.Style()
        print(style.theme_names())

root = tk.Tk()
window = Window(root)
root.mainloop()
('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')

The number and types of available themes are different based on your Operating system. The above themes are available on Windows.

Now lets explore how we can switch between these frames. The simplest of “using” a theme is to use the theme_use() method, which takes a string as a parameter.

style.theme_use("clam")

For this tutorial, we will develop a GUI application that presents to the user a list of all available themes, and allows his to swap between them dynamically.

Here is the code, where we place all the available themes as options inside a listbox, and then allow the user to select them.

import tkinter as tk
from tkinter import ttk

window = tk.Tk()
style = ttk.Style()


def update_theme(event):
    # Get the selected theme from the listbox and select it
    theme = themes_listbox.get(themes_listbox.curselection())
    style.theme_use(theme)


button = ttk.Button(window, text = "Hello World")
button.pack(padx = 30, pady = 20)

# Create a tk.Listbox widget to display the available themes
themes_listbox = tk.Listbox(window)
themes_listbox.pack(padx = 30, pady = 20)

# Add the available themes to the listbox
for index, theme in enumerate(style.theme_names()):
    themes_listbox.insert(index, theme)


# Bind the 'update_theme' function to the '<<ListboxSelect>>' event
themes_listbox.bind("<<ListboxSelect>>", update_theme)
window.mainloop()

Some sample outputs:

How to switch between Themes TTK
Vista theme in TTK
Alt theme in ttk

Things to Remember

Remember that the themes will only work on the ttk version of the Tkinter widgets. Every tkinter widget has a ttk version which supports its new styling and theme options. Instead of doing:

button = tk.Button(window, text = "Hello World")

do:

button = ttk.Button(window, text = "Hello World")

Or if you import tkinter like this:

from tkinter import *

do this instead:

from tkinter.ttk import *

Tip: You can even create your own custom themes in Tkinter if you want.


This marks the end of the How to switch between Themes TTK Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions about the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments