我正在try 编写一个能够将主题从黑暗更改为Illuminate 的应用程序.我用的是rdbende的forest主题.当通过root.tk.call("source", "forest-dark.tcl")仅加载一个主题时,一切都被正确地显示,但是如果在root.tk.call("source", "forest-light.tcl")之后加载了另一个第二主题,则窗口背景和组合框填充将被重新绘制为其 colored颜色 ,即使它不适用.(仅适用于style.theme_use("forest-dark")).

only dark theme add light theme

我试图通过root.option_add('*TCombobox*Listbox*Background', bg_color)对事件函数中的组合框重新着色,但它只在第一次起作用,然后就被忽略了.

以下是我的代码:

from tkinter import *
from tkinter import ttk


root = Tk()
root.geometry('550x500')

# Create a style
style = ttk.Style(root)

# Import the tcl file
root.tk.call("source", "forest-dark.tcl")
# Import the tcl file
root.tk.call("source", "forest-light.tcl")

dark_mode = True
theme_dark = "forest-dark"
theme_light = "forest-light"

# Set the theme with the theme_use method
style.theme_use(theme_dark)

# create a Frame widget with the custom style
frame = ttk.Frame(root, width=200, height=200)
frame.pack(expand=1, fill=BOTH)


def change_mode():
    global dark_mode 
    dark_mode = not dark_mode
    style.theme_use(theme_dark if dark_mode else theme_light)
    
    _style = ttk.Style()
    bg_color = _style.lookup('TFrame', 'background')
    fg_color = _style.lookup('TLabel', 'foreground')
    
    print(f"{bg_color}-{fg_color}")
    
    root.option_add('*TCombobox*Listbox*Background', bg_color)
    root.option_add('*TCombobox*Listbox*Foreground', fg_color)
    root.option_add('*TCombobox*Listbox*selectBackground', fg_color)
    root.option_add('*TCombobox*Listbox*selectForeground', bg_color)


button = ttk.Button(frame, text='mode', command=change_mode)
button.pack()

combo = ttk.Combobox(frame, width=40, justify=CENTER, values=[
'value1', 'value1', 'value1', 'value1', 'value1', 'value1', 'value1', 'value1'])
combo.pack()

root.mainloop()

推荐答案

找到了一种通过TCL解释器切换主题的方法: here

最后代码:

from tkinter import *
from tkinter import ttk


root = Tk()
root.geometry('550x500')

# Create a style
style = ttk.Style(root)

# Import the tcl file
root.tk.call("source", "forest-dark.tcl")
# Import the tcl file
root.tk.call("source", "forest-light.tcl")

dark_mode = True
theme_dark = "forest-dark"
theme_light = "forest-light"

# Set the theme with the theme_use method
style.theme_use(theme_dark)

# create a Frame widget with the custom style
frame = ttk.Frame(root, width=200, height=200)
frame.pack(expand=1, fill=BOTH)


class MyCombo(ttk.Combobox):
    def __init__(self, master, **kwargs):
        ttk.Combobox.__init__(self, master, **kwargs)
        self._master = master
        self._change_color()
        self._master.bind("<<ThemeChanged>>", self._change_color)
    
    def _change_color(self, *args):
        _style = ttk.Style()
        # Get current Frame bg and Label fg
        bg_color = _style.lookup('TFrame', 'background')
        fg_color = _style.lookup('TLabel', 'foreground')
        
        # Create a new background for the combobox a little lighter if it's a dark theme, a little darker if it's light
        is_dark_mode = True if int(str(bg_color).replace("#", ''), 16) < int(str(fg_color).replace("#", ''), 16) else False
        new_bg_color = "#404040" if is_dark_mode else "#F8F8F8"
        
        # Set colors
        self._master.tk.eval('[ttk::combobox::PopdownWindow {}].f.l configure -background {} -foreground {}'.format(self, new_bg_color, fg_color))

def change_mode():
    global dark_mode 
    dark_mode = not dark_mode
    style.theme_use(theme_dark if dark_mode else theme_light)

button = ttk.Button(frame, text='Change mode', command=change_mode)
button.pack()

combo = MyCombo(frame, width=40, justify=CENTER, values=[
'value1', 'value1', 'value1', 'value1', 'value1', 'value1', 'value1', 'value1'])
combo.pack()

root.mainloop()

Python相关问答推荐

对于一个给定的数字,找出一个整数的最小和最大可能的和

Python,Fitting into a System of Equations

Pre—Commit MyPy无法禁用非错误消息

迭代嵌套字典的值

如何根据一列的值有条件地 Select 前N组?

提取相关行的最快方法—pandas

在matplotlib中删除子图之间的间隙_mosaic

为什么\b在这个正则表达式中不解释为反斜杠

如何使regex代码只适用于空的目标单元格

处理具有多个独立头的CSV文件

Pandas—堆栈多索引头,但不包括第一列

BeautifulSoup-Screper有时运行得很好,很健壮--但有时它失败了::可能这里需要一些更多的异常处理?

如何获得3D点的平移和旋转,给定的点已经旋转?

简单 torch 模型测试:ModuleNotFoundError:没有名为';Ultralytics.yolo';

Python OPCUA,modbus通信代码运行3小时后出现RuntimeError

高效生成累积式三角矩阵

Regex用于匹配Python中逗号分隔的AWS区域

有了Gekko,可以创建子模型或将模型合并在一起吗?

用0填充没有覆盖范围的垃圾箱

时间戳上的SOAP头签名无效