代码之家  ›  专栏  ›  技术社区  ›  johnashu

页面加载后显示MessageBox Pyqt5

  •  0
  • johnashu  · 技术社区  · 6 年前

    我有一个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 回复  |  直到 6 年前
        1
  •  1
  •   eyllanesc Yonghwan Shin    6 年前

    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