Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否可以更改tkinter应用程序';基于windows10';s当前的亮/暗主题配置?_Python_Tkinter_Windows 10 - Fatal编程技术网

Python 是否可以更改tkinter应用程序';基于windows10';s当前的亮/暗主题配置?

Python 是否可以更改tkinter应用程序';基于windows10';s当前的亮/暗主题配置?,python,tkinter,windows-10,Python,Tkinter,Windows 10,当用户将Windows 10颜色应用程序模式从亮变暗,反之亦然时,如何使tkinter应用程序能够自动将其颜色方案更改为深色?您可以使用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_ch

当用户将Windows 10颜色应用程序模式从亮变暗,反之亦然时,如何使tkinter应用程序能够自动将其颜色方案更改为深色?

您可以使用
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")
    ...

您可以使用
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")
    ...

不回答问题…你能澄清一下你认为缺少的部分吗?在第一句话中:“…制作一个tkinter应用程序,能够自动改变它的配色方案…”。在我看来,仅仅改变一个
标签
并不是一回事。哦,好的。我将他的问题解释为如何获得windows主题模式,而不是tkinter,因为已经有关于主题更改的答案。对于如何确定当前主题或检测其已更改,您的答案可能仍然有用。在tkinter中,我所知道的唯一“主题化”是。不回答问题…您能澄清您认为缺少的部分吗?在第一句话中:“…制作一个tkinter应用程序,能够自动更改其配色方案…”。在我看来,仅仅改变一个
标签
并不是一回事。哦,好的。我将他的问题解释为如何获得windows主题模式,而不是tkinter,因为已经有关于主题更改的答案。对于如何确定当前主题或检测其已更改,您的答案可能仍然有用。在tkinter中我所知道的唯一的“主题化”是为。