我有下面的功能,应该切换我的GUI的主题,但我正在处理一个小问题。主题切换CheckButton第一次按下。
但再次尝试时出现以下错误:
_tkinter.TclError: Theme forest-light already exists
以及:
_tkinter.TclError: Theme forest-dark already exists
代码:
import tkinter as tk
from tkinter import ttk
themeTCL = 'forest-light.tcl'
themeFiles = 'forest-light'
window = tk.Tk()
window.title("TRANSMITTER")
window.geometry("1024x600")
window.tk.call('source', themeTCL)
ttk.Style().theme_use(themeFiles)
x_cordinate = int((window.winfo_screenwidth()/2) - (window.winfo_width()/2))
y_cordinate = int((window.winfo_screenheight()/2) - (window.winfo_height()/2))
window.geometry("+{}+{}".format(x_cordinate, y_cordinate))
var = tk.IntVar()
var.set(0)
def themeSwitch():
global themeTCL
global themeFiles
if var.get() == 0:
switch.config(text="Light Mode")
themeTCL = 'forest-light.tcl'
themeFiles = 'forest-light'
window.tk.call('source', themeTCL) # this line
ttk.Style().theme_use(themeFiles) # this line
elif var.get() == 1:
switch.config(text="Dark Mode")
themeTCL = 'forest-dark.tcl'
themeFiles = 'forest-dark'
window.tk.call('source', themeTCL) # this line
ttk.Style().theme_use(themeFiles) # this line
switch = ttk.Checkbutton(window, text="Light Mode", variable=var, command=themeSwitch, style="Switch")
switch.pack(side="top", fill="both", anchor="e", pady=(5, 5), padx=(5, 5))
window.mainloop()
我不是这方面的Maven,但想知道是否有方法配置值tk.call()和ttk.Style().theme_use(),这样我就可以在黑暗和光明模式之间切换。
主题文件来自:https://github.com/rdbende/Forest-ttk-theme
任何帮助都将不胜感激。谢谢!
1条答案
按热度按时间aamkag611#
试试这个
请参阅,
self.tk.call(...)
如何在初始化期间为每个主题调用一次,以及self.style.theme_use(...)
如何用于在主题之间切换