tkinter: RuntimeError: threads can only be started once

前端 未结 1 1571
一整个雨季
一整个雨季 2021-01-25 17:30

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

相关标签:
1条回答
  • 2021-01-25 17:55

    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()
    
    0 讨论(0)
提交回复
热议问题