我试图用不同的参数调用相同的函数,对应于 Tkinter python 中的 for in 和按钮,当我单击其他按钮时,函数给出的值是最后调用的值。我是一名 js 开发人员,曾将 foreach 和 array 与类似的东西一起使用。
apps=["k","c","d"]
for app in apps:
btn = tk.Button(innerFrame, text=" {}".format(app), command=(
lambda: runThis(app)))
btn.pack()
def runThis(val, i):
print("Value of the btn {}".format(val))
单击每个按钮时的预期输出是
Value of the btn k
Value of the btn c
Value of the btn d
但我得到的是
Value of the btn d
Value of the btn d
Value of the btn d
慕莱坞森
相关分类