Search code examples
pythonpyqtqt-designer

Picture not showing on my loading screen in QLabel


I'm currently using QT designer to show a picture on my loading screen.

It should look like this: enter image description here

However, it looks like this: enter image description here

This is because for some reason its not showing my picture, when it registers in my IDE that the filepath is correct as seen here: enter image description here

The only time the picture actually shows in my loading GUI is when I use the FULL file path which is: C:\Users\myalt\OneDrive\Desktop\GUINEW\assets\PostmonkeyLogo.png

But of course, this is not viable when this software will be used on many different computer with different file paths.

self.label.setPixmap(QPixmap(u"assets/PostmonkeyLogo.png")) ## image file path to show

Solution

  • The problem is that the file path is relative to where the console was opened and the python.exe command is executed. It is better to build the full path using the information as the path of the .py:

    import os.path
    
    # ...
    
    CURRENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
    filename = os.path.join(CURRENT_DIRECTORY, "assets/PostmonkeyLogo.png")
    self.label.setPixmap(QPixmap(filename))