GaTTaCa
GaTTaCa

Reputation: 489

Multithreading with Tkinter

I'm having some issues with a Tkinter-based GUI. Basically the GUI creates lots of threads and run them. When each thread has finished, I'd like it to update a label to inform the user of this specific thread completion.

I know Tkinter widgets are not thread-safe and that it is a bad practice to allow subthreads to update the view. So I'm trying to trigger an event on the main thread so it can update the view itself.

I'm running the simplified code sample below:

from Tkinter import *
from threading import *

def myClass(root):

   def __init__(self, root):
      self.root = root
      # Other stuff populating the GUI

   # Other methods creating new 'threading.Thread'
   # objects which will call 'trigger_Event'

   # Called by child threads 
   def trigger_Event(self):
      self.root.event_generate("<<myEvent>>", when="tail")

   # Called by main thread
   def processEvent(self):
      # Update GUI label here

if __name__ == '__main__':
   root = Tk()
   root.geometry("500x655+300+300")
   app = myClass(root)
   root.bind("<<myEvent>>", app.processEvent())
   root.mainloop() 

Unfortunately, this does not work: processEvent is never called. What am I missing ?

Upvotes: 0

Views: 454

Answers (1)

Kevin
Kevin

Reputation: 76254

root.bind("<<myEvent>>", app.processEvent())

Here, you're binding myEvent to the return value of app.processEvent, because you're calling the function rather than just referring to it. Try removing the parentheses.

root.bind("<<myEvent>>", app.processEvent)

Upvotes: 2

Related Questions