title: Tkinter menu bindings and accelerators
body: <p>I've come across a weird problem and I can't work out what is happening. I am working on a Tkinter application which utilises a menu. I have found that while creating the menu, in which all items have accelerators, some items require additional bindings to make the accelerators work (unless the menu bars are already selected) and some don't.</p>
  
  <p>When an additional binding is required, I have a problem with double entry key strokes. For example, if I open a <code>Toplevel</code> window, I get double entries every time I type a character in an <code>Entry</code> box, both in the <code>Toplevel</code> and in the main window. This only happens if the menu item is called via the key command.</p>
  
  <p>Most of the time this is not a problem, although I'd really like to know what the underlying cause is because it just seems wrong, but this particularly came to my attention recently when I implemented the built-in OS X Preferences menu, using the following code:</p>
  
  <pre><code>self.window.createcommand('::tk::mac::ShowPreferences', self._settings)
  </code></pre>
  
  <p>Now when I call the settings function from the build-in Preferences key command <code>Command-,</code>, which instantiates a new Toplevel window, this double entry is what happens. It does not happen if I navigate to the menu and open it with the mouse.</p>
  
  <p>The example below recreates the problem for me. The menu bar is not strictly necessary, but the problem occurs with both the menu bar and the OS X built-in Preferences item. Interestingly, 'Settings A' which does not require a binding reproduces the problem, but 'Settings B', which does require a binding, works fine. And again, only with key commands.</p>
  
  <pre><code>import Tkinter
  
  def settings(event = None):
      top = Tkinter.Toplevel()
      Tkinter.Entry(top).pack()
      top.mainloop()
  
  root = Tkinter.Tk()
  
  root.createcommand('::tk::mac::ShowPreferences', settings)
  
  menuBar = Tkinter.Menu(root)
  fileMenu = Tkinter.Menu(menuBar)
  fileMenu.add_command(label = 'Settings A', accelerator = 'Command-Shift-a', command = settings) # Does not require binding
  fileMenu.add_command(label = 'Settings B', accelerator = 'Command-b', command = settings)       # Requires binding
  menuBar.add_cascade(label = 'File', menu = fileMenu)
  root.config(menu = menuBar)
  root.bind('&lt;Command-b&gt;', settings)
  
  Tkinter.Entry(root).pack()
  root.mainloop()
  </code></pre>
  
  <p>I suspected it may be a computer issue but I have tried it on another machine and I get the same result. Does anybody have any idea what is happening here and how I can prevent it?</p>