2

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!

1 Answer 1

4

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 .

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Do you know the PyQT equivalent to Tkinter's after? I couldn't find it in the documentation.
I believe QTimer is what you'd use, srinikom.github.io/pyside-docs/PySide/QtCore/QTimer.html .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.