在页面加载后显示消息框pyqt5

时间:2018-07-08 15:51:43

标签: python pyqt pyqt5 qwizard qwizardpage

我有一个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_())

1 个答案:

答案 0 :(得分: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