正如Bryan Oakley所说,ttk按钮没有背景选项,更改其背景的唯一方法是使用样式:
import tkinter as tk
from tkinter import ttk
def updateButton(value):
for btn in btnList:
btnList[btn].configure(style='white.TButton')
btnList[value].configure(style='green.TButton')
root = tk.Tk()
style = ttk.Style(root)
style.configure('white.TButton', background='white')
style.configure('green.TButton', background='green')
btn1 = ttk.Button(root, text="BTN 1", command=lambda: updateButton("BTN 1"),
style='white.TButton')
btn1.pack()
btn2 = ttk.Button(root, text="BTN 2", command=lambda: updateButton("BTN 2"),
style='white.TButton')
btn2.pack()
btnList = { "BTN 1" : btn1, "BTN 2" : btn2 }
root.mainloop()