title: Python Tkinter - Protocol Handler on CLose Window Event
body: <p>How will I overwrite the exit function a Top Level Window if the user clicks on the X button at the upper right of the window? I have tried this code with no fortune.</p>
  
  <pre><code>def chat():
  global root, private_chat, chat_list_window, chat_list, ip, port, address,username
  global private_chat_window, private_chat_messages, private_chat_label, private_chat_entry
  address = (ip,int(port))
  
  
  invited_client = chat_list[len(chat_list) -1].get(chat_list[len(chat_list) -1].curselection())
  
  private_chat_window.append(tk.Toplevel(root))
  length = len(private_chat_window)
  private_chat_window[length-1].title("Private Chat with '" +  invited_client[2] + "'" )
  
  
  private_chat_messages.append(Listbox(private_chat_window[length -1], width=80, height=30))
  private_chat_messages[length-1].grid(row=1,column=1, columnspan=3,rowspan=1)
  private_chat_messages[length-1].insert(END, "Inviting client '" + invited_client[2] + "'for a private chat...")
  
  
  private_chat_label.append(ttk.Label(private_chat_window[length -1], text='Message Entry Box : '))
  private_chat_label[length-1].grid(row=2,column=1)
  
  
  private_chat_entry.append(ttk.Entry(private_chat_window[length -1] , width=60))
  private_chat_entry[length-1].grid(row=2, column=2, columnspan = 2)    
  
  
  daemon = Pyro4.Daemon()       
  uri_str = daemon.register(Messaging())      
  
  private_chat_window[length-1].bind('&lt;KeyPress&gt;', lambda event: private_chat_onkeypress(event,private_chat_messages[length -1], private_chat_entry[length -1], uri_str = uri_str) )
  chat_list_window[len(chat_list_window)-1].destroy()
  
  s.sendto(str.encode(":::PrivateChatInvitation:::" + '&lt;' + username + '&gt;&lt;' +  str(uri_str) + '&gt;' + json.dumps(invited_client)) , address)
  
  
  Thread(target = daemon.requestLoop).start()
  Thread(target=check_successful_connection(uri_str, private_chat_messages[length -1])).start()
  
  #EXIT
  private_chat_window[length -1].protocol("WM_DELETE_WINDOW", on_closing(private_chat_window[length -1]))
  private_chat_window[length -1].mainloop()
  
  
  def on_closing(window_instance):
  
      if messagebox.askokcancel("Quit", "Do you want to quit?"):
           window_instance.destroy()
  </code></pre>
  
  <p>The protocol handler doesn't  work on the top level window but when attached to the root main window, it works fine. Below is the code:</p>
  
  <p>def render_gui():
      global root
      global prev_key
      global listbox, label1, entry1, username
      prev_key = 0</p>
  
  <pre><code>root = tk.Tk()
  root.title("Welcome Client '" + username + "'" )
  
  
  listbox = Listbox(root, width=80, height=30)      
  listbox.grid(row=1,column=1, columnspan=5,rowspan=3)
  
  
  vsb = tk.Scrollbar(root, orient="vertical", command=listbox.yview)
  hsb = tk.Scrollbar(root, orient="horizontal", command=listbox.xview)
  vsb.grid(row=1, column=4,rowspan = 3, sticky='ns')
  hsb.grid(row=3, column=0,rowspan = 1, columnspan=5, sticky = 'wes')
  
  
  #Menubar
  
  menubar = Menu(root)
  filemenu = Menu(menubar,tearoff=0)
  filemenu.add_command(label="Private Chat",command=private_chat_threader) #Hmppp
  menubar.add_cascade(label="Options", menu= filemenu)
  root.config(menu=menubar)
  
  listbox.configure(yscrollcommand=vsb.set,xscrollcommand=hsb.set)
  
  label1 = ttk.Label(root, text='Message Entry Box : ')
  label1.grid(row=4,column=1)
  
  entry1 = ttk.Entry(root, width=60)
  entry1.grid(row=4, column=2)    
  
  root.geometry('490x520')
  root.bind('&lt;KeyPress&gt;', onKeyPress)
  
  root.protocol("WM_DELETE_WINDOW", on_closing(root))
  root.mainloop()
  </code></pre>
  
  <p>Please help me. Thanks.</p>