Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

python - Error while destroying tkinter root application with a different thread

I want to have a tkinter popup which should close after a certain task is complete but as soon as the popup comes up that execution of the code stops there and wait until the popup is destroyed using root.destroy()

Then I tried to use thread and and call that kill_popup() function simultaneously and it killed the popup but also crashed the program by giving an error async handler deleted by the wrong thread tkinter

My understanding about this error is that it comes because my tkinter popup in running on different thread and kill_popup() is on different so when a different thread is trying to kill popup then this error is coming.

I used this method to parallely call that kill_popup() method which doesn't require threading root.after(1000, lambda : kill_popup())

But in this case when the executing of kill_popup() goes on then tkinter popup freezes and i cannot do anything on popup,so I wanted a solution to this problem as I need a OK button on popup which will perform certain task button on popup.

So can anyone please help me with a solution, I am adding my code as well

def prompt_popup():
    global root
    global inputText
    root = Tk()
    root.geometry("750x405")
    my_frame=Frame(root)
    my_frame.pack(pady=18)
    button=Button(my_frame,text="YES",width=8,command=lambda: Action_Pass(),bg="white")
    button.grid(row=0,column=0,padx=20)
    button=Button(my_frame,text="NO",width=8,command=lambda: Action_Fail(),bg="white")
    button.grid(row=0,column=1,padx=20)        
     
    root.after(1000, lambda : kill_popup())
    root.mainloop()

def kill_popup():
    while True:
        time.sleep(2)
        # condition
    root.destroy()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...