Skip to content Skip to sidebar Skip to footer

I Get This Error "RuntimeError: Threads Can Only Be Started Once" When I Click Close And Then Click Run Again

import threading from tkinter import * running = False def run(): global running c = 1 running = True while running: print(c) c += 1 run_threa

Solution 1:

As the error said, terminated thread cannot be started again.

You need to create another thread:

import threading
from tkinter import *

running = False

def run():
    global running
    c = 1
    running = True
    while running:
        print(c)
        c += 1

def start():
    if not running:
        # no thread is running, create new thread and start it
        threading.Thread(target=run, daemon=True).start()

def kill():
    global running
    running = False

root = Tk()
button = Button(root, text='Run', command=start)
button.pack()
button1 = Button(root, text='close', command=kill)
button1.pack()
button2 = Button(root, text='Terminate', command=root.destroy)
button2.pack()
root.mainloop()

Post a Comment for "I Get This Error "RuntimeError: Threads Can Only Be Started Once" When I Click Close And Then Click Run Again"