【问题标题】:Unable to access wizard() of wizardpage无法访问 wizardpage 的 wizard()
【发布时间】:2022-11-28 23:50:34
【问题描述】:

我正在尝试创建一个非常简单的 QWizard(实际上是为不同错误创建最小可重现示例的过程的一部分)。我想要做的是访问 QWizardPage 的父级,即使用 .wizard() 调用。

这是代码:

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

class MagicWizard(QWizard):
    def __init__(self, parent=None):
        super(MagicWizard, self).__init__(parent)
        self.addPage(Page1(self))
        self.setWindowTitle("PyQt5 Wizard Example - based on pythonspot.com example")
        self.resize(640,480)

class Page1(QWizardPage):
    def __init__(self, parent=None):
        super(Page1, self).__init__(parent)
        self.myLabel = QLabel("Testing registered fields")
        layout = QVBoxLayout()
        layout.addWidget(self.myLabel)
        self.setLayout(layout)
        print(self.wizard())
        print(self.parent())

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    wizard = MagicWizard()
    wizard.show()
    sys.exit(app.exec())

这会正确加载并且控制台记录:

None
<__main__.MagicWizard object at 0x101693790>

第一行是对 self.wizard() 的调用,我希望它与 self.parent() 相同。我显然可以使用 .parent() 并且它会起作用,但我知道 .wizard() 是正确的方法。

【问题讨论】:

  • 它显示None,因为你在__init__中调用它,此时addPage()仍在等待构造函数返回实例。
  • 谢谢!现在你提到它很明显。移动对 initializePage 函数的调用,我可以看到它有效。

标签: pyqt pyqt6 qwizard qwizardpage


【解决方案1】:

根据@musicamante 的指导,我已更改为将 wizard() 调用移出构造函数(显然)它将不起作用。它现在看起来像这样并且工作正常。

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

class MagicWizard(QWizard):
    def __init__(self, parent=None):
        super(MagicWizard, self).__init__(parent)
        self.addPage(Page1(self))
        self.setWindowTitle("PyQt5 Wizard Example - based on pythonspot.com example")
        self.resize(640,480)

class Page1(QWizardPage):
    def __init__(self, parent=None):
        super(Page1, self).__init__(parent)
        self.myLabel = QLabel("Testing registered fields")
        layout = QVBoxLayout()
        layout.addWidget(self.myLabel)
        self.setLayout(layout)
    
    def initializePage(self):
        print(self.wizard())

    def button_push(self):
        print(self.wizard())

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    wizard = MagicWizard()
    wizard.show()
    sys.exit(app.exec())

【讨论】:

    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多