是否可以根据Windows10当前的亮/暗主题配置更改tkinter应用程序的主题?

2024-05-19 10:54:34 发布

您现在位置:Python中文网/ 问答频道 /正文

当用户将Windows10的颜色应用程序模式从浅色改为深色时,tkinter应用程序如何能够自动将其配色方案更改为深色?在


Tags: 用户应用程序颜色tkinter模式方案配色深色
1条回答
网友
1楼 · 发布于 2024-05-19 10:54:34

您可以使用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对象以更改主题:

^{pr2}$

相关问题 更多 >

    热门问题