图片未显示在 QLabel 的加载屏幕上 [英] Picture not showing on my loading screen in QLabel

查看:21
本文介绍了图片未显示在 QLabel 的加载屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 QT 设计器在我的加载屏幕上显示图片.

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

它应该是这样的:

然而,它看起来像这样:

However, it looks like this:

这是因为由于某种原因它没有显示我的图片,当它在我的 IDE 中注册时,文件路径是正确的,如下所示:

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:

图片在我的加载 GUI 中实际显示的唯一时间是当我使用完整文件路径时:C:\Users\myalt\OneDrive\Desktop\GUINEW\assets\PostmonkeyLogo.png

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

推荐答案

问题是文件路径是相对于打开控制台和执行 python.exe 命令的位置.最好使用该信息作为 .py 的路径构建完整路径:

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

这篇关于图片未显示在 QLabel 的加载屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆