title: Tk/Tkinter: Detect application lost focus
body: <p>I am making a Tkinter application.  In the application, I want to pop up a context menu, which I do using Tk.Menu.post().  </p>
  
  <p><strong>I don't know how to make this menu disappear when the application loses focus.</strong>  I need to do this because the menu stays on top, even when switching to another window, leaving a menu 'artifact'.</p>
  
  <p>I put a <code>&lt;FocusOut&gt;</code> event on the menu, which is triggered if the menu has focus and the user moves focus to another application.  This works well.</p>
  
  <p>What do I do if the main application window has focus?  I could put a <code>&lt;FocusOut&gt;</code> event on the application window, which closes the menu; however, this ends up being called when I give focus to the menu, which closes the menu. The menu is created with parent as the main application, so I am not sure why <code>&lt;FocusOut&gt;</code> on the main app is even triggered when the menu gets focus.</p>
  
  <p><strong>How do I distinguish between the main application window losing focus to a different application vs losing focus to my menu?</strong>  </p>
  
  <p>I don't want to use tk_popup() because I want the user to continue to provide input to the main window. (use of the menu is optional).</p>
  
  <p><strong>Thanks to @Brad Lanam I came up with a SOLUTION, which I have included:</strong></p>
  
  <pre><code>from Tkinter import *
  
  class App(Tk):
      def __init__(self, *args, **kwargs):
          Tk.__init__(self, *args, **kwargs)
  
          self.entry = Entry(self)
          self.entry.grid(padx=30, pady=30)
          self.entry.focus_set()
          self.entry.bind("&lt;Tab&gt;", self.put_menu)
          self.entry.bind("&lt;Down&gt;", self.set_focus_on_menu)
  
          self.menu = Menu(self, tearoff=False)
          self.menu.add_command(label="test")
          self.menu.bind("&lt;FocusOut&gt;", self.destroy_menu)
  
  
          self.bind("&lt;FocusIn&gt;", self.app_got_focus)
          self.bind("&lt;FocusOut&gt;", self.app_lost_focus)
          self.bind("&lt;3&gt;", self.put_menu)
  
  
      def app_got_focus(self, event):
          self.config(background="red")
  
      def app_lost_focus(self, event):
          self.config(background="grey")
  
          ######## SOLUTION BEGIN #########
          if self.focus_get() != self.menu:
              self.destroy_menu(event)
          ######## SOLUTION END ###########
  
      def set_focus_on_menu(self, event):
          self.menu.focus_set()
  
      def menu_got_focus(self, event):
          self.menu.activate(0)
  
      def put_menu(self, event):
          self.menu.post(self.winfo_x() + self.entry.winfo_x(), self.winfo_y() + self.entry.winfo_y()+20)
  
      def destroy_menu(self, event):
          self.menu.destroy()
  
  app = App()
  
  app.mainloop()
  </code></pre>