Light or Dark Theme Changer using Tkinter

A GUI (Graphical User Interface) that smoothly alternates between light and dark themes can greatly enhance user experience. Tkinter, Python's built-in GUI library, makes it easy to create such applications. This tutorial will show you how to build a theme changer using Tkinter.

What is Tkinter?

Tkinter is Python's standard GUI library included with every Python installation. It provides a simple yet powerful way to create desktop applications with widgets like buttons, labels, text boxes, and menus that can be customized with different colors and styles.

Importance of Theme Changing Feature

Theme switching improves user experience by allowing customization based on personal preferences and lighting conditions. Dark themes reduce eye strain in low-light environments, while light themes work better in bright conditions. This feature has become essential in modern applications.

Basic Theme Changer

Let's start with a simple theme switcher that toggles between light and dark backgrounds ?

from tkinter import Tk, Button

def change_theme():
    current_bg = root.cget("bg")
    new_bg = "white" if current_bg == "black" else "black"
    root.configure(bg=new_bg)

root = Tk()
root.geometry("300x300")
root.title("Theme Changer")
root.configure(bg="white")  # Start with light theme

change_theme_button = Button(root, text="Change Theme", command=change_theme)
change_theme_button.pack(pady=20)

root.mainloop()

This creates a window with a button that toggles the background color between white and black when clicked.

Advanced Theme Changer

For a more sophisticated theme switcher, we can create complete color schemes that affect multiple widgets ?

from tkinter import Tk, Button, Label, Frame

class ThemeChanger:
    def __init__(self):
        self.themes = {
            "light": {
                "bg": "#ffffff",
                "fg": "#000000", 
                "button_bg": "#e1e1e1",
                "button_fg": "#000000"
            },
            "dark": {
                "bg": "#2b2b2b",
                "fg": "#ffffff",
                "button_bg": "#404040", 
                "button_fg": "#ffffff"
            }
        }
        self.current_theme = "light"
        self.setup_ui()
    
    def setup_ui(self):
        self.root = Tk()
        self.root.geometry("400x300")
        self.root.title("Advanced Theme Changer")
        
        # Main frame
        self.main_frame = Frame(self.root)
        self.main_frame.pack(fill="both", expand=True, padx=20, pady=20)
        
        # Title label
        self.title_label = Label(
            self.main_frame, 
            text="Theme Changer Demo",
            font=("Arial", 16, "bold")
        )
        self.title_label.pack(pady=10)
        
        # Info label
        self.info_label = Label(
            self.main_frame, 
            text="Click the button below to toggle between light and dark themes"
        )
        self.info_label.pack(pady=10)
        
        # Theme button
        self.theme_button = Button(
            self.main_frame,
            text="Switch to Dark Theme",
            command=self.toggle_theme,
            font=("Arial", 12),
            padx=20,
            pady=10
        )
        self.theme_button.pack(pady=20)
        
        # Apply initial theme
        self.apply_theme()
    
    def toggle_theme(self):
        self.current_theme = "dark" if self.current_theme == "light" else "light"
        self.apply_theme()
    
    def apply_theme(self):
        theme = self.themes[self.current_theme]
        
        # Apply to root window
        self.root.configure(bg=theme["bg"])
        
        # Apply to main frame
        self.main_frame.configure(bg=theme["bg"])
        
        # Apply to labels
        self.title_label.configure(bg=theme["bg"], fg=theme["fg"])
        self.info_label.configure(bg=theme["bg"], fg=theme["fg"])
        
        # Apply to button
        self.theme_button.configure(
            bg=theme["button_bg"], 
            fg=theme["button_fg"],
            activebackground=theme["button_bg"],
            activeforeground=theme["button_fg"]
        )
        
        # Update button text
        next_theme = "Dark" if self.current_theme == "light" else "Light"
        self.theme_button.configure(text=f"Switch to {next_theme} Theme")
    
    def run(self):
        self.root.mainloop()

# Create and run the application
app = ThemeChanger()
app.run()

This advanced version uses a class-based approach and applies consistent styling to all widgets. The themes include background colors, text colors, and button styling for a cohesive appearance.

Key Features

  • Complete Theme Support: Colors are applied to all widgets consistently
  • Professional Styling: Uses proper color schemes with good contrast
  • Dynamic Button Text: The button text updates to show which theme it will switch to
  • Organized Code: Class-based structure makes it easy to extend and maintain

Conclusion

Theme switching is an essential feature for modern applications. Using Tkinter's configuration methods, you can create both simple and sophisticated theme changers that enhance user experience by providing visual customization options.

---
Updated on: 2026-03-27T08:16:59+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements