[英]Unable to access wizard() of wizardpage
我正在尝试创建一个非常简单的 QWizard(实际上是为不同错误创建最小可重现示例的过程的一部分)。 我想要做的是访问 QWizardPage 的父级,即使用 the.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() 是到 go 的正确方法。
根据@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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.