Question related to the thread Multithreading PyQt applications with QThreadPool

Heads up! You've already completed this tutorial.

bharathinmail5419 | 2020-07-19 10:31:58 UTC | #1

Can you explain more how

python
    # Add the callback to our kwargs
    self.kwargs['**progress_callback**'] = self.signals.progress

had binded to the below function

python
    def execute_this_fn(self, progress_callback):
        for n in range(0, 5):
            time.sleep(1)
            progress_callback.emit(n*100/4)

martin | 2020-07-19 10:39:49 UTC | #2

Hey @bharathinmail5419 welcomet to the forum!

This is making use of Python keyword unpacking. This allows you to pass keyword parameters to functions using a dictionary of key=value pairs.

When we call the execute_this_fn we call it with the following ...

Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book

python
execute_this_fn(**self.kwargs)

This unpacks the key=value pairs in the self.kwargs dictionary, as keywords for the function. So for example, if we had the following dictionary

python
my_dict = {
'something': 3,
'another': 6,
}

and then called a function as follows...

python
my_function(**my_dict)

this would be the equivalent of ...

python
my_function(something=3, another=6)

So, going back to the original example. We have a dictionary called kwargs which holds keyword arguments. We add another entry to that dictionary called 'progress_callback' which holds the function we're going to call. That could be anything, but for example say we stored the print function --

1:1 Coaching & Tutoring for your Python GUIs project
Martin Fitzpatrick Python GUIs Coaching & Training
60 mins ($195) Book Now

1:1 Python GUIs Coaching & Training

Comprehensive code reviewBugfixes & improvements • Maintainability advice and architecture improvements • Design and usability assessment • Suggestions and tips to expand your knowledgePackaging and distribution help for Windows, Mac & Linux • Find out more.

python
kwargs = {}
kwargs['progress_callback'] = print   # Store the print function in this dictionary

If we then called our function with...

python
execute_this_fn(**kwargs)

That would be the equivalent of calling

python
execute_this_fn(progress_callback=print)

The value stored in the dictionary under progress_callback is passed as an argument with that keyword to the function.


Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PyQt6 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

Question related to the thread Multithreading PyQt applications with QThreadPool was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular https://www.martinfitzpatrick.com/browse/books/ on the subject.