简体   繁体   中英

Unable to access wizard() of wizardpage

I'm trying to create a very simple QWizard (actually as part of the process to create a min reproducible example for a different error). What I want to be able to do is to access the QWizardPage's parent, ie using the.wizard() call.

Here is the code:

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())

This loads correctly and the console logs:

None
<__main__.MagicWizard object at 0x101693790>

The first line is the call to self.wizard() which I was expecting would be the same as self.parent(). I can obviously use.parent() and it will work but I understood that.wizard() was the correct way to go.

As per guidance from @musicamante I've changed to move the wizard() call out of the constructor where it (obviously) will not work. It now looks like this and works fine.

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM