2014-11-25 73 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