У вас должен был появиться файл с рекурсами .qrc
, пусть будет resources.qrc
Теперь нужно его скомпилировать в модуль на питоне через команду в консоли:
pyrcc5 resources.qrc -o resources.py
И добавьте импорт перед импортом from mydesign import Ui_MainWindow
:
import resources
Источник
PS.
Файл pyrcc5
устанавливается вместе с PyQt5
и находится в папке питона \Scripts
. Например:
C:\Users\<имя пользователя>\AppData\Local\Programs\Python\Python310\Scripts
Путь к этой папке в переменных путях PATH
должен быть, тогда pyrcc5
будет доступен по имени. Иначе, нужно или добавить эту папку в PATH
, или запускать, указывая полный путь.
PPS.
Повторил проект из вопроса:
mydesign.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>60</x>
<y>50</y>
<width>221</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>23</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>70</x>
<y>100</y>
<width>561</width>
<height>381</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="resources.qrc">:/newPrefix/input.jpg</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources>
<include location="resources.qrc"/>
</resources>
<connections/>
</ui>
resources.qrc
<RCC>
<qresource prefix="newPrefix">
<file>input.jpg</file>
</qresource>
</RCC>
Выглядит это так:

Далее, в папке проекта в консоли выполнил команды:
Генерация формы:
pyuic5 -x mydesign.ui -o mydesign.py
Компиляция ресурсов:
pyrcc5 resources.qrc -o resources_rc.py
Папка проекта теперь имеет такие файлы (кст, если удалить input.jpg
, то при запуске скрипта картинка будет, т.к. она уже в resources_rc.py
):

mydesign.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mydesign.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 50, 221, 41))
font = QtGui.QFont()
font.setPointSize(23)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(70, 100, 561, 381))
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap(":/newPrefix/input.jpg"))
self.label_2.setScaledContents(True)
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "TextLabel"))
import resources_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Кусочек resources_rc.py

main.py
from PyQt5 import QtWidgets
from mydesign import Ui_MainWindow # импорт файла дизайна
import sys
class mywindow(QtWidgets.QMainWindow):
def __init__(self):
super(mywindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
app = QtWidgets.QApplication([])
application = mywindow()
application.show()
sys.exit(app.exec())
При запуске картинка из ресурсов подтягивается:
