当顶层关闭tkinter时如何关闭整个GUI

时间:2019-07-31 14:35:50

标签: python tkinter

我正在编写一个tkinter应用程序,该应用程序创建具有多个顶级的类。当按下顶层中的任何X按钮时,我需要能够关闭整个gui。我该怎么办?

def main():
    root = tk.Tk()
    app = Example(master=root)
    app.mainloop()

class Example(tk.Frame):
    def __init__(self, master):
        self.master = master
        super().__init__(master)
        self.initUI()

 def initUI(self):
        self.master.withdraw()
        self.initUIL = tk.Toplevel(self.master)

        self.initUIL.title('Init')
        self.pack(fill = tk.BOTH, expand=1)

        frame1 = tk.Frame(self.initUIL)

       #I need to close the whole gui when the x in this toplevel is pressed

1 个答案:

答案 0 :(得分:2)

我解决了这个问题,这非常简单,您需要为每个顶级更改协议。

self.toplevel.protocol("WM_DELETE_WINDOW", self.ask_quit)

def ask_quit():
    MsgBox = tk.messagebox.askquestion ('Quit',"Are you sure you want to quit?")
    if MsgBox == 'yes':
       self.master.destroy()