问题是当我在添加资源文件并在其上添加带边框图像的PushButtons后运行PyQt5 GUI时,主窗口不会启动也不会出错。
我使用以下命令将主窗口UI转换为Python:
pyuic5 mainWindow.iu -o mainWindow.py
所以这是python中的mainWindow。
from PyQt5 import QtCore, QtGui, QtWidgets
import resources_rc
#import sys
#import random
#import matplotlib
#matplotlib.use("Qt5Agg")
#from PyQt5 import QtCore
#from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget
#from numpy import arange, sin, pi
#from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
#from matplotlib.figure import Figure
#from matplotlib import pyplot as plt
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1108, 672)
MainWindow.setAutoFillBackground(False)
MainWindow.setStyleSheet("background-color: rgb(211, 211, 211);")
MainWindow.setIconSize(QtCore.QSize(40, 40))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalScrollBar = QtWidgets.QScrollBar(self.centralwidget)
self.verticalScrollBar.setGeometry(QtCore.QRect(1050, 0, 21, 611))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.MinimumExpanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.verticalScrollBar.sizePolicy().hasHeightForWidth())
self.verticalScrollBar.setSizePolicy(sizePolicy)
self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical)
self.verticalScrollBar.setObjectName("verticalScrollBar")
self.horizontalScrollBar = QtWidgets.QScrollBar(self.centralwidget)
self.horizontalScrollBar.setGeometry(QtCore.QRect(0, 610, 1051, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.horizontalScrollBar.sizePolicy().hasHeightForWidth())
self.horizontalScrollBar.setSizePolicy(sizePolicy)
self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
self.horizontalScrollBar.setObjectName("horizontalScrollBar")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(900, -30, 151, 141))
self.label.setStyleSheet("border-image: url(:/img/NM_State_University_logo.png);")
self.label.setText("")
self.label.setObjectName("label")
self.house1 = QtWidgets.QPushButton(self.centralwidget)
self.house1.setGeometry(QtCore.QRect(31, 31, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house1.sizePolicy().hasHeightForWidth())
self.house1.setSizePolicy(sizePolicy)
self.house1.setAutoFillBackground(False)
self.house1.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house1.setText("")
self.house1.setObjectName("house1")
self.house2 = QtWidgets.QPushButton(self.centralwidget)
self.house2.setGeometry(QtCore.QRect(258, 31, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house2.sizePolicy().hasHeightForWidth())
self.house2.setSizePolicy(sizePolicy)
self.house2.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house2.setText("")
self.house2.setObjectName("house2")
self.house3 = QtWidgets.QPushButton(self.centralwidget)
self.house3.setGeometry(QtCore.QRect(484, 31, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house3.sizePolicy().hasHeightForWidth())
self.house3.setSizePolicy(sizePolicy)
self.house3.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house3.setText("")
self.house3.setObjectName("house3")
self.house4 = QtWidgets.QPushButton(self.centralwidget)
self.house4.setGeometry(QtCore.QRect(711, 31, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house4.sizePolicy().hasHeightForWidth())
self.house4.setSizePolicy(sizePolicy)
self.house4.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house4.setText("")
self.house4.setIconSize(QtCore.QSize(40, 40))
self.house4.setObjectName("house4")
self.house6 = QtWidgets.QPushButton(self.centralwidget)
self.house6.setGeometry(QtCore.QRect(258, 200, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house6.sizePolicy().hasHeightForWidth())
self.house6.setSizePolicy(sizePolicy)
self.house6.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house6.setText("")
self.house6.setObjectName("house6")
self.house5 = QtWidgets.QPushButton(self.centralwidget)
self.house5.setGeometry(QtCore.QRect(31, 200, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house5.sizePolicy().hasHeightForWidth())
self.house5.setSizePolicy(sizePolicy)
self.house5.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house5.setText("")
self.house5.setObjectName("house5")
self.house9 = QtWidgets.QPushButton(self.centralwidget)
self.house9.setGeometry(QtCore.QRect(31, 368, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house9.sizePolicy().hasHeightForWidth())
self.house9.setSizePolicy(sizePolicy)
self.house9.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house9.setText("")
self.house9.setObjectName("house9")
self.house7 = QtWidgets.QPushButton(self.centralwidget)
self.house7.setGeometry(QtCore.QRect(484, 200, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house7.sizePolicy().hasHeightForWidth())
self.house7.setSizePolicy(sizePolicy)
self.house7.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house7.setText("")
self.house7.setObjectName("house7")
self.house8 = QtWidgets.QPushButton(self.centralwidget)
self.house8.setGeometry(QtCore.QRect(711, 200, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house8.sizePolicy().hasHeightForWidth())
self.house8.setSizePolicy(sizePolicy)
self.house8.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house8.setText("")
self.house8.setIconSize(QtCore.QSize(70, 20))
self.house8.setAutoDefault(True)
self.house8.setObjectName("house8")
self.house11 = QtWidgets.QPushButton(self.centralwidget)
self.house11.setGeometry(QtCore.QRect(484, 368, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house11.sizePolicy().hasHeightForWidth())
self.house11.setSizePolicy(sizePolicy)
self.house11.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house11.setText("")
self.house11.setObjectName("house11")
self.house10 = QtWidgets.QPushButton(self.centralwidget)
self.house10.setGeometry(QtCore.QRect(258, 368, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house10.sizePolicy().hasHeightForWidth())
self.house10.setSizePolicy(sizePolicy)
self.house10.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house10.setText("")
self.house10.setObjectName("house10")
self.house12 = QtWidgets.QPushButton(self.centralwidget)
self.house12.setGeometry(QtCore.QRect(711, 368, 32, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house12.sizePolicy().hasHeightForWidth())
self.house12.setSizePolicy(sizePolicy)
self.house12.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house12.setText("")
self.house12.setObjectName("house12")
#####################################Power Plot House 1
self.PowPlotHouse1 = QtWidgets.QWidget(self.centralwidget)
self.PowPlotHouse1.setGeometry(QtCore.QRect(70, 30, 171, 121))
self.PowPlotHouse1.setObjectName("PowPlotHouse1")
#####################END of widge
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1108, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
self.menuOptions = QtWidgets.QMenu(self.menubar)
self.menuOptions.setObjectName("menuOptions")
self.menuAdmin = QtWidgets.QMenu(self.menubar)
self.menuAdmin.setObjectName("menuAdmin")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionSave = QtWidgets.QAction(MainWindow)
self.actionSave.setObjectName("actionSave")
self.actionSave_As = QtWidgets.QAction(MainWindow)
self.actionSave_As.setObjectName("actionSave_As")
self.menuFile.addAction(self.actionSave)
self.menuFile.addAction(self.actionSave_As)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.menubar.addAction(self.menuOptions.menuAction())
self.menubar.addAction(self.menuAdmin.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuEdit.setTitle(_translate("MainWindow", "Edit"))
self.menuOptions.setTitle(_translate("MainWindow", "Options"))
self.menuAdmin.setTitle(_translate("MainWindow", "Admin"))
self.actionSave.setText(_translate("MainWindow", "Save"))
self.actionSave_As.setText(_translate("MainWindow", "Save As"))
import resources_rcenter
我通过以下方式转换了我的资源文件:
pyrcc5 resources.qrc -o resources_rc.py
我的资源文件内容
<RCC>
<qresource prefix="newPrefix">
<file>img/Crystal_Clear_app_kfm_home.png</file>
<file>img/Gohomenew.png</file>
<file>img/NM_State_University_logo.png</file>
<file>img/NMlogo_1colorstate_black.png</file>
<file>img/NMSU.png</file>
<file>img/Search_in_home.png</file>
</qresource>
</RCC>
这是尝试运行它时的结果。
我考虑编写一个例外,看看它是否能捕捉到任何东西。
答案 0 :(得分:0)
更正错误的resources_rc并且不要忘记创建main.py文件来调用MainWindow.py。
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from MainWindow import *
import sys
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
def main():
app = QApplication(sys.argv)
instance = Main()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
然后运行main.py
答案 1 :(得分:0)
问题是您使用的路径不正确,这是由您未添加的前缀引起的。
例如,您必须更改:
self.label.setStyleSheet("border-image: url(:/img/NM_State_University_logo.png);")
为:
self.label.setStyleSheet("border-image: url(:/newPrefix/img/NM_State_University_logo.png);")
但上述内容可能很乏味,更好的方法是将前缀更改为"/"
,在这种情况下,它应如下所示:
<RCC>
<qresource prefix="/">
<file>img/Crystal_Clear_app_kfm_home.png</file>
<file>img/Gohomenew.png</file>
<file>img/NM_State_University_logo.png</file>
<file>img/NMlogo_1colorstate_black.png</file>
<file>img/NMSU.png</file>
<file>img/Search_in_home.png</file>
</qresource>
</RCC>
然后用pyrcc5 resources.qrc -o resources_rc.py
重新编译。
另一个问题是,不能在pyqt中启动应用程序,您必须创建应用程序,为此,我建议将以下内容添加到您的文件mainWindow.py中:
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(w)
w.show()
sys.exit(app.exec_())