There is an interesting issue with Tkinter and threading: I have a Tkinter based GUI and some code executed alongside mainloop. It works like charm if I only do it once. But if i do it twice Tkinter.Tk() blocks BOTH threads: GUI and MainThread.
here is the code (inspired by another Tkinter vs threading topic):
import Tkinter
import threading
import logging
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s: %(message)s")
def gui():
logging.info("Starting GUI")
root = Tkinter.Tk()
logging.info("Setup GUI")
root.after(500, root.destroy)
root.mainloop()
logging.info("Mainloop is over")
def start_gui_thread():
gui_thread = threading.Thread(None, gui, None)
gui_thread.setDaemon(True)
gui_thread.start()
return gui_thread
def test():
gui_thread = start_gui_thread()
logging.info("Do something alongside gui")
logging.info("Wait for gui to terminate")
gui_thread.join()
logging.info("thread terminated")
logging.info("--------- 1 ---------")
test()
logging.info("--------- 2 ---------")
test()
result: winxp + python 2.7.1 (default installation)
INFO: --------- 1 ---------
INFO: Do something alongside gui
INFO: Starting GUI
INFO: Wait for gui to terminate
INFO: Setup GUI
INFO: Mainloop is over
INFO: thread terminated
INFO: --------- 2 ---------
INFO: Starting GUI
INFO: Do something alongside gui
UPD result with OS X 10.6.7 + python 2.7.1
MacBook-Pro:~ igann$ python test.py
2012-07-05 21:01:52,939 INFO: --------- 1 ---------
2012-07-05 21:01:52,940 INFO: Starting GUI
2012-07-05 21:01:54,835 INFO: Do something alongside gui
2012-07-05 21:01:54,835 INFO: Wait for gui to terminate
2012-07-05 21:01:54,835 INFO: Setup GUI
2012-07-05 21:01:55,337 INFO: Mainloop is over
2012-07-05 21:01:55,339 INFO: thread terminated
2012-07-05 21:01:55,339 INFO: --------- 2 ---------
2012-07-05 21:01:55,339 INFO: Starting GUI
2012-07-05 21:01:55,366 INFO: Do something alongside gui
2012-07-05 21:01:55,366 INFO: Wait for gui to terminate
2012-07-05 21:01:55,367 INFO: Setup GUI
Tcl_WaitForEvent: CFRunLoop finished
Abort trap
MacBook-Pro:~ igann$
It works fine for me (OS X 10.7, Python 2.7.1) so maybe it's related to your installation:
but operations do not always execute in the same order, of course (compare this with above):
On 10.6.8 with Python 2.6.1 it also works, but with strange messages: