0

I'm trying to load a logo into a python scripts UI. I'm using Qt Designer and I created a label and set pixmap to the image. The image loads fine in the designer but when I import the ui file into the python script I get this error message

C:\Users\Mason\AppData\Local\Continuum\anaconda3\python.exe "C:/Users/Mason/PycharmProjects/Inspector/Tester/main.py"
Traceback (most recent call last):
  File "C:/Users/Mason/PycharmProjects/Inspector/Tester/main.py", line 9, in <module>
    UIClass, QtBaseClass = uic.loadUiType("ui5.ui")
  File "C:\Users\Mason\AppData\Local\Continuum\anaconda3\lib\site-packages\PyQt5\uic\__init__.py", line 201, in loadUiType
    exec(code_string.getvalue(), ui_globals)
  File "<string>", line 30
    import 3_rc

I still get that error even if I take the image out of the ui file and reload it. What am I doing wrong?

from PyQt5 import QtCore, uic, QtWidgets
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
from PyQt5.QtGui import QIcon, QPixmap
import sys


UIClass, QtBaseClass = uic.loadUiType("ui5.ui")

class MyApp(UIClass, QtBaseClass):
    def __init__(self):
        UIClass.__init__(self)
        QtBaseClass.__init__(self)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.on_pushbutton_click)



if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())
Mason White
  • 95
  • 2
  • 10

1 Answers1

0

Qt relies on a resource system for images and other assets. When you add an image in qt designer, it creates a resource file. You have to convert that resource file, just like with a ui file (uic is a shortcut to this process). The autogenerated ui file you get with uic is looking for that resource file, but can't find it.

You can convert your resource file to python with this, depending on your qt version: pyrcc5 -o 3_rc.py your_resource_file_here.qrc

More info:

NateTheGrate
  • 590
  • 3
  • 11
  • Yep this worked. Thanks for the tip. Just an fyi for anyone else reading this if you're using PyQT5 you will have to remove a line called import 3_rc from the code once you convert it from a ui to a .py. and you will have to change to to just the file path as well. – Mason White May 03 '19 at 18:34