将参数传递给函数python

时间:2014-11-25 07:47:04

标签: python python-2.7 user-interface tkinter

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()     

为什么在点击按钮之前执行功能? * /

1 个答案:

答案 0 :(得分: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"))