Image inserted in label using designer does not load from python

Heads up! You've already completed this tutorial.

Alexis_Labuschagne | 2020-09-02 13:02:57 UTC | #1

In PyQT5 designer I insert an image into a label but the image does not show when I run it in python. It shows in designer : image|353x268 Anything I need to do in addition? Here image is called in GUI when I write out the Python code: image|473x51


martin | 2020-09-04 19:18:52 UTC | #2

Looking at the code in the second block, the image is referenced using a QResource path -- starting with the colon :. This is a virtual path referencing a file in the Qt Resource system, used by Qt to bundle assets. For those paths to work in Python you need to first compile the resource file to Python code and then import it into your application.

You can compile it using the pyrcc5 command (change the name of your file as needed).

python
pyrcc5 resources.qrc -o resources.py

This compile step goes through all the files referenced in the .qrc file, copying their data (and resource name) into a Python data structure. When you import the resulting resources.py file, these files become available to Qt under theit resource paths (beginning with :).

There is covered with some more examples in the book's QResource chapter (under Designer).


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

Image inserted in label using designer does not load from python 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.