首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3程序处于无响应状态

Python3程序处于无响应状态
EN

Stack Overflow用户
提问于 2017-08-29 16:30:53
回答 1查看 168关注 0票数 0

我在同一个文件夹中有两个python文件-- "main.py"和main.py。我正在创建一个应用程序,所以如果我运行mainLauncher.py,那么它将成功运行并打开一个窗口,如下所示。

mainLauncher.py代码

代码语言:javascript
复制
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中删除对mainLauncher.py函数的调用,并从main.py调用它。因此,我将main.py制作为:

代码语言:javascript
复制
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,则该窗口处于未响应状态,我必须从任务管理器中终止该进程,并获得以下输出:

有谁能告诉我为什么会发生这种事,我该如何解决?我想运行main_launch()main.py

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-01 22:03:43

QApplication只能有一个实例,因为它处理主循环,但在您的示例中,您已经创建了2,生成了第一个块和第二个块。为了解决这个问题,我们验证如果一个新实例不存在,只创建一个新实例,因为它将main.py更改为我接下来展示的内容:

main.py

代码语言:javascript
复制
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_())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45944135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档