pyqt:单击QwizardPage的NextButton时如何发出信号

时间:2015-06-25 08:05:35

标签: python wizard pyqt5

我创建了一个包含多个页面的QWizard对象,当我点击特定页面的NextButton时,我试图打开一个文件。起初,我尝试了QWizard.NextButton.clicked.connect(),附带: " AttributeError:' WizardButton'对象没有属性'点击''。

之后,我搜索了#34; WizardButton"在Qt助手中,无法发出任何有用的信号,是否有可能像QPushButton.clicked.connect()那样实现?

这是特定页面:

class AccountPage(QWizardPage):

    def __init__(self, parent=None):
        super(AccountPage, self).__init__(parent)

        self.setTitle("Account Information")

        NameLabel = QLabel("&Name:")
        NameLineEdit = QLineEdit()
        NameLabel.setBuddy(NameLineEdit)

        EmailLabel = QLabel("&Email Address:")
        EmailLineEdit = QLineEdit()
        EmailLabel.setBuddy(EmailLineEdit)

        PwdLabel = QLabel("&Password:")
        PwdLineEdit = QLineEdit()
        PwdLabel.setBuddy(PwdLineEdit)

        self.registerField('Name*', NameLineEdit)
        self.registerField('EmailAddress*', EmailLineEdit)
        self.registerField('Password*', PwdLineEdit)

        layout = QGridLayout()
        layout.addWidget(NameLabel, 0, 0)
        layout.addWidget(NameLineEdit, 0, 1)
        layout.addWidget(EmailLabel, 1, 0)
        layout.addWidget(EmailLineEdit, 1, 1)
        layout.addWidget(PwdLabel, 2, 0)
        layout.addWidget(PwdLineEdit, 2, 1)

        self.setLayout(layout)

        QWizard.NextButton.clicked.connect(self.setMB)

    def setMB(self):
        try:
            with open('mb.ini', 'w') as actInfo:
                actInfo.write('sj')

        except IOError as err:
            print('mb.ini error:' + str(err))

1 个答案:

答案 0 :(得分:1)

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

class Window(QWizard):  
    def __init__(self):
        super(Window, self).__init__()
        self.accoutPage = AccountPage()
        self.secondPage = Page2()
        self.thirdPage = Page3()
        self.addPage(self.accoutPage)
        self.addPage(self.secondPage)
        self.addPage(self.thirdPage)
        self.button(QWizard.NextButton).clicked.connect(self.accoutPage.setMB)


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

        self.setTitle("Account Information")

        self.NameLabel = QLabel("&Name:")
        self.NameLineEdit = QLineEdit()
        self.NameLabel.setBuddy(self.NameLineEdit)

        self.EmailLabel = QLabel("&Email Address:")
        self.EmailLineEdit = QLineEdit()
        self.EmailLabel.setBuddy(self.EmailLineEdit)

        self.PwdLabel = QLabel("&Password:")
        self.PwdLineEdit = QLineEdit()
        self.PwdLabel.setBuddy(self.PwdLineEdit)

        self.registerField('Name*', self.NameLineEdit)
        self.registerField('EmailAddress*', self.EmailLineEdit)
        self.registerField('Password*', self.PwdLineEdit)


        layout = QGridLayout()
        layout.addWidget(self.NameLabel, 0, 0)
        layout.addWidget(self.NameLineEdit, 0, 1)
        layout.addWidget(self.EmailLabel, 1, 0)
        layout.addWidget(self.EmailLineEdit, 1, 1)
        layout.addWidget(self.PwdLabel, 2, 0)
        layout.addWidget(self.PwdLineEdit, 2, 1)

        self.setLayout(layout)
        self.i = 0 #if you need to call it once

    def setMB(self):
        if self.i == 0: 
            print(self.PwdLineEdit.text(), self.i) 
            try:
                with open('mb.ini', 'w') as actInfo:
                    actInfo.write('sj')

            except IOError as err:
                print('mb.ini error:' + str(err))
            finally:
                self.i += 1


class Page2(QWizardPage):
    def __init__(self):
        super(Page2, self).__init__()

class Page3(QWizardPage):
    def __init__(self):
        super(Page3, self).__init__()

def main():
    app = QApplication(sys.argv)
    app.setStyle('plastique')

    window = Window()
    window.setWizardStyle(1)
    window.show()
    app.exec_()  

if __name__ == "__main__":
    sys.exit(main())