Image does not display on Pyqt [duplicate]

2024/9/22 8:32:55

I am using Pyqt5, python3.9, and windows 11. I am trying to add an image to my app but it wont display anything as show below.

from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_Dialog(object):def setupUi(self, Dialog):Dialog.setObjectName("Dialog")Dialog.resize(531, 316)self.label = QtWidgets.QLabel(Dialog)self.label.setGeometry(QtCore.QRect(70, 30, 491, 241))self.label.setText("")self.label.setPixmap(QtGui.QPixmap(":/newPrefix/download.png"))self.label.setObjectName("label")self.retranslateUi(Dialog)QtCore.QMetaObject.connectSlotsByName(Dialog)def retranslateUi(self, Dialog):_translate = QtCore.QCoreApplication.translateDialog.setWindowTitle(_translate("Dialog", "Dialog"))if __name__ == "__main__":import sysapp = QtWidgets.QApplication(sys.argv)Dialog = QtWidgets.QDialog()ui = Ui_Dialog()ui.setupUi(Dialog)Dialog.show()sys.exit(app.exec_())][1]

My Application:

My Application

This is my qrc file:

<RCC><qresource prefix="newPrefix"><file>download.png</file><file>background.gif</file></qresource>
</RCC>

And both the image and the main.py are in the same directory. Any idea why this does not work?

EDIT: The code seems to work on windows 10 but not on windows 11.

Answer

you should first use pyrcc5 and convert your qrc to .py :

for example, I do this :

pyrcc5 resourse.qrc -o logo_rc.py

then you should import it :

from PyQt5 import QtCore, QtGui, QtWidgetsimport logo_rcclass Ui_Dialog(object):def setupUi(self, Dialog):Dialog.setObjectName("Dialog")Dialog.resize(550, 600)self.label = QtWidgets.QLabel(Dialog)self.label.setGeometry(QtCore.QRect(0, 0, 550, 600))self.label.setText("")self.label.setPixmap(QtGui.QPixmap(":/images/images/photos.png"))self.label.setObjectName("label")self.retranslateUi(Dialog)QtCore.QMetaObject.connectSlotsByName(Dialog)def retranslateUi(self, Dialog):_translate = QtCore.QCoreApplication.translateDialog.setWindowTitle(_translate("Dialog", "Dialog"))if __name__ == "__main__":import sysapp = QtWidgets.QApplication(sys.argv)Dialog = QtWidgets.QDialog()ui = Ui_Dialog()ui.setupUi(Dialog)Dialog.show()sys.exit(app.exec_())

this is my out put :

enter image description here

https://en.xdnf.cn/q/119153.html

Related Q&A

Expand the following dictionary into following list

how to generate the following list from the following dictionary d = {2: 4, 3: 1, 5: 3}f = [2**1,2**2, 2**3, 2**4, 3**1, 5**1, 5**2, 5**3, 2**1 * 3, 2**2 * 3, 2**3 * 3, 2**4 * 3, 5**1 * 3, 5**2 * 3, 5*…

how can I improve the accuracy rate of the below trained model using CNN

I have trained a model using python detect the colors of the gemstone and have built a CNN.Herewith Iam attaching the code of mine.(Referred https://www.kaggle.com) import os import matplotlib.pyplot a…

Classes and methods, with lists in Python

I have two classes, called "Pussa" and "Cat". The Pussa has an int atribute idPussa, and the Cat class has two atributes, a list of "Pussa" and an int catNum. Every class …

polars dataframe TypeError: must be real number, not str

so bascially i changed panda.frame to polars.frame for better speed in yolov5 but when i run the code, it works fine till some point (i dont exactly know when error occurs) and it gives me TypeError: m…

Get a string in Shell/Python using sys.argv

Im beginning with bash and Im executing a script :$ ./readtext.sh ./InputFiles/applications.txt Here is my readtext.sh code :#!/bin/bash filename="$1" counter=1 while IFS=: true; doline=read …

Need to combine two functions into one (Python)

Here is my code-def Max(lst):if len(lst) == 1:return lst[0]else:m = Max(lst[1:])if m > lst[0]: return melse:return lst[0] def Min(lst):if len(lst) == 1:return lst[0]else:m = Min(lst[1:])if m < ls…

Error: descriptor blit requires a pygame.Surface object but received a NoneType [duplicate]

This question already has answers here:How can I draw images and sprites in Pygame?(4 answers)Closed 2 years ago.I am creating a game. I wrote this code to create a sprite and its hitbox:hg = pygame.i…

How can I deal with overlapping rectangles? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

panda df not showing all rows after loading from MS SQL

Im using Pandas with latest sqlalchemy (1.4.36) to query a MS SQL DB, using the following Python 3.10.3 [Win] snippet: import pandas as pd # from sqlalchemy…

How to extract multiple grandchildren/children from XML where one child is a specific value?

Im working with an XML file that stores all "versions" of the chatbot we create. Currently we have 18 versions and I only care about the most recent one. Im trying to find a way to extract a…