2014-11-25 80 views
-3
from Tkinter import * 

class Program:  
    def __init__(self): 
     b = Button(text="click me", command=self.callback("1")) 
     b.pack() 

    def callback(self,s): 
     print "clicked!" 

program = Program() 

mainloop()  

为什么执行功能befor点击按钮? */传递参数到函数python

+0

这不是有效的Python。检查评论语法。 – 2014-11-25 07:48:33

+0

我编辑代码 现在可以回答我吗? – 2014-11-25 07:51:54

+0

请不要通过使用所有关键词来大喊大叫。 – 2014-11-25 07:52:32

回答

0

您应该传递函数参考command参数。否则,您可以执行该功能。

b = Button(text="click me", command=self.callback) 
# Or if you want to pass parameters 
b = Button(text="click me", command=lambda: self.callback("1")) 
+0

thannnnnnnnnks 其工作 – 2014-11-25 09:10:52