title: How do I stop and restart a python thread within Tkinter GUI?
body: <pre><code>import tkinter as tk
  import tkinter.font
  import threading 
  
  def sensor_4():
      i = 0
      while True: 
           i+=1
  
  t4 = threading.Thread(target=sensor_4)
  
  mainwindow = tk.Tk()
  
  HEIGHT = 700
  WIDTH = 800
  
  canvas = tk.Canvas(mainwindow, height = HEIGHT, width = WIDTH)
  canvas.pack()
  
  frame = tk.Frame(mainwindow, bg='#08030D')  #inside box
  frame.place(relx=0, rely=0.1, relwidth = 0.95, relheight = 0.6)
  
  start_sensor4=tk.Button(frame, text = &quot;Press to Start 4&quot;, bg='#292230',fg='white',command = t4.start)
  start_sensor4.place(relx=0, rely=0.24, relwidth = 0.2, relheight = 0.05)
  
  mainwindow.mainloop()
  
  </code></pre>
  <p>In the code example, I am able to start the thread function by pressing the button within the Tkinter GUI. I am wondering how I can exit the thread function without the function having to return, and restart the thread by clicking on the same button or maybe a separate button.
  I also get the error stating that Threads can only be started once, if I click on the start button.</p>