0

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.

  • 1
    Don't know for what reason you're passing something to `exitNoPress`, but in this case you need a `lambda` function as a `command` parameter to prevent execution of `exitNoPress` when `ButtonNo` declared. `ButtonNo = Button(exit_warning, text='No', command=lambda: exitNoPress(exit_warning))` – CommonSense Apr 25 '17 at 10:33
  • Thank you very much, CommonSense. That has solved my problem. I had not heard of lambda function before. I'm afraid I cannot give you points for this comment, if you would like to answer it I can give you the points. – user7914722 Apr 25 '17 at 10:40
  • Thank you very much for that link! It has clarified a lot of things. – user7914722 Apr 25 '17 at 11:04

0 Answers0