Skip to content
Advertisement

Passing a function to a Worker (QObject) class in Python GUI application to prevent freezing/blocking

I am trying to find a way to successfully pass a function to a Worker class in Python using PyQT5. Instead of using the pre-defined run function (or Long-running task) in the sample Worker class code, I would like to be able to pass a custom function to the worker class. Below I’ve pasted the sample code I’m working with, followed by an adjustment I’ve tried.

JavaScript

JavaScript

Advertisement

Answer

As one of solutions, you can pass a function and an argument to the Worker’s __init__ method, like:

JavaScript

So you can pass a method and an argument when creating a worker and connect the method to handle the result:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement