Add Dark Mode to Tkinter GUI – Python Theme Toggle Example

 

← Back to Home

🌙 Bonus Part 1: Adding Dark Mode to Your Tkinter App


📘 Overview

Dark Mode is more than a visual trend — it's easier on the eyes, especially in low-light environments. In this bonus part, you'll learn how to:

  • Create a toggle for light/dark themes

  • Dynamically change widget colors

  • Implement a reusable dark mode for your Tkinter apps


🎨 Why Add Dark Mode?

✅ Benefits:

  • Reduces eye strain

  • Preferred by many users

  • Makes your app look modern and polished


🧱 Approach

Tkinter doesn’t have built-in themes like “dark” or “light,” so we define two color palettes and apply them dynamically to your widgets.


✅ Step-by-Step: Dark Mode Toggle Example

import tkinter as tk

# Color themes
LIGHT_THEME = {
    "bg": "#ffffff",
    "fg": "#000000",
    "button_bg": "#f0f0f0",
    "entry_bg": "#ffffff"
}

DARK_THEME = {
    "bg": "#2e2e2e",
    "fg": "#ffffff",
    "button_bg": "#3e3e3e",
    "entry_bg": "#4e4e4e"
}

class DarkModeApp(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Dark Mode Toggle")
        self.geometry("300x200")
        self.theme = LIGHT_THEME

        self.label = tk.Label(self, text="Hello, Tkinter!", font=("Arial", 14))
        self.label.pack(pady=10)

        self.entry = tk.Entry(self)
        self.entry.pack(pady=5)

        self.toggle_btn = tk.Button(self, text="Toggle Dark Mode", command=self.toggle_theme)
        self.toggle_btn.pack(pady=10)

        self.apply_theme()

    def apply_theme(self):
        self.configure(bg=self.theme["bg"])
        self.label.configure(bg=self.theme["bg"], fg=self.theme["fg"])
        self.entry.configure(bg=self.theme["entry_bg"], fg=self.theme["fg"], insertbackground=self.theme["fg"])
        self.toggle_btn.configure(bg=self.theme["button_bg"], fg=self.theme["fg"])

    def toggle_theme(self):
        self.theme = DARK_THEME if self.theme == LIGHT_THEME else LIGHT_THEME
        self.apply_theme()

if __name__ == "__main__":
    app = DarkModeApp()
    app.mainloop()

🧠 Key Concepts

  • You store the theme values in dictionaries

  • When the user clicks “Toggle Dark Mode”, the theme switches and apply_theme() re-colors everything

  • You can expand this to theme more widgets (buttons, frames, checkbuttons, etc.)


🔌 Bonus: Remember Theme Preference

To store the user’s theme preference:

  • Use a simple settings.txt file

  • Save dark or light

  • Load it when the app starts



💡 Recap

In this part, you’ve learned:

  • How to define and switch between light/dark themes

  • How to dynamically apply colors to widgets

  • How to build a theme-aware GUI with clean logic



 next : remember theme preference 


No comments:

Post a Comment

Featured Post

Add Keyboard Shortcuts to Tkinter GUI – Python Hotkeys with Bind Examples

  ← Back to Home ⌨️ Bonus Part 5: Keyboard Shortcuts & Hotkeys in Tkinter – Boost App Productivity 📘 Overview Keyboard shortcuts ...

Popular Posts