title: call function after certain time and kill on button press, tkinter
body: <p>I have made an entry widget and it gets the number entered when they click OK (this becomes the number of seconds for function to recur(n)), I want to call the function every n seconds. When OK is clicked, I want a button to show on the screen that will stop the function from recurring. How can I do this?</p>
  
  <p><strong>This is what I have:</strong></p>
  
  <pre><code>def ok_func():
      global stop
      stop = 1
      timer_pop.destroy()
      seconds = seconds_entry.get()
      seconds = int(seconds)
      stop_timer.grid()
      while stop == 1:
          stop_timer.config(highlightbackground=mycolor)
          background()
          time.sleep(seconds)
  </code></pre>
  
  <p>This is what I have for the stop timer button:</p>
  
  <pre><code>def stopTimer():
      global stop
      stop = 0
      stop_timer.grid_forget()
  </code></pre>
  
  <p>Thanks
  edit:</p>
  
  <pre><code>global counter
  counter = 0
  
  def ok_func():
      global stop_timer
      print('function: "ok" is running now.')
      global counter
      counter += 1
      def stopTimer():
          global recur
          stop_timer.destroy()
          timer_pop.after_cancel(recur)
      try:
          if counter == 1:
              global time
              time = int(seconds_entry.get())
              timer_pop.destroy()
              stop_timer = Button(app, text="Stop Timer", command=stopTimer)
              stop_timer.grid(row=0, column=6, padx=10)
              #stop_timer.config(highlightbackground=ok_func)
              global recur
              print ('function will start again in', time, 'seconds')
              recur = app.after(1000*time, background)
          else:
              print ('function will start again in', time, 'seconds')
              recur = app.after(1000*time, background)
              #stop_timer.config(highlightbackground=mycolor)
      except ValueError:
          counter = 0
          print("thats not a number")
  </code></pre>
  
  <p>I tried what you said but still not working. The color only changes once, then stops. Also, I would like the stoptimer button to change background with the background but it doesn't work. Thanks for your help.</p>