I am trying to close a popup window, asking if a user wants to close the program. It has two button for yes/no. I have managed to close the entire program, but am struggling to close just the popup (if no is selected), due to "bad window path name". I suspect I am not calling the local variable exit_warning correctly, or not destroying it properly. Below is a simplified version of my code.
from tkinter import *
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("SePic")
self.pack(fill = BOTH, expand = 1)
ButtonExit = Button(root, text="Exit", command=show_exit_warning)
ButtonExit.pack()
#command implementation
def show_exit_warning():
exit_warning = Toplevel()
w = Label(exit_warning, text='Are you sure you want to quit?')
ButtonYes = Button(exit_warning, text="Yes", command=exitYesPress)
ButtonNo = Button(exit_warning, text='No', command=exitNoPress(exit_warning))
w.pack()
ButtonYes.pack()
ButtonNo.pack()
##General Implementations
def exitYesPress():
exit()
def exitNoPress(exit_warning):
exit_warning.destroy()
root = Tk()
app = Window(root)
root.mainloop()
My understanding is you want to you x.destroy() to destroy a Window 'x'. But as the window exit_warning is a local var in show_exit_warning, you need to send it to exitNoPress to do anything with it. Is this correct? Any other advice/guidance would be most appreciated. Thanks for your time.