我在同一个文件夹中有两个python文件“main.py”和“mainLauncher.py”。 我正在创建一个应用程序,所以如果我运行 mainLauncher.py ,那么它正在成功运行并打开一个窗口,如下所示。
mainLauncher.py 的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainLauncher(object):
def main_launcher_ui(self, WizardPage):
WizardPage.setObjectName("WizardPage")
WizardPage.resize(626, 284)
WizardPage.setStyleSheet("background-color:#e0e0e0;\n"
"border:none;")
self.textBrowser = QtWidgets.QTextBrowser(WizardPage)
self.textBrowser.setGeometry(QtCore.QRect(10, 20, 601, 51))
self.textBrowser.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.IBeamCursor))
self.textBrowser.setStyleSheet("border:none;\n"
"color:#424242 ;\n"
"font-family:futura light;\n"
"")
self.textBrowser.setFrameShape(QtWidgets.QFrame.WinPanel)
self.textBrowser.setObjectName("textBrowser")
self.next = QtWidgets.QPushButton(WizardPage)
self.next.setGeometry(QtCore.QRect(390, 240, 84, 28))
self.next.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.next.setStyleSheet("border:1px solid #ccc;\n"
"background: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ccc, stop: 1 #fff);")
self.next.setObjectName("next")
self.quit = QtWidgets.QPushButton(WizardPage)
self.quit.setGeometry(QtCore.QRect(490, 240, 84, 28))
self.quit.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.quit.setStyleSheet("border:1px solid #ccc;\n"
"background: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ccc, stop: 1 #fff);")
self.quit.setObjectName("quit")
self.retranslateUi(WizardPage
QtCore.QMetaObject.connectSlotsByName(WizardPage)
def retranslateUi(self, WizardPage):
_translate = QtCore.QCoreApplication.translate
WizardPage.setWindowTitle(_translate("WizardPage", "WizardPage"))
self.textBrowser.setHtml(_translate("WizardPage",
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'futura 53\'; font-size:11pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'Cantarell\'; font-size:24pt; font-weight:600;\">Welcome to GDrive</span></p></body></html>"))
self.next.setText(_translate("WizardPage", "Next"))
self.quit.setText(_translate("WizardPage", "Quit"))
def retranslateUi(self, WizardPage):
_translate = QtCore.QCoreApplication.translate
WizardPage.setWindowTitle(_translate("WizardPage", "WizardPage"))
self.textBrowser.setHtml(_translate("WizardPage",
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'futura 53\'; font-size:11pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'Cantarell\'; font-size:24pt; font-weight:600;\">Welcome to GDrive</span></p></body></html>"))
self.next.setText(_translate("WizardPage", "Next"))
self.quit.setText(_translate("WizardPage", "Quit"))
def main_launch():
import sys
app = QtWidgets.QApplication(sys.argv)
WizardPage = QtWidgets.QWizardPage()
ui = Ui_MainLauncher()
ui.main_launcher_ui(WizardPage)
WizardPage.show()
sys.exit(app.exec_())
main_launch()
现在,如果我从 mainLauncher.py 中移除 main_launch()功能,并从 main.py 调用它。所以我将 main.py 改为:
import sys
from PyQt5 import QtWidgets
from mainLauncher import main_launch
app = QtWidgets.QApplication(sys.argv)
main_launch() # calling of main_launch() which is in mainLaunch.py
sys.exit(app.exec_())
但如果我正在运行 main.py ,那么该窗口将进入 Not Responding 状态,我必须从任务管理器我得到以下输出:
任何人都可以告诉我为什么会这样,我该如何解决?我想从 main.py 运行 main_launch()。
答案 0 :(得分:0)
只有一个QApplication
实例,因为它处理主循环,但在你的情况下,你创建了2,第二个生成第一个块。为了解决这个问题,我们验证只有一个新实例不存在才会被创建,因为它会将main.py更改为我接下来要显示的内容:
<强> main.py 强>
import sys
from PyQt5 import QtWidgets
from mainLauncher import main_launch
if QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
main_launch()
sys.exit(app.exec_())