title: Python tkinter mainloop not quitting on closing the window
body: <p>I'm new to programming and Python, and am making a calculator app to practise.
  I'm using Tkinter with Python 2.7. The app has various buttons as you'd expect, and Entry and Label widgets for showing the numbers/result.</p>
  
  <p>I think my program does start mainloop, but closing the window doesn't stop mainloop. Since it did work as normal before I added an after loop, I assume the after is the problem. I'm also using wait_variable.</p>
  
  <p>I'd be very grateful if you could have a look at my code and give some advice!
  I've included the main stuff; the code to create the widgets and deal with user input/results output are in different files (Buttons, Displays, Inputs) but hopefully it's understandable without those.</p>
  
  <pre><code>import Tkinter as tk
  # These contain the other bits of code
  import Buttons as bt
  import Displays as ds
  import Inputs as ip
  
  
  class MainWindow(tk.Frame):
      def __init__(self, parent):
          tk.Frame.__init__(self, parent)
          self.parent = parent
          self.parent.title("Calculator")
  
          # Making frames to fill with widgets
          self.displays_frame = tk.Frame(parent)
          self.displays_frame.grid(padx=10, pady=10)
          self.buttons_frame = tk.Frame(parent)
          self.buttons_frame.grid()
          # Initialising the widgets and user input functions
          self.display = ds.Displays(self.displays_frame)
          self.button = bt.Button(self.buttons_frame)
          self.input = ip.Inputs()
  
      def take_inputs(self, parent):
          self.parent = parent
  
          # This waits for a button to be pressed
          self.wait_variable(self.button.button_pressed)
          # On a button press, its value is inserted into display Entry box/result is put in Label
          # Both self.button.button_pressed above and self.display.r below are StringVars
          self.input.process_input(self.button.button_pressed, self.display.entry_box, self.display.r)
          # Using after here so it keeps waiting for button presses to be recorded
          self.after(100, self.take_inputs, self.parent)
  
  def main():
      root = tk.Tk()
      app = MainWindow(root)
      # Here the wait_variable and after functions are called
      app.take_inputs(root)
      # The below string is printed after the first button has been pressed
      print "main, now starting mainloop" 
      root.mainloop()
      # "finished" is never printed
      print "finished"
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>As I suppose I've made my own event handler instead of using mainloop, I did try adding <code>self.parent.protocol("WM_DELETE_WINDOW", self.end(parent))</code> to the take_inputs method, so I could quit everything without needing to run mainloop. The self.end function there was a method I added to MainWindow class that printed "closing now" and then exited or destroyed the program.</p>
  
  <p>However, whatever function I put in there for <code>protocol</code> was run immediately; <code>"WM_DELETE_WINDOW"</code> wasn't being looked up properly (replacing "WM_DELETE_WINDOW" with "foo" didn't give an error).</p>
  
  <p>Thanks for your help!</p>