python 切换tk.call()和ttk.Style().theme_use()的值

np8igboo  于 2023-03-28  发布在  Python
关注(0)|答案(1)|浏览(99)

我有下面的功能,应该切换我的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
任何帮助都将不胜感激。谢谢!

aamkag61

aamkag611#

试试这个
请参阅,self.tk.call(...)如何在初始化期间为每个主题调用一次,以及self.style.theme_use(...)如何用于在主题之间切换

import tkinter as tk
from tkinter import ttk

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title("TRANSMITTER")
        # self.geometry("1024x600")

        self.style = ttk.Style(self)

        # import the tcl files
        self.tk.call("source", "forest-light.tcl")
        self.tk.call("source", "forest-dark.tcl")

        # set the theme with the theme_use method
        # self.style.theme_use("forest-dark")

        # init window frame
        frame = ttk.Frame(self)

        # set frame to fill x and y axis and expand to fill window
        frame.pack(fill='both', expand=True)

        # init variable to hold the selected theme
        self.selected_theme = tk.StringVar()

        self.selected_theme.set("forest-dark")

        # create a label frame to hold the radio buttons
        theme_frame = ttk.LabelFrame(frame, text='Themes')

        # pack the label frame to the window with padding and sticky to the west side
        theme_frame.grid(padx=10, pady=10, ipadx=20, ipady=20, sticky='w')

        # create a radio button for each theme
        for theme_name in self.style.theme_names():
            rb = ttk.Radiobutton(
                theme_frame,
                text=theme_name,
                value=theme_name,
                variable=self.selected_theme,
                command=self.change_theme)

            rb.pack(expand=True, fill='both')

    def change_theme(self):
        self.style.theme_use(self.selected_theme.get())

if __name__ == "__main__":
    app = App()
    app.mainloop()

相关问题