import tkinter
class App():
def __init__(self):
self.root = Tkinter.Tk()
button = Tkinter.Button(self.root, text = 'root quit', command=self.quit)
button.pack()
self.root.mainloop()
def quit(self):
self.root.destroy
app = App()
Run Code Online (Sandbox Code Playgroud)
如何使我的quit功能关闭窗口?
unu*_*tbu 52
def quit(self):
self.root.destroy()
Run Code Online (Sandbox Code Playgroud)
在destroy调用方法后添加括号.
使用command=self.root.destroy时将方法传递给Tkinter.Button 没有括号的方法,因为您希望Tkinter.Button存储方法以供将来调用,而不是在创建按钮时立即调用它.
但是当你定义quit方法时,你需要调用self.root.destroy()方法的主体,因为那时方法已被调用.