Showing posts with label python gui dark mode memory. Show all posts
Showing posts with label python gui dark mode memory. Show all posts

Save and Load Theme Preference in Tkinter – Python GUI Dark Mode Persistence

← Back to Home

 How to extend the Dark Mode feature by saving and loading the user’s theme preference using a simple settings.txt file.


💾 Remember Theme Preference in Tkinter (Dark/Light)

We’ll modify the previous DarkModeApp to:

  1. Save the selected theme (light or dark) in settings.txt

  2. Read the preference when the app starts

  3. Automatically apply the saved theme


✅ Step-by-Step Implementation

import tkinter as tk
import os

# Theme dictionaries
LIGHT_THEME = {
    "bg": "#ffffff",
    "fg": "#000000",
    "button_bg": "#f0f0f0",
    "entry_bg": "#ffffff"
}

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

SETTINGS_FILE = "settings.txt"

class DarkModeApp(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Dark Mode with Saved Preference")
        self.geometry("300x200")

        self.theme = self.load_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.save_theme("dark" if self.theme == DARK_THEME else "light")
        self.apply_theme()

    def save_theme(self, theme_name):
        with open(SETTINGS_FILE, "w") as f:
            f.write(theme_name)

    def load_theme(self):
        if os.path.exists(SETTINGS_FILE):
            with open(SETTINGS_FILE, "r") as f:
                saved = f.read().strip().lower()
                return DARK_THEME if saved == "dark" else LIGHT_THEME
        return LIGHT_THEME  # default

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

📂 What This Does

  • Reads settings.txt on startup to get the last theme used

  • If no file is found, it defaults to Light Mode

  • When the user toggles the theme, it updates the file


🔐 Tip

To make this platform-independent, you could store the file in a user-safe location like:

from pathlib import Path
SETTINGS_FILE = Path.home() / ".tkinter_theme_settings"

Now that your app remembers the user's theme preference, it's a much more polished and user-friendly product.


✅ You're all set to continue with Bonus Part 2: Animations & Transitions in Tkinter


Featured Post

Number Guessing Game (code) in python

← Back to Projects About the project: This is a simple number guessing game and it is suitable for beginners who are learning python progra...

Popular Posts