gpt4 book ai didi

python - PyQt5 GUI 运行没有错误,但不出现窗口

转载 作者:太空宇宙 更新时间:2023-11-03 15:01:40 25 4
gpt4 key购买 nike

问题是,当我在添加资源文件并添加带有边框图像的按钮后运行 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>

这是尝试运行时的结果。

ScreenShot

我考虑编写一个异常来看看它是否捕获到任何东西。

最佳答案

问题是你使用的路径不正确,这是你没有添加前缀造成的。

例如,您必须更改:

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

关于python - PyQt5 GUI 运行没有错误,但不出现窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45043904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com