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