简体   繁体   English

tkinter 按钮文字颜色不变

[英]tkinter button text color is not changing

The button text color is blue on creation, once it is clicked the button text color should change to red color as per the code.按钮文本颜色在创建时为蓝色,一旦单击,按钮文本颜色应根据代码变为红色。 But on clicking the color is not changing但是点击颜色并没有改变

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()

You need to remove state=DISABLED from your on_click function to make this work您需要从on_click function 中删除state=DISABLED才能使这项工作

def on_click(button_id):
      btn[button_id].configure(text='clicked', fg ="red")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM