【问题标题】:pyqt5 python: how to keep open dialog box when clicking "retry" button on same dialogpyqt5 python:单击同一对话框上的“重试”按钮时如何保持打开的对话框
【发布时间】:2021-10-28 10:20:36
【问题描述】:

是否有任何方法可以在单击其中一个按钮以重试第一次打开此框的 IF 语句后保持对话框打开? 一旦条件满足,我想在不关闭此对话框的情况下继续单击“重试”按钮...否则,您能告诉我如何实现此功能吗?

import random
import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QPushButton

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("My App")

        button = QPushButton("Press me for a dialog!")
        button.clicked.connect(self.button_clicked)
        self.setCentralWidget(button)

    def button_clicked(self):
        self.rand = random.uniform(0, 1)
        print(self.rand)
        if self.rand > 0.5:
            self.critical = QMessageBox.critical(
            self,
            "Oh no!",
            "Something went very wrong.",
            buttons=QMessageBox.Retry | QMessageBox.Cancel,
            defaultButton=QMessageBox.Retry)
            if self.critical == QMessageBox.Retry:
                print("Retry!")
                self.rand = random.uniform(0, 1)
                print(self.rand)
            else:
                print("Cancel!")
        
        else:
            self.ok = QMessageBox(self)
            self.ok.setWindowTitle("All good!")
            self.ok.setText("Everything looks perfect!")
            self.button = self.ok.exec()
    
            if self.button == QMessageBox.Ok:
                print("OK!")
            

app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec()

谢谢大家!

【问题讨论】:

    标签: python pyqt5 qmessagebox


    【解决方案1】:

    QMessageBox 的静态函数会自动连接它的按钮,所以你只有两个选择:你要么使用一个 while 循环并在每次值无效并且按下回复按钮时创建一个 QMessageBox,或者你创建一个QMessageBox 实例并断开其默认信号。

    基本的while循环

    这是最简单的解决方案:一个while循环,不断显示消息框,直到值有效;缺点是你不能重复使用现有的对话框,并且总是会显示一个新的;

        def button_clicked(self):
            self.rand = random.uniform(0, 1)
            print(self.rand)
            if self.rand > 0.5:
                while self.rand > 0.5:
                    self.critical = QMessageBox.critical(
                    self,
                    "Oh no!",
                    "Something went very wrong.",
                    buttons=QMessageBox.Retry | QMessageBox.Cancel,
                    defaultButton=QMessageBox.Retry)
                    if self.critical == QMessageBox.Retry:
                        print("Retry!")
                        self.rand = random.uniform(0, 1)
                        print(self.rand)
                    else:
                        print("Cancel!")
                        break
            # ...
    

    使用 QMessageBox 实例

    这有点复杂,但也更一致。您需要创建一个新的 QMessageBox 实例,断开其按钮框的clicked 信号(这是 QMessageBox 用来决定如何设置其finished 值的),而是连接其acceptedrejected 信号;后者将取消对话,而前者将调用一个本地函数来生成一个新值,如果对话有效,则最终接受该对话:

        def button_clicked(self):
            self.rand = random.uniform(0, 1)
            if self.rand > 0.5:
                def checkRand():
                    self.rand = random.uniform(0, 1)
                    if self.rand > 0.5:
                        msgBox.accept()
                        print('OK!')
                    else:
                        print(self.rand)
    
                msgBox = QMessageBox(
                    QMessageBox.Critical, 
                    "Oh no!", 
                    "Something went very wrong.", 
                    buttons=QMessageBox.Retry | QMessageBox.Cancel, 
                    parent=self
                    )
                buttonBox = msgBox.findChild(QDialogButtonBox)
                buttonBox.clicked.disconnect()
                buttonBox.rejected.connect(msgBox.reject)
                buttonBox.accepted.connect(checkRand)
                msgBox.exec_()
            # ...
    

    【讨论】:

    • 非常感谢@musicamante 我尝试了 While 选项并且效果很好。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-10-01
    • 2014-03-08
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多