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

2024-06-26 13:56:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我希望第一页上的Next按钮首先执行名为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")

当然,问题是每次单击Next时都会调用该方法,因此我尝试断开按钮的连接,并以这种方式将其重新连接到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)

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


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

如何将QWizard.NextButton连接到特定QWizardPage的自定义方法,并使按钮在下面的页面上正常工作


Tags: 方法selfexecutevenvinitdefcreatebutton
1条回答
网友
1楼 · 发布于 2024-06-26 13:56:13

您的方法是正确的,但必须使用与页面关联的QWizard对象,该对象可以通过^{}方法获得:

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)

相关问题 更多 >