-1

I'm trying to make my app written in python with a Tkinter GUI to change dependant on whether the device is in light or dark mode. I can't seem to change the colour of each label in tk.

Here is my code so far:

def monitor_changes():
    registry = ConnectRegistry(None, HKEY_CURRENT_USER)
    key = OpenKey(registry, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize')
    mode = QueryValueEx(key, "AppsUseLightTheme")
    tk.config(bg="#f0f0f0" if mode[0] else "black")
    tk.after(100,monitor_changes)
    for Label in tk:
        if mode[0]:
            Label.config(bg="#f0f0f0")
        else:
            Label.config(bg="black")

monitor_changes()
petezurich
  • 9,280
  • 9
  • 43
  • 57
Sunari
  • 13
  • 1
  • 5
  • Does this answer your question? [Is it possible to have a standard style for a widget?](https://stackoverflow.com/questions/52210391/is-it-possible-to-have-a-standard-style-for-a-widget) – stovfl Apr 15 '20 at 15:14

2 Answers2

0

What I understand that tk is instance of Tk(). Then you can use tk.winfo_children() to get all the children of tk and use isinstance() to check whether a child is a Label:

def monitor_changes():
    registry = ConnectRegistry(None, HKEY_CURRENT_USER)
    key = OpenKey(registry, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize')
    mode = QueryValueEx(key, "AppsUseLightTheme")
    tk.config(bg="#f0f0f0" if mode[0] else "black")
    # go through all the children of tk
    for widget in tk.winfo_children():
        # check whether widget is instance of Label
        if isinstance(widget, Label):
            widget.config(bg="#f0f0f0" if mode[0] else "black")
    tk.after(100,monitor_changes)
acw1668
  • 40,144
  • 5
  • 22
  • 34
0

After making a slight alteration to acw1668's code, I was able to get it to work. Here is the working code:

def monitor_changes():
    registry = ConnectRegistry(None, HKEY_CURRENT_USER)
    key = OpenKey(registry, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize')
    mode = QueryValueEx(key, "AppsUseLightTheme")
    tk.config(bg="#f0f0f0" if mode[0] else "black")
    # go through all the children of tk
    for widget in tk.winfo_children():
        # check whether widget is instance of Label
        if isinstance(widget, Label):
            widget.config(background="#f0f0f0" if mode[0] else "black")
            widget.config(foreground="white")
    tk.after(100,monitor_changes)

monitor_changes()
Sunari
  • 13
  • 1
  • 5