Reputation: 556
Not wanting my GUI to freeze, I've decided to use a thread for a file operation. Currently I'm using thread.start_new_thread(self.openFile, (filepath, self.openedFile))
to do so, with self.openedFile
being my callback method. Inside self.openFile
it is just invoked using callback(success)
.
But unfortunately I couldn't figure out how to execute my callback in the main thread instead of the newly created one. This is required as the GUI cannot be modified from another thread.
I really appreciate all your help!
Upvotes: 2
Views: 2897
Reputation: 881785
The thread owning the GUI will have to periodically check a Queue.Queue
instance on which other threads can put work requests (e.g a callback function and arguments for it).
How easy or hard it is to insert such checks within a GUI's main loop is 100% dependent on exactly what GUI framework you're using.
For example if you're using Tkinter, the after
method of widgets lets you do such periodic checks, as explained e.g at Run an infinite loop in the backgroung in Tkinter .
Upvotes: 4