0

我有一个 PyQt5 向导,在第二页上,我希望根据以前的内容改变行为。

我已经实施initializePage

def initializePage(self): 
    if self.altThing:
        self.onloadbtn.show()
        # .. do Something
        QMessageBox.about(self, 'Hello', 'Hello') #Pops up before the page is rendered
        # do other stuff..

我希望页面先加载,然后做一些事情,例如下载文件并显示进度条等,但直到页面加载。

QMessageBox在页面加载之前弹出。

下面是一个MVCE

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class Wizard(QWizard):
    def __init__(self, parent=None):
        super(Wizard, self).__init__(parent)

        self.addPage(EnterToken(self)) 
        self.addPage(ProcessData(self))

        self.buttons = [self.button(t) for t in (QWizard.NextButton, QWizard.FinishButton)]

        for btn in self.buttons:
            btn.installEventFilter(self)

    def eventFilter(self, obj, event):
        if obj in self.buttons and event.type() == QEvent.Show:
            obj.setDefault(False)
        return super(Wizard, self).eventFilter(obj, event)

class EnterToken(QWizardPage):
    def __init__(self, parent=None):
        super(EnterToken, self).__init__(parent)

        self.setTitle("Enter your token here")
        self.setSubTitle(" ")           

        # Enter Token Widgets
        self.label = QLabel()
        self.enter_token_box = QLineEdit()        

        self.btn = QPushButton('OK')

        # layout options
        layout = QVBoxLayout()
        layout.addWidget(self.label)        
        self.label.setText("Enter Your 12 Digit Code.")
        layout.addWidget(self.enter_token_box)
        layout.addWidget(self.btn)

        # Enter Key TRigger
        self.enter_token_box.returnPressed.connect(self._EnterToken)

        self.btn.clicked.connect(self._EnterToken)

        self.setLayout(layout)         

    def _EnterToken(self):
        """ Method for processing user input after the button is pressed"""
        pass


class ProcessData(QWizardPage):
    """ Sensor Code Entry """
    def __init__(self, parent=None):
        super(ProcessData, self).__init__(parent)        

        self.altThing = True

        # num of logs combo box
        self.num_logs_combo = QComboBox(self)

        self.alt = QComboBox(self)


        # ~buttons
        self.btn = QPushButton('OK')
        self.onloadbtn = QPushButton('OK')

        layout = QVBoxLayout()
        layout.addWidget(self.num_logs_combo)
        layout.addWidget(self.alt)
        layout.addWidget(self.btn) 
        layout.addWidget(self.onloadbtn)  

        self.onloadbtn.hide()
        self.alt.hide()

        self.setLayout(layout)  

        self.btn.clicked.connect(self._EnterToken)  


    def initializePage(self): 
        if self.altThing:
            self.onloadbtn.show()
            # .. do Something
            QMessageBox.about(self, 'Hello', 'Hello') #Pops up before the page is rendered
            # do other stuff..

    def _EnterToken(self):
        """ Method for processing user input after the button is pressed"""
        pass

if __name__ == '__main__':
    app = QApplication(sys.argv)
    wizard = Wizard()
    wizard.show()
    sys.exit(app.exec_())
4

1 回答 1

1

initializePage()方法在显示页面之前调用,因此您不应在该部分调用该逻辑,可能的解决方案是使用QTimer.

class ProcessData(QWizardPage):
    """ Sensor Code Entry """
    def __init__(self, parent=None):
        super(ProcessData, self).__init__(parent)        
        self.altThing = True
        # num of logs combo box
        self.num_logs_combo = QComboBox(self)
        self.alt = QComboBox(self)
        # ~buttons
        self.btn = QPushButton('OK')
        self.onloadbtn = QPushButton('OK')

        layout = QVBoxLayout(self)
        layout.addWidget(self.num_logs_combo)
        layout.addWidget(self.alt)
        layout.addWidget(self.btn) 
        layout.addWidget(self.onloadbtn)  

        self.onloadbtn.hide()
        self.alt.hide()
        self.btn.clicked.connect(self._EnterToken) 

    def foo(self):
        if self.altThing:
            # .. do Something
            QMessageBox.about(self, 'Hello', 'Hello') #Pops up before the page is rendered
            # do other stuff..""

    def initializePage(self):
        QTimer.singleShot(0, self.foo)

    def _EnterToken(self):
        """ Method for processing user input after the button is pressed"""
        pass
于 2018-07-09T00:13:39.887 回答