tkinter threaded gui instance halts all further spawns
When I create a tkinter window instance using a thread, even though the window is destroyed after execution, and the thread is joined; I can't make another tkinter window later on in the program flow. Why?
def on_button_click(root): //destroys window on button click
root.destroy()
def init(): //thread calls this
root=Tk()
b = Button(root, text="OK", command=lambda:开发者_开发技巧on_button_click(root))
b.pack()
root.mainloop()
t = Thread(target=init)
t.start()
t.join()
root=Tk() //program flow halts here with no window being displayed
root.mainloop()
From what I can gather using my Google-foo, the problem is that the Tk event loop (which is created during your call to root.mainloop()) is single-threaded and you can only have one instance of it at a time. So that's probably why its getting stuck at that location. Your thread is properly setting up the Tk subsystem but the program fails when you try to create a second Tk subsystem to run at the same time. Your call to root.destroy() is only destroying the windows that you created and not the entire Tk subsystem.
It's been a while since I used Tk but I'd suggest calling root.mainloop() once when you first start your program and then leave your functions to instantiate Tk windows only, not the entire Tk subsystem.
精彩评论