以下代码给出:
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.quitButton = tk.Button(self, text='Quit', command=self.destroy)
self.quitButton.grid()
app = Application()
app.master.title('Sample application')
app.mainloop()
运行此代码时,会出现一个小窗口,其中包含QUIT-Button。按下时按钮会消失,但窗口会停留。如何通过按钮单击关闭窗口和应用程序?
您在代码中缺少self.master.destroy
,按下按钮Quit
会破坏窗口
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.quitButton = tk.Button(self, text='Quit', command=self.master.destroy)
self.quitButton.grid()
app = Application()
app.master.title('Sample application')
app.mainloop()