title: Tkinter's event_generate command ignored
body: <p>I am trying to figure out how to unittest  a bind command in a dialog window. I'm attempting this with tkinter's <code>event_generate</code>. It is not working the way I expect. For this StackOverflow question I've set up some code with a single call to <code>event_generate</code>. Sometimes that line works and sometimes it is as if the line doesn't even exist.</p>
  <p>The bind in the dialog's <code>__init__</code> method looks like this:</p>
  <pre><code>        self.bind('&lt;BackSpace&gt;',  #Print &quot;BackSpace event generated.&quot;
              lambda event: print(event.keysym, 'event generated.'))
  </code></pre>
  <p>Any action in the dialog will call back to its terminate method (The dialog is based on Frederik Lundh's example Dialog in 'An Introduction to Tkinter'.)</p>
  <pre><code>    def terminate(self, event=None):
          print('terminate called')  # Make sure we got here and the next line will be called
          self.event_generate('&lt;BackSpace&gt;')
          self.parent.focus_set()
          self.destroy()
  </code></pre>
  <p>When the dialog is called using the code below any user action will end up calling <code>terminate</code>. In each case &quot;terminate called&quot; and &quot;BackSpace event generated.&quot; are displayed. This proves that the call to event_generate is set up correctly.</p>
  <pre><code>parent = tk.Tk()
  dialog = Dialog(parent)
  dialog.wait_window()
  </code></pre>
  <p>In case it's relevant I ought to mention that I have moved Lundh's call to <code>self.wait_window</code> from his dialog's <code>__init__</code> method to the caller. Whilst this breaks the neat encapsulation of his dialog it appears to be necessary for automated unittests. Otherwise the unittest will display the dialog and halt waiting for user input.  I don't like this solution but I'm not aware of any alternative.</p>
  <p>The problem I'm having is when <code>wait_window</code> is replaced with a direct call to the <code>terminate</code> method. This is the sort of thing that I'd expect to be able to do in unittesting which is to test my GUI code without running tkinter's mainloop or wait_window.</p>
  <pre><code>parent = tk.Tk()
  dialog = Dialog(parent)
  dialog.terminate()
  </code></pre>
  <p>This only prints &quot;terminate called&quot; and does not print &quot;BackSpace event generated.&quot;. The call to <code>event_generate</code> appears to have no effect. If I follow the call in the debugger I can see that tkinter's <code>event_generate()</code> is being called with the correct arguments. <code>self = {Dialog} .99999999, sequence = {str}'&lt;BackSpace&gt;', kw = {dict}{}</code>
  In view of the warning in the TkCmd man pages about window focus I have verified the dialog with the binding is given focus in its <code>__init__</code> method.</p>
  <p>Tkinter is not executing the callback. Why?</p>
  <p>EDIT: This bare bones code shows <code>update</code> working. However, it only works if it is called in <code>__init__</code> before <code>event_generate</code> is called by the main program. (This puzzle has been raised as a <a href="https://stackoverflow.com/questions/27603822/unexpected-order-of-tkinters-event-generate-and-update-commands">separate question</a>)</p>
  <pre><code>class UpdWin(tk.Tk):
      def __init__(self):
          super().__init__()
          self.bind('&lt;BackSpace&gt;',
                    lambda event: print(event.keysym, 'event generated.'))
          self.update()  # Update works if placed here
  
  
  app = UpdWin()
  app.event_generate('&lt;BackSpace&gt;')
  # app.update() # Update doesn't work if placed here
  </code></pre>
  <h1>Six Years On</h1>
  <p>4/12/2021. See Mark Roseman's <a href="https://tkdocs.com/tutorial/eventloop.html#nested" rel="nofollow noreferrer">excellent web site</a> for a detailed explanation of why any use of <code>update</code> is a bad idea.</p>
  <p>The problem posed by this six year old question is entirely avoided by better program design in which tkinter widget objects are <em>never</em> subclassed. Instead they should be created by composition where they can be easily monkey patched. (This advice is contrary to patterns shown in  Frederik Lundh's example Dialog in 'An Introduction to Tkinter'.)
  For unittest design, not only is there no need to start Tk/Tcl via tkinter but it is also unwise.</p>