如何完全关闭tkinter-app?

时间:2017-12-18 15:43:40

标签: python-3.x tkinter

以下代码:

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。按下时按钮会消失,但窗口会停留。 如何通过按钮单击关闭窗口和应用程序?

1 个答案:

答案 0 :(得分:0)

您的代码中缺少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()