title: In Python3/tkinter how to intercept when the user clicks the close button on a Toplevel window
body: <p>I want to have one main window and one or more <code>Toplevel()</code> windows that may be opened on demand. I am able to create the windows and even to destroy them. </p>
  
  <p>However, I am trying to implement a button in the main window that both opens and closes the second window (that second window should be always unique, i.e. never open twice at the same time). This is what I have now, after fiddling a little:</p>
  
  <pre><code>#!/usr/bin/python3
  
  import tkinter as tk
  from tkinter import ttk
  import tkinter.font
  
  
  class baseApp(ttk.Frame):
      def __init__(self,master,*args,**kwargs):
          super().__init__(master,*args,**kwargs)
          self.master = master
  
          self.mainframe = ttk.Frame(master)
          self.topframe = ttk.Frame(self.mainframe, padding="5 8 5 5")
  
          self.topframe.pack(side=tk.TOP, fill=tk.X)
          self.mainframe.pack(side=tk.TOP, expand=True, fill=tk.BOTH)
  
  
  class App(baseApp):
      def __init__(self,master,*args,**kwargs):
          super().__init__(master,*args,**kwargs)
          self.master = master
          self.button1 = ttk.Button(self.topframe,text="One",command=self.button_one)
          self.btn_remessas = ttk.Button(self.topframe,text="Open/close Toplevel window",command=self.create_window1)
          self.button1.grid(row=0,column=0)
          self.btn_remessas.grid(row=0,column=1)
          self.topframe.pack(side=tk.TOP, fill=tk.X)
          self.mainframe.pack(side=tk.TOP, expand=True, fill=tk.BOTH)
  
      def create_window1(self):
          if current_state.window2_open == False:
              self.newWindow2 = tk.Toplevel(self.master)
              self.newWindow2.geometry('600x500+680+0')
              self.newWindow2.title('Second window')
              self.janela_remessas = SecondWindow(self.newWindow2)
              current_state.window2_open = True              
          else:
              self.newWindow2.destroy()
              root.update_idletasks()
              current_state.window2_open = False
  
      def button_one(self):
          print("button 1 pressed")
  
  
  class SecondWindow:
      def __init__(self,master,*args,**kwargs):
          #super().__init__(master,*args,**kwargs)
          self.mainframe = ttk.Frame(master, padding="5 8 5 5")
          self.topframe = ttk.Frame(self.mainframe)
          self.button1 = ttk.Button(self.topframe,text="button",command=self.button_function)
          self.button1.pack()                
          self.topframe.pack(side=tk.TOP, fill=tk.X)
          self.mainframe.pack(side=tk.TOP, expand=True, fill=tk.BOTH)
  
      def button_function(self, *event):
          print("user just pressed button")
  
      def close_window(self, *event): #Please fix me!
          current_state.window2_open = False
          self.destroy()
  
  
  class AppStatus:
      def __init__(self):
          self.window2_open = False  
  
  
  if __name__ == "__main__": 
      root = tk.Tk() 
      app = App(root) 
      current_state = AppStatus()
      root.configure(background='grey95')
      root.title('Application window')
      root.geometry('1000x760+0+0')
      root.bind_all("&lt;Mod2-q&gt;", exit)
      root.mainloop()
  </code></pre>
  
  <p>Right now, if the user clicks the close window button, or the corresponding keyboard shortcut, the app will not know that the second window does not exist anymore, so when we press the button to open/close the window, nothing happens. Also, if we keep pressing the open/close button, there are some times when the button does not open the second window. What am I doing wrong?</p>