How to destroy a parent window when child window's red X is clicked in Tkinter 8.5 and Python 3.3
Question: Newbie question here. I would like for `secondWindow` to be destroyed in
addition to `thirdWindow` when the red X is clicked in `thirdWindow`, just
like what happens when you click `thirdButton` in `thirdWindow`. `firstWindow`
should remain alive.

Below is some example code:

    
    
    import tkinter
    from tkinter import ttk 
    
    def secondWindow():
        secondWindow = tkinter.Toplevel(firstWindow)
        secondFrame = ttk.Frame(secondWindow)
        secondFrame.pack()
        secondButton = ttk.Button(secondFrame, text="proceed to 3rd window",\
                                  command=lambda: thirdWindow(secondWindow))
        secondButton.pack()
    
    def thirdWindow(parent):
        thirdWindow = tkinter.Toplevel(parent)
        thirdFrame = ttk.Frame(thirdWindow)
        thirdFrame.pack()
        thirdButton = ttk.Button(thirdFrame, text="close 2nd and 3rd windows",\
                                 command=lambda: destroy(thirdWindow, parent))
        thirdButton.pack()
    
    def destroy(self, parent):
        self.destroy()
        parent.destroy()
    
    firstWindow = tkinter.Tk()
    firstFrame = ttk.Frame(firstWindow)
    firstFrame.pack()
    firstButton = ttk.Button(firstFrame, text="proceed to 2nd window",\
                             command=secondWindow)
    firstButton.pack()
    firstWindow.mainloop()
    

Thanks in advance for any help you can provide!


Answer: Use `parent` to run `parent` function or get variable from `parent` \- for
example to get its `parent` or run function which close its parent.

    
    
    import tkinter
    from tkinter import ttk 
    
    def secondWindow(parent):
        secondWindow = tkinter.Toplevel(parent)
        secondWindow.parent = parent # remember parent
    
        secondFrame = ttk.Frame(secondWindow)
        secondFrame.pack()
        secondButton = ttk.Button(secondFrame, text="proceed to 3rd window",\
                                  command=lambda: thirdWindow(secondWindow))
        secondButton.pack()
    
    def thirdWindow(parent):
        thirdWindow = tkinter.Toplevel(parent)
        thirdWindow.parent = parent # remember parent
    
        thirdFrame = ttk.Frame(thirdWindow)
        thirdFrame.pack()
        thirdButton = ttk.Button(thirdFrame, text="close 2nd and 3rd windows",\
                                 command=lambda: destroy(thirdWindow))
        thirdButton.pack()
    
    def destroy(self):
        self.parent.parent.destroy()  # destroy grand-parent and all children are dead too.
    
    firstWindow = tkinter.Tk()
    firstFrame = ttk.Frame(firstWindow)
    firstFrame.pack()
    firstButton = ttk.Button(firstFrame, text="proceed to 2nd window",\
                             command=lambda:secondWindow(firstWindow))
    firstButton.pack()
    firstWindow.mainloop()
    

