我试图创建一个非常简单的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()是正确的方法。
根据@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())