按钮文字颜色在创建时为蓝色,一旦单击,按钮文字颜色应根据代码更改为红色。但是点击后颜色不会改变
from tkinter import Button,Tk,Frame,DISABLED,Label
def on_click(button_id):
btn[button_id].configure(text='clicked',state=DISABLED,fg ="red")
window = Tk()
frame = Frame(window)
frame2 = Frame(window)
frame2.pack()
frame.pack()
btn=[]
lbl=Label(frame2,text="Hello there")
lbl.pack()
for i in range(0,5):
b=Button(frame,text="hello!!", fg="blue",font='sans 8 bold',command = lambda
button_id=i: on_click(button_id),width = 4, relief = "groove")
b.grid(row=i,column=0)
btn.append(b)
window.mainloop()
答案 0 :(得分:0)
您需要从 state=DISABLED
功能中删除 on_click
才能实现此功能
def on_click(button_id):
btn[button_id].configure(text='clicked', fg ="red")