title: How to run a function in the background of tkinter
body: <p>I am new to GUI programming and I want to write a Python program with tkinter. All I want it to do is run a simple function in the background that can be influenced through the GUI.</p>
  
  <p>The function counts from 0 to infinity until a button is pressed. At least that is what I want it to do. But I have no idea how I can run this function in the background, because the mainloop() of tkinter has control all the time. And if I start the function in an endless loop, the mainloop() cannot be executed and the GUI is dead.</p>
  
  <p>I would like to return control back to the mainloop() after each cycle, but how can I get the control back from the mainloop() to the runapp-function without a user-triggered event?</p>
  
  <p>Here is some sample code that kills the GUI:</p>
  
  <pre><code>from Tkinter import *
  
  class App:
      def __init__(self, master):
  
          frame = Frame(master)
          frame.pack()
  
          self.button = Button(frame, text="START", command=self.runapp)
          self.button.pack(side=LEFT)
  
          self.hi_there = Button(frame, text="RESTART", command=self.restart)
          self.hi_there.pack(side=LEFT)
  
          self.runapp()
  
      def restart(self):
          print "Now we are restarting..."
  
      def runapp(self):
          counter = 0
          while (1):
              counter =+ 1
              time.sleep(0.1)
  </code></pre>