Reputation: 13
How could I make a tkinter application capable of automatically changing its colour scheme to a dark one when the user changes Windows 10 colour app mode from Light to Dark and viceversa?
Upvotes: 1
Views: 3245
Reputation: 22503
You can use root.after
to check for changes in registry.
from winreg import *
import tkinter as tk
root = tk.Tk()
root.config(background="white")
label = tk.Label(root,text="Light mode on")
label.pack()
def monitor_changes():
registry = ConnectRegistry(None, HKEY_CURRENT_USER)
key = OpenKey(registry, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize')
mode = QueryValueEx(key, "AppsUseLightTheme")
root.config(bg="white" if mode[0] else "black")
label.config(text="Light Mode on" if mode[0] else "Dark Mode on",
bg="white" if mode[0] else "black",
fg="black" if mode[0] else "white")
root.after(100,monitor_changes)
monitor_changes()
root.mainloop()
For completeness, here is how you configure a ttk.Style
object to change the theme:
root = tk.Tk()
style = ttk.Style()
style.configure("BW.TLabel",foreground="black",background="white")
label = ttk.Label(root,text="Something",style="BW.TLabel")
label.pack()
def monitor_changes():
...
style.configure("BW.TLabel",foreground="black" if mode[0] else "white", background="white" if mode[0] else "black")
...
Upvotes: 2