当用户将Windows 10颜色应用程序模式从光明更改为黑暗时,我如何使一个更简单的应用程序能够自动将其颜色方案更改为深色的?
发布于 2019-09-27 09:13:12
您可以使用root.after检查注册表中的更改。
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()为了完整起见,下面是如何配置ttk.Style对象以更改主题:
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")
...https://stackoverflow.com/questions/58130332
复制相似问题