5
votes

I need to run some simple function in multi-threading with a Tkinter GUI, so I've tried mtTkinter.

Everything works fine except for a particular: even if I just start the GUI and then I close it without touching nothing some thread keeps running.

In other words; I have this code:

from Tkinter import *
root = Tk()

#simple GUI code with buttons, labels, text and scrollbars widget
...
...    
root.mainloop()

If I run this code the GUI appears and when I close it this python script ends successfully.

Now if I replace Tkinter with mtTkinter

from mtTkinter import *
root = Tk()

#simple GUI code with buttons, labels, text and scrollbars widget
...
...    
root.mainloop()

the GUI appears once again, but if I close it there is still some thread from mtTkinter that keeps running!

Any help would be apprecied, thank you in advance and sorry for my bad english!

3
You can do multi-threaded functions, just use the thread that Tcl's interpreter was created to update Tk's GUI elements (this stands for all the major GUI toolkits). mtTkinter isn't supposed to be used. If you need further help, include the actual code you have. - mmgp
very ugly workaround: os._exit(0) - Gonzo
@mmgp: how would you start the mainloop non blocking? - Gonzo
You don't start it non-blocking. - mmgp
did you set the daemon flag? stackoverflow.com/questions/4330111/… - Gonzo

3 Answers

3
votes

I ran into a similar problem for my application (https://github.com/joecole889/spam-filter). After some investigation, I realized that when I close my application Tkinter (or possibly Matplotlib) uses a threading._DummyThread instance to delete one of the widgets. I have a Matplotlib graph in a Tkinter canvas widget in my application. In any case, it looks like an “image delete” event is added to the event queue and mtTkinter blocks waiting for a response on the responseQueue that never comes.

I was able to fix the problem by allowing events from instances of threading._DummyThread to run without going through the queue infrastructure of mtTkinter. That is, I changed:

if threading.currentThread() == self._tk._creationThread:

to

if (threading.currentThread() == self._tk._creationThread) or \
   isinstance(threading.currentThread(), threading._DummyThread) :

Things seem to be working for me now...hope this helps!

2
votes

I've "resolved" not using it. mTkinter seems to be a bit buggy.

0
votes

This is an old topic, but I don't see where it was even closed. I have a python application using 4 threads using the 'theading' module and MtTkinter.

I was having similar problems with MtTkinter. The application worked but would not close. I have searched and tried quite a few solutions, none worked. For my application, using queues would have been a chore.

Here is what I did. Its not elegant, but it worked. Its pretty ruthless.

cleanup():`
    pidx = os.getpid()
    cmd1 = "kill" + " " + str(pidx)
    if __name__ == "__main__":
        os.system(cmd1)