我在一个窗口中放置了 9 个按钮。每个按钮上都有一张图片,我希望点击按钮时图片会发生变化。
我看到的所有方法都太复杂了,我无法理解(lambda、self 等),所以我想知道是否有一种简单的方法。
到目前为止,这是我的三个按钮:
tl=Button(root)
tm=Button(root)
tr=Button(root)
photo1=PhotoImage(file="/Users/apple/Desktop/images Tkinter/arrowhead.gif")
photo2=PhotoImage(file="/Users/apple/Desktop/images Tkinter/arrowhead_plus.gif")
tl.config(image=photo1,width="100",height="100",command=function)
tl.grid(row=1, column=1)
tm.config(image=photo1,width="100",height="100")
tm.grid(row=1, column=2)
tr.config(image=photo1,width="100",height="100")
tr.grid(row=1, column=3)
“功能”可能是什么?我不希望它是:
def function():
tl.config(image=photo2,width="100",height="100",command=function)
tl.grid(row=1, column=1)
因为这个函数只适用于这个按钮,我不想为每个按钮都写一个函数。
希望我已经说得够清楚了。提前致谢。
请您参考如下方法:
你可以使用 lambda 关键字来做到这一点:
tl.config(image=photo2,width="100",height="100",command=lambda: function(1))
等等..
检查Tkinter Callbacks并搜索 lambda。
