如何完全关闭tkinter-app?

问题描述 投票:0回答:1

以下代码给出:

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

python-3.x tkinter
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.