title: How to functional test if tkinter bind works
body: <p>I'm working on an application with the tkinter graphical user interface. With the MVC pattern. I am partially using TDD - I realize that it is unnecessary for GUI build part. However, I assume that I need to automate the testing of controller duties. Most of which rely on passing data from view to model.</p>
  <p>Passing process should be initialize by losing focus by entry widget, or by pressing Enter key. In my application there will be few different types of data to pass, so I decided to subclass widgets that have to initialize passing variables process.</p>
  <p>Sidenote: I found a topic, <a href="https://stackoverflow.com/questions/27581864/tkinters-event-generate-command-ignored">Tkinter's event_generate command ignored</a> where OP points in addendum that subclassing widgets is not a good idea. Nonetheless I don't know how to avoid it and still provide that many different widgets would have a standardized method to return different types of data.</p>
  <p>Minimal reproducible example (main.py):</p>
  <pre><code>import tkinter as tk
  
  
  class App():
  
      def __init__(self):
          self.model = Model()
          self.view = View(self.pass_value_to_model)
  
      def __enter__(self):
          return self
  
      def __exit__(self, exc_type, exc_val, exc_tb):
          self.view.quit()
          del self
  
      def run_gui(self):
          self.view.mainloop()
  
      def pass_value_to_model(self, value):
          self.model.variable = value
          print(f&quot;changed model variable value to : {self.model.variable}&quot;)
  
  class CustomEntry(tk.Entry):
  
      def __init__(self, *args, **kwargs):
          tk.Entry.__init__(self, *args, **kwargs)
          self.parent = args[0]
  
      def pass_value(self, event):
          self.parent.passing_method(self.get())
  
  class View(tk.Tk):
  
      def __init__(self, passing_method):
  
          tk.Tk.__init__(self)
          self.passing_method = passing_method
          self.frame = tk.Frame(self)
          self.frame.pack()
          self.variable = tk.StringVar(self.frame)
          self.entry = CustomEntry(self.frame, textvariable=self.variable)
          self.entry.pack(padx=100, pady=100)
  
          self.entry.bind('&lt;Return&gt;', self.entry.pass_value)
          self.entry.bind('&lt;FocusOut&gt;', self.entry.pass_value)
  
  
  class Model():
  
      def __init__(self):
          self.variable = &quot;init value&quot;
  
  if __name__ == '__main__':
          app = App()
          app.run_gui()
  </code></pre>
  <p>And this is working as I expect. Model variable is changing it's value properly. The problem is how to test this.</p>
  <p>Here's what I wrote in pytest (test_module.py):</p>
  <pre><code>import main
  
  def test_passing_value():
  
      with main.App() as controller:
          test_value = &quot;test&quot;
          controller.view.variable.set(test_value)
          controller.view.entry.event_generate(&quot;&lt;Return&gt;&quot;)
  
          assert controller.model.variable == test_value
  </code></pre>
  <p><code>event_generate</code> is not working. I suppose, that is because pytest don't start mainloop. If i code this - I can't automatically kill it. What I need to do to automate testing of this passing process?</p>
  <p>python version is 3.7</p>