Picture not showing on my loading screen in QLabel

ε祈祈猫儿з 提交于 2021-02-05 11:12:28

问题


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

It should look like this:

However, it looks like this:

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:

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

回答1:


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))


来源:https://stackoverflow.com/questions/65204163/picture-not-showing-on-my-loading-screen-in-qlabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!