Skip to content Skip to sidebar Skip to footer

Tkinter: RuntimeError: Threads Can Only Be Started Once

but I'm trying to make the GUI for my script, when I click bt_send I start a thread (thread_enviar), that thread also start other thread (core), the problem is that thread_enviar i

Solution 1:

Question: RuntimeError: threads can only be started once

As I understand, you don't want to run multiple threads, you only want to do a Task in a thread to avoid freezing the Tk().mainloop().
To inhibit, to start a new thread while the previous thread are still running you have to disable the Button or verify if the previous thread is still .alive().

Try the following approach:

import tkinter as tk
import threading, time

class Task(threading.Thread):
    def __init__(self, master, task):
        threading.Thread.__init__(self, target=task, args=(master,))

        if not hasattr(master, 'thread_enviar') or not master.thread_enviar.is_alive():
            master.thread_enviar = self
            self.start()

def enviar(master):
    # Simulating conditions
    if 0:
        pass
    #if any(...
    #elif len(data) < 300:
    else:
        #master.pg_bar.start(500)

        # Simulate long run
        time.sleep(10)
        #rnn10f.forecasting(filepath)

        print("VIVO?")
        #master.pg_bar.stop()

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        bt_send = tk.Button(text="Send", bg="blue", 
                            command=lambda :Task(self, enviar))

if __name__ == "__main__":
    App().mainloop()

Post a Comment for "Tkinter: RuntimeError: Threads Can Only Be Started Once"