gpt4 book ai didi

python - 仅在第一页将 QWizard.NextButton 连接到自定义方法

转载 作者:行者123 更新时间:2023-12-01 00:40:54 25 4
gpt4 key购买 nike

我有一个三页向导。第一页是 BasicSettings(),第二页是 InstallPackages(),最后一页是 Summary()

我希望第一页上的下一步按钮首先执行名为execute_venv_create()的方法,然后调用下一页。在接下来的页面中,下一步按钮应该像往常一样工作。

为此,我将下一步按钮连接到execute_venv_create(),如下所示:

class VenvWizard(QWizard):
"""The wizard class."""
def __init__(self):
super().__init__()
# ...

class BasicSettings(QWizardPage):
"""This is the first page."""
def __init__(self):
super().__init__()
# ...

def initializePage(self):
next_button = self.wizard().button(QWizard.NextButton)
next_button.clicked.connect(self.execute_venv_create)

def execute_venv_create(self):
# do something, then display an info message
QMessageBox.information(self, "Done", "message text")

问题是,当然每次我单击下一步时都会调用该方法,因此我尝试断开按钮并将其重新连接到QWizard.next()方法如下:

class InstallPackages(QWizardPage):
"""This is the second page."""
def __init__(self):
super().__init__()
# ...

def initializePage(self):
next_button = self.wizard().button(QWizard.NextButton)
next_button.disconnect()
next_button.clicked.connect(QWizard.next)

在第一页上,下一步按钮按我的预期工作,它调用该方法并切换到下一页。但是,在第二页 InstallPackages() 上,如果我单击下一步,GUI 就会崩溃。

<小时/>

这是将 QWizard 按钮连接到自定义方法的正确方法吗?还是无法使用 QWizardPages 中的向导按钮?

如何将QWizard.NextButton连接到特定QWizardPage的自定义方法,并使按钮在后续页面上正常运行?

最佳答案

你的方法是正确的,但你必须使用QWizard可以通过 wizard() 获取与页面关联的对象方法:

def execute_venv_create(self):
# do something, then display an info message
QMessageBox.information(self, "Done", "message text")
next_button = self.wizard().button(QWizard.NextButton)
next_button.disconnect()
next_button.clicked.connect(self.wizard().next)

关于python - 仅在第一页将 QWizard.NextButton 连接到自定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57360587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com