【发布时间】:2014-02-20 00:43:54
【问题描述】:
当我想添加一个按钮时
b = Button(app_name,text="..." , command = any_function)
是否可以使用带有参数的函数
b = Button(app_name, text="..." , command = any_function(7))
我试过了
(...)
def cash(price):
global total
total.set(total.get() + price)
total = IntVar()
total.set(0)
b1 = Button(app,text = "one ",width = 10,command = cash(1))
b1.pack()
b2 = Button(app,text = "two ",width = 10,command = cash(10))
b2.pack()
b3 = Button(app,text = "three ",width = 10,command = cash(100))
b3.pack()
b4 = Button(app,text = "four ",width = 10,command = cash(1000))
b4.pack()
l_total = Label(app,textvariable = total)
l_total.pack()
(...)
但是l_total 在程序运行时(1000+100+10+1)已经是 1111,就像我按下了四个按钮一样,而且这些按钮也没有向 l_total 添加值。我只想知道为什么它不起作用,因为我知道一个解决方案。
【问题讨论】:
标签: python python-3.x tkinter