我不明白为什么这段代码不起作用:
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()
我一直收到错误:“quit()需要1个位置参数,但是给出了2个”。有没有办法关闭绑定密钥的主Tkinter窗口?
谢谢
答案 0 :(得分:1)
只需将另一个变量添加到quit方法(&#34; i&#34;,&#34; n&#34;等),当您将事件绑定到方法时,该方法必须能够处理将事件称为参数。
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()