def changeColour():
LT.configure(bg = "white")
LM.configure(bg = "white")
LB.configure(bg = "white")
LT = Button(root, width=16, height=8, bg = "blue", command = changeColour)
LT.place(x=10, y=10)
LM = Button(root, width=16, height=8, bg = "red", command = changeColour)
LM.place(x=10, y=150)
LB = Button(root, width=16, height=8, bg = "green", command = changeColour)
LB.place(x=10, y=290)
如何编写函数changeColour()
,以便更改按钮的颜色而不用线条配置每个按钮以明确更改颜色?
答案 0 :(得分:1)
我假设“所以它改变了 按钮的颜色”,你只想要实际点击的按钮改变颜色。
我知道两种方法。
def changeColour(widget):
widget.config(bg="white")
#...
LM = Button(root, width=16, height=8, bg = "red", command = lambda: changeColour(LM))
LM.place(x=10, y=150)
bind
代替command
,因为前者可以推断出引发事件的小部件。
def changeColour(event):
event.widget.config(bg="white")
#...
LT = Button(root, width=16, height=8, bg = "blue")
LT.bind("<1>", changeColour)
LT.place(x=10, y=10)