为什么执行Python QT5代码时不显示图标和图像?

Gau*_*tez 1 qt-designer python-3.x pyqt5

环境:

Python 3.7.7

Qt5

Qt 设计器 5.11

问题:

我使用 Qt Designer 设计了一个 GUI,并在按钮中的标签和图标背景中添加了一些图像。我可以从 Qt 设计器软件中正确看到图像和图标。但是当我执行 python 脚本来加载 UI 时,我再也看不到图像了。

代码:

我的主要 python 脚本位于根文件夹中,ui、qrc 和图像文件位于文件夹“ui”中:

main.py
ui/config.qrc
ui/doc.qrc
ui/document.png
ui/gear.png
ui/logo.qrc
ui/logo_MyUI_300.jpg
ui/MyUI.ui
ui/play-button.png
ui/play.png
ui/question.png
ui/quit.png
ui/quit.qrc
ui/report.qrc
ui/run.qrc
ui/save.png
ui/save.qrc
ui/scan.qrc
ui/search.png
ui/seo-report.png
ui/support.qrc
ui/telegram.png
ui/telegram.qrc
ui/watch_videos.qrc
Run Code Online (Sandbox Code Playgroud)

主要.py:

from PyQt5 import QtWidgets, uic

gui = QtWidgets.QApplication([])
ui=uic.loadUi('ui/MyUI.ui')

ui.show()
gui.exec()
Run Code Online (Sandbox Code Playgroud)

我的用户界面:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <author>MyUI.co</author>
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>580</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MyUI.co</string>
  </property>
  <property name="windowIcon">
   <iconset>
    <normalon>:/logo/logo_MyUI_300.jpg</normalon>
   </iconset>
  </property>
  <property name="styleSheet">
   <string notr="true">background-color: rgb(255, 255, 255);</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLineEdit" name="lineEdit_Email">
    <property name="geometry">
     <rect>
      <x>126</x>
      <y>190</y>
      <width>221</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="placeholderText">
     <string>i.e: johndoe@gmail.com</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_License">
    <property name="geometry">
     <rect>
      <x>126</x>
      <y>220</y>
      <width>221</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="placeholderText">
     <string>i.e: gold89DE-B4JI-HQ3E-D8UT</string>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>76</x>
      <y>190</y>
      <width>41</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Email</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>10</y>
      <width>75</width>
      <height>75</height>
     </rect>
    </property>
    <property name="autoFillBackground">
     <bool>true</bool>
    </property>
    <property name="styleSheet">
     <string notr="true">background-image: url(:/logo/logo_MyUI_300.jpg);</string>
    </property>
    <property name="text">
     <string/>
    </property>
    <property name="textFormat">
     <enum>Qt::RichText</enum>
    </property>
    <property name="pixmap">
     <pixmap resource="logo.qrc">:/logo/logo_MyUI_300.jpg</pixmap>
    </property>
    <property name="openExternalLinks">
     <bool>false</bool>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>36</x>
      <y>220</y>
      <width>81</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>License Key</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_1">
    <property name="geometry">
     <rect>
      <x>36</x>
      <y>140</y>
      <width>31</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>1.</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_1_Title">
    <property name="geometry">
     <rect>
      <x>66</x>
      <y>146</y>
      <width>231</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>16</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Activate your MyUI</string>
    </property>
   </widget>
   <widget class="QPushButton" name="button_SaveLicense">
    <property name="geometry">
     <rect>
      <x>256</x>
      <y>260</y>
      <width>91</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>16</pointsize>
     </font>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color: rgb(199, 199 199);</string>
    </property>
    <property name="text">
     <string>Save</string>
    </property>
    <property name="icon">
     <iconset>
      <normalon>:/save/save.png</normalon>
     </iconset>
    </property>
   </widget>
   <widget class="QLabel" name="label_5">
    <property name="geometry">
     <rect>
      <x>36</x>
      <y>304</y>
      <width>31</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>2.</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2_Title">
    <property name="geometry">
     <rect>
      <x>66</x>
      <y>310</y>
      <width>331</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>16</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Required Softwares are Installed?</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_8">
    <property name="geometry">
     <rect>
      <x>86</x>
      <y>350</y>
      <width>101</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Java Runtime:</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_9">
    <property name="geometry">
     <rect>
      <x>186</x>
      <y>350</y>
      <width>41</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>No</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_10">
    <property name="geometry">
     <rect>
      <x>186</x>
      <y>370</y>
      <width>41</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>No</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_11">
    <property name="geometry">
     <rect>
      <x>126</x>
      <y>370</y>
      <width>61</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>NodeJS:</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_12">
    <property name="geometry">
     <rect>
      <x>186</x>
      <y>390</y>
      <width>41</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>No</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_13">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>390</y>
      <width>91</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>SDKManager:</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_14">
    <property name="geometry">
     <rect>
      <x>186</x>
      <y>410</y>
      <width>41</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>No</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_15">
    <property name="geometry">
     <rect>
      <x>106</x>
      <y>410</y>
      <width>81</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Build-Tools:</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_16">
    <property name="geometry">
     <rect>
      <x>186</x>
      <y>430</y>
      <width>41</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>No</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_17">
    <property name="geometry">
     <rect>
      <x>126</x>
      <y>430</y>
      <width>61</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Appium:</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_18">
    <property name="geometry">
     <rect>
      <x>186</x>
      <y>450</y>
      <width>41</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>No</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_19">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>450</y>
      <width>71</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>11</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Tesseract:</string>
    </property>
   </widget>
   <widget class="QPushButton" name="button_Run">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>20</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>10</pointsize>
     </font>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color: rgb(85, 255, 127);</string>
    </property>
    <property name="text">
     <string>Run</string>
    </property>
    <property name="icon">
     <iconset>
      <normalon>:/run/play-button.png</normalon>
     </iconset>
    </property>
   </widget>
   <widget class="QPushButton" name="button_Quit">
    <property name="geometry">
     <rect>
      <x>180</x>
      <y>20</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>10</pointsize>
     </font>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color: rgb(255, 112, 93);</string>
    </property>
    <property name="text">
     <string>Quit</string>
    </property>
    <property name="icon">
     <iconset>
      <normalon>:/quit/quit.png</normalon>
     </iconset>
    </property>
   </widget>
   <widget class="QPushButton" name="button_Report">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>20</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>10</pointsize>
     </font>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color: rgb(199, 199, 199);</string>
    </property>
    <property name="text">
     <string>See Report</string>
    </property>
    <property name="icon">
     <iconset>
      <normalon>:/report/seo-report.png</normalon>
     </iconset>
    </property>
   </widget>
   <widget class="QPushButton" name="button_Doc">
    <property name="geometry">
     <rect>
      <x>390</x>
      <y>20</y>
      <width>121</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>10</pointsize>
     </font>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color: rgb(199, 199, 199);</string>
    </property>
    <property name="text">
     <string>Read the Doc</string>
    </property>
    <property name="icon">
     <iconset>
      <normalon>:/doc/document.png</normalon>
     </iconset>
    </property>
   </widget>
   <widget class="QPushButton" name="button_Support">
    <property name="geometry">
     <rect>
      <x>650</x>
      <y>20</y>
      <width>131</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>10</pointsize>
     </font>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color: rgb(199, 199, 199);</string>
    </property>
    <property name="text">
     <string>Contact Support</string>
    </property>
    <property name="icon">
     <iconset>
      <normalon>:/support/question.png</normalon>
     </iconset>
    </property>
   </widget>
   <widget class="QLabel" name="label_4">
    <property name="geometry">
     <rect>
      <x>430</x>
      <y>140</y>
      <width>31</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>3.</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3_Title">
    <property name="geometry">
     <rect>
      <x>460</x>
      <y>146</y>
      <width>331</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>16</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Connect your Smartphone(s)</string>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="plainTextEdit_Smartphones">
    <property name="geometry">
     <rect>
      <x>440</x>
      <y>175</y>
      <width>311</width>
      <height>171</height>
     </rect>
    </property>
    <property name="plainText">
     <string>No smartphones were detected... Please verify you enable USB debug mode and you plug correctly your smartphone(s) to your computer through USB, and press the &qu

eyl*_*esc 7

uic.loadUi 函数与 Qt Designate 用于显示预览的方法不同,uic.loadUi 动态生成 python 允许的类和属性,因此它需要您使用 Pyrcc5 编译 rc 并将它们导入到脚本中,例如例子:

pyrcc5 ui/logo.qrc -o logo_rc.py
Run Code Online (Sandbox Code Playgroud)
from PyQt5 import QtWidgets, uic

import logo_rc

gui = QtWidgets.QApplication([])
ui=uic.loadUi('ui/MyUI.ui')

ui.show()
gui.exec()
Run Code Online (Sandbox Code Playgroud)