1. import threading
  2.  
  3. import contexts
  4.  
  5. class TkContext(contexts.CallableContext):
  6.     def __init__(self, app):
  7.         self.app = app
  8.         self._post_signal = threading.Condition()
  9.  
  10.     @staticmethod
  11.     def invoke(callable, args, kwargs):
  12.         callable(*args, **kwargs)
  13.  
  14.     @staticmethod
  15.     def invoke_and_notify(signal, callable, args, kwargs):
  16.         with signal:
  17.             callable(*args, **kwargs)
  18.             signal.notify_all()
  19.  
  20.     def post(self, callable, *args, **kwargs):
  21.         '''Adds a callable to invoke within this context.'''
  22.         self.app.after(0, TkContext.invoke, callable, args, kwargs)
  23.  
  24.     def submit(self, callable, *args, **kwargs):
  25.         '''Adds a callable to invoke within this context and returns when it
  26.        has been completed.
  27.        '''
  28.         with self._post_signal:
  29.             self.app.after(0, TkContext.invoke_and_notify, self._post_signal, callable, args, kwargs)
  30.             self._post_signal.wait()