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

classTask(threading.Thread):
    def__init__(self, master, task):
        threading.Thread.__init__(self, target=task, args=(master,))

        ifnothasattr(master, 'thread_enviar') ornot master.thread_enviar.is_alive():
            master.thread_enviar = self
            self.start()

defenviar(master):
    # Simulating conditionsif0:
        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()classApp(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"