我正在尝试使用 PyQt5 实现一个 Qwizard 应用程序,但我无法弄清楚如何根据单击的下一个按钮来调用不同的函数。显然,下一个按钮属于 Qwizard 类,我无法在每个页面上为它们定义特定的行为(如调用不同的函数)。这是一个例子:
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.firstPage = Page1()
self.secondPage = Page2()
self.thirdPage = Page3()
self.addPage(self.firstPage)
self.addPage(self.secondPage)
self.addPage(self.thirdPage)
class Page1(QWizardPage):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.setTitle('''Page 1 Title''')
self.vLayout = QVBoxLayout()
self.lineEdit = QLineEdit()
self.vLayout.addWidget(self.lineEdit)
self.setLayout(self.vLayout)
self.registerField('p1_lineEdit*', self.lineEdit, self.lineEdit.text(), self.lineEdit.textChanged)
def f1(self):
print("f1")
if(len(self.lineEdit.text()) < 10):
self.lineEdit.setText("0" * (10 - len(self.lineEdit.text())) + self.lineEdit.text())
def validatePage(self):
self.f1()
return super(Page1, self).validatePage()
class Page2(QWizardPage):
def __init__(self):
super(Page2, self).__init__()
self.setTitle('''Page 2 Title''')
self.vLayout = QVBoxLayout()
self.textBrowser = QTextBrowser()
self.vLayout.addWidget(self.textBrowser)
text = f'''Please make sure the provided number: {self.field('p1_lineEdit')} that you previously indicated, is valid.'''
self.textBrowser.setText(text)
self.setLayout(self.vLayout)
def f2(self):
print("f2")
print(self.field('p1_lineEdit'))
def validatePage(self):
self.f2()
return super(Page2, self).validatePage()
class Page3(QWizardPage):
def __init__(self):
super(Page3, self).__init__()
self.setTitle('''Page 3 Title''')
def f3(self):
print("f3")
def validatePage(self):
self.f3()
return super(Page3, self).validatePage()
def main():
app = QApplication(sys.argv)
app.setStyle('plastique')
window = Window()
window.setWizardStyle(1)
window.show()
app.exec_()
if __name__ == "__main__":
sys.exit(main())
如何根据 QwizardPage 而不是 Qwizard 区分下一个按钮的行为?假设我希望在第 1 页上按下一个按钮后调用 f1(),在第 2 页上按下一个按钮后调用 f2,在第 3 页后按 f3()。
更新:我正在按照有关最小可重现示例和 XY 问题的指南添加已编辑的问题。单击下一个按钮后,我正在尝试验证特定向导页面上的用户输入,并将其用于填充后续页面。在这里,我要求在第 1 页输入,假装它很短,并将其插入第 2 页文本浏览器中的文本中间。但是,我看到 None 代替页面文本浏览器内的注册字段 p1_lineEdit 2,但是当我在移动到第 3 页之前在 f2() 中打印它时,我看到了正确的值。我希望用户在第 2 页加载时观察到正确的值。
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.firstPage = Page1()
self.secondPage = Page2()
self.thirdPage = Page3()
self.addPage(self.firstPage)
self.addPage(self.secondPage)
self.addPage(self.thirdPage)
class Page1(QWizardPage):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.setTitle('''Page 1 Title''')
self.vLayout = QVBoxLayout()
self.lineEdit = QLineEdit()
self.vLayout.addWidget(self.lineEdit)
self.setLayout(self.vLayout)
self.registerField('p1_lineEdit*', self.lineEdit, self.lineEdit.text(), self.lineEdit.textChanged)
def f1(self):
print("f1")
if(len(self.lineEdit.text()) < 10):
self.lineEdit.setText("0" * (10 - len(self.lineEdit.text())) + self.lineEdit.text())
def validatePage(self):
self.f1()
return super(Page1, self).validatePage()
class Page2(QWizardPage):
def __init__(self):
super(Page2, self).__init__()
self.setTitle('''Page 2 Title''')
self.vLayout = QVBoxLayout()
self.textBrowser = QTextBrowser()
self.vLayout.addWidget(self.textBrowser)
text = f'''Please make sure the provided number: {self.field('p1_lineEdit')} that you previously indicated, is valid.'''
self.textBrowser.setText(text)
self.setLayout(self.vLayout)
def f2(self):
print("f2")
print(self.field('p1_lineEdit'))
def validatePage(self):
self.f2()
return super(Page2, self).validatePage()
class Page3(QWizardPage):
def __init__(self):
super(Page3, self).__init__()
self.setTitle('''Page 3 Title''')
def f3(self):
print("f3")
def validatePage(self):
self.f3()
return super(Page3, self).validatePage()
def main():
app = QApplication(sys.argv)
app.setStyle('plastique')
window = Window()
window.setWizardStyle(1)
window.show()
app.exec_()
if __name__ == "__main__":
sys.exit(main())