Understanding Tkinter window responsiveness when running a background thread
<p>I am using Tkinter to create a GUI that is continually updated from a computation thread. In order to keep the GUI responsive, I create an event in each loop of this thread that tells the main thread (in which the Tkinter window was created) to update the GUI with the computed data. The structure of the thread looks like this:</p>

<pre><code>class background_thread(threading.Thread):
    def __init__(self, parent, stopper):
        threading.Thread.__init__(self)

        self.parent  = parent
        self.stopper = stopper

        ### Init. variables ###

    def run(self):
        while(not self.stopper.is_set()):

            ### Compute data ###

            root.event_generate("&lt;&lt;update_gui&gt;&gt;", when = "now")
</code></pre>

<p>This works nicely, but I'm having trouble understanding the behavior of my Tkinter window when the thread is running. First, even though my <code>mainloop()</code> is still responsive (I get approximately 40 frames per second), the window itself is not responsive at all. By this, I mean that it's not really possible to drag it nor to close it. What is the reason for this and is there any way to prevent it?</p>

<p>One of the problems arising from this is that I have to stop the thread before closing the window. For this, I use a button that toggles its execution:</p>

<pre><code>def callback_toggle_thread(self, event):    
    if(not self.background_thread.isAlive()):
        self.background_thread.start()
    else:
        self.stopper.set()
</code></pre>

<p>But if I try to redefine the window close button protocole:</p>

<pre><code>root.protocol("WM_DELETE_WINDOW", on_closing)

def on_closing():
    if(self.background_thread.isAlive()):
        self.stopper.set()                  # Asks the thread to stop
        self.background_thread.join()       # Waits for the thread to stop

    root.destroy()                          # Closes the window
</code></pre>

<p>the program just freezes. Am I doing anything wrong?</p>