我正在使用 ttk 创建一个 tkinter GUI,我已经为我的项目下载并导入了 Azure 主题。然后我尝试使用一种样式将其中一个按钮的颜色更改为红色,但尽管没有出现错误,但这并没有奏效。该按钮只是该主题的默认按钮(它确实响应我可以在样式中指定的其他选项,如填充,所以我认为这不是样式本身的问题。
这是我的问题的一个例子:
import tkinter as tk
from tkinter import ttk
def myfunc():
print("Button Clicked")
root = tk.Tk()
root.geometry("200x200")
root.tk.call("source", "C:/Users/samue/OneDrive/Desktop/Website folder/Azure-ttk-theme-main/azure.tcl")
root.tk.call("set_theme", "dark")
buttonstyle = ttk.Style()
buttonstyle.configure("custom.TButton", padding=0, background="red")
mybutton = ttk.Button(root, text="Click Me", command=myfunc, style="custom.TButton")
mybutton.pack()
root.mainloop()
抱歉,这不是完全可重现的,您必须下载 azure 主题才能运行它(并修改路径)。可以在这里找到 Azure:https://github.com/rdbende/Azure-ttk-theme
片段:
from tkinter import ttk
from tkinter import Tk
root = Tk()
style = ttk.Style()
button_1 = ttk.Button(root, text='click me')
style.theme_use('alt')
style.configure('TButton', font=('Helvetica', 14), background='blue', foreground='white')
style.map('TButton', background=[('active', 'green')])
button_1.pack()
root.mainloop()