問題是,當我在添加資源文件並在它們上添加帶有邊界圖像的PushButton後運行PyQt5 GUI時,mainwindow不會啓動,也沒有錯誤。PyQt5 GUI運行沒有錯誤,但沒有出現窗口
我轉換主窗口UI到Python與以下命令:
pyuic5 mainWindow.iu -o mainWindow.py
因此,這是在python的主窗口。
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>
這是結果試圖運行它的時候。
我想編寫一個例外,看看是否能抓住什麼。
是什麼'resources_rcenter'? – eyllanesc
你在哪裏用QApplication創建應用程序? – eyllanesc
錯寫時提問,但它的我的資源文件。它在文件底部註釋掉,並一直聲明在頂部eyllanesc –