1

I need to stop the while loop via stop_button function. Tried several ways but failed. Looking for tips, thanks.

def start_button():
    t1 = threading.Thread(target=thread1)
    t1.start()

def thread1():
    while True:
        start()

def stop_button():
    pass
martineau
  • 119,623
  • 25
  • 170
  • 301
JesJos
  • 43
  • 6
  • 1
    Add something like `continue=True` before while statement. Change while to `while continue`. Make stop button return `False` and if pressed assign it to `continue` variable. – NotAName Jun 15 '21 at 01:00
  • Possible duplicated of: https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread – Bruno Jun 15 '21 at 01:00
  • 1
    You could use a [`threading.Condition`](https://docs.python.org/3/library/threading.html#condition-objects) variable to notify it to stop. See [How to start and stop thread?](https://stackoverflow.com/questions/15729498/how-to-start-and-stop-thread) for an example. – martineau Jun 15 '21 at 01:02

1 Answers1

2

Maybe this

flag = False

def start_button():
    flag = True
    t1 = threading.Thread(target=thread1)
    t1.start()

def thread1():
    while flag:
        start()

def stop_button():
    flag = false
Erick Dávila
  • 347
  • 5
  • 9