2017-07-11 206 views
0

問題是,當我在添加資源文件並在它們上添加帶有邊界圖像的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> 

這是結果試圖運行它的時候。

ScreenShot

我想編寫一個例外,看看是否能抓住什麼。

+0

是什麼'resources_rcenter'? – eyllanesc

+1

你在哪裏用QApplication創建應用程序? – eyllanesc

+0

錯寫時提問,但它的我的資源文件。它在文件底部註釋掉,並一直聲明在頂部eyllanesc –

回答

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_()) 
+0

eyllanesc,Your答案工作!非常感謝你。 –

+0

如果我的回答可以幫助您將其標記爲正確。 – eyllanesc

+0

@LuisMartinez你沒有說我的回答會有幫助。 – eyllanesc

0

更正mistype 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