0

I have recently started using multithreading to be able to, for example, supply input during a for loop. This was successfull but I wanted to try more. When I tried threading in combination with Tkinter things went wrong. So for my example I have a very simple piece code (Python 2.7.11). I realize it beats the point of threading but it shows my problem:

import Tkinter
import threading


def func():
    root=Tkinter.Tk()       #Open Tkinter window
    Tkinter.mainloop()
    root.quit()             #Extra quit, although it doesn't seem to help

thread1=threading.Thread(target=func)
thread1.start()

thread1.join()              #Wait for thread to end (for me to close the window)
del thread1                 #Extra deletes (although they don't seem to help)
del func

Running this code once works fine. The Tkinter window opens, I close it manually and the rest works fine. However if I run it again the interpreter freezes. If a second thread, or a simple loose print statement, is added it doesn't even start those. The entire enterpreter freezes and I can only re-run this code by restarting the Kernel. So do I have to quit the thread/Tkinter in a special way to prevent this from happening?

Additional information: My CPU usage goes way up. When running it the 2nd time task manager shows up to 30% CPU usage. So I guess there is an infinite loop going on somewhere. I have read that Tkinter does not take threading well, but I have seen instances where this workes. What I have here is a very simple thing so it must be something I am doing wrong. Hopefully someone can help me, because I couldn't find an answer in whatever way I tried to Google it.

Thanks in advance.

Dennis
  • 23
  • 6
  • `Tkinter` should run only one `Tk()` and `mainloop()` (it is endless loop) - if you run your code in IDLE then you have even three mainloops because IDLE uses `Tkinter` – furas Jan 17 '17 at 21:13
  • If you have to use `threads` (instead of `root.after()`) then run only some function in thread not `mainloop()` – furas Jan 17 '17 at 21:15
  • What does "run it again" mean? Does that mean from a command prompt you are running the script again (ie: starting a new process)? Or, do you mean that calling `func` a second time in the same process causes a problem? – Bryan Oakley Jan 17 '17 at 22:05
  • @BryanOakley Running again means "execute cell". For this case that means just re-running the entire things WITHOUT restarting python/script or anything. The interpreter stops working after executing thread1.start() for the second time. I even double checked after running it the first time with thread1.isAlive() but it returns False so it should be gone. – Dennis Jan 17 '17 at 22:42
  • Here's a really nice and concise description of how tkinter interacts with threads, written by one of the guys who maintains tcl/tk (upon which tkinter is built): http://stackoverflow.com/questions/38767355/callback-to-python-function-from-tkinter-tcl-crashes-in-windows/38767665#38767665 – Bryan Oakley Jan 17 '17 at 22:45

0 Answers0