[英]How can I configure multiple buttons with one function in Tkinter?
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()
以便它更改按钮的颜色而不用一行配置每个按钮来显式更改颜色?
我假设“所以它改变了按钮的颜色”,你想只有实际点击来改变颜色的按钮。
我知道两种方法。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.