2016-07-25 48 views
0

我不明白爲什麼這個代碼不工作:關閉主窗口的Tkinter結合的關鍵

import tkinter 

class Application(): 

def__init__(self): 

     self.master = tkinter.Tk() 
     self.master.bind("<Enter>", self.quit) 
     self.master.mainloop() 

    def quit (self): 
     self.master.destroy() 

my_app = Application() 

我不斷收到錯誤:「退出()需要1周位置的說法,但2被賦予」。有沒有辦法關閉一個主鍵的Tkinter窗口綁定?

感謝

回答

1

只是另一個變量添加到戒菸方法(「I」,「N」等),當你綁定一個事件的方法,該方法必須能夠處理該事件作爲參數。

import tkinter 

class Application(): 

    def __ init __ (self): 

     self.master = tkinter.Tk() 
     self.master.bind("<Enter>", self.quit) 
     self.master.mainloop() 

    def quit (self,n): 
     self.master.destroy() 

#notice that the n variable doesnt really do anything other than "handling" of the event, so when 
#it gets 2 arguments it can handle 2 parameters without giving an exception 
#the (old) method only had space for 1 argument (self), but the moment you "bind" a button or event 
#the method MUST be able to handle such information 

my_app = Application() 
+0

謝謝@Byron Mh! – joseluis

+1

不要只爲變量選擇一個隨機字母。將它命名爲'event',以便明確這個額外的參數是什麼。 –