2017-08-29 31 views
0

我有兩個python文件「main.py」「mainLauncher.py」在同一個文件夾中。 所以如果我運行mainLauncher.py那麼它成功運行並打開一個窗口,如下圖所示,我提出申請。Python3程序進入非響應狀態

By running 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()功能和主要調用它。 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則該窗口會在未響應狀態,我要殺死從任務管理器過程而我得到以下輸出:

By Running main.py

誰能告訴我,爲什麼會發生,如何解決呢?我想從main.py運行main_launch()

+0

我已經測試你的代碼,並刪除了重複UI功能,我認爲你複製它的形式沒有意識到它,它可以正常工作,你可以共享通過驅動器,保管箱或類似的生成錯誤的完整代碼。 – eyllanesc

+0

@eyllanesc確定這是谷歌驅動器鏈接:https://drive.google.com/drive/folders/0B6dxEingt2ARUzFJbWhBQjdINk0?usp=sharing –

+0

你需要Python3和PyQt5模塊運行這段代碼。 –

回答

1

有也只能是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_())