简体   繁体   English

pyqt5 python:单击同一对话框上的“重试”按钮时如何保持打开对话框

[英]pyqt5 python: how to keep open dialog box when clicking "retry" button on same dialog

Is there any way to keep the dialog box open once clicking one of the buttons to retry the IF statement that opens this box in first instance?单击其中一个按钮以重试在第一个实例中打开此框的 IF 语句后,是否有任何方法可以使对话框保持打开状态? I want to continue clicking the "Retry" button once the condition is achieved without having this dialog box closed... otherwise, can you give me an idea how I can make this functionality?我想在满足条件后继续单击“重试”按钮而不关闭此对话框...否则,您能告诉我如何实现此功能吗?

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()

Thanks heaps!谢谢堆!

The static functions of QMessageBox automatically connect its buttons, so you only have two options: you either use a while loop and create a QMessageBox every time the value isn't valid and the reply button has been pressed, or you create a QMessageBox instance and disconnect its default signals. QMessageBox 的静态函数会自动连接它的按钮,所以你只有两个选择:你要么使用 while 循环并在每次值无效且回复按钮被按下时创建一个 QMessageBox,要么创建一个 QMessageBox 实例并断开其默认信号。

Basic while loop基本的while循环

This is the simplest solution: a while loop that continuously shows the message box until the value is valid;这是最简单的解决方案:持续显示消息框直到值有效的while循环; the drawback is that you cannot reuse the existing dialog, and a new one will always be shown;缺点是你不能重用现有的对话框,并且总是会显示一个新的;

    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
        # ...

Using a QMessageBox instance使用 QMessageBox 实例

This is a bit more complex, but also more consistent.这有点复杂,但也更一致。 You need to create a new instance of QMessageBox, disconnect the clicked signal of its buttonbox (which is what QMessageBox uses to decide how to set its finished value), and instead connect its accepted and rejected signals;您需要创建一个新的 QMessageBox 实例,断开其clicked框的clicked信号(这是 QMessageBox 用来决定如何设置其finished值的信号),而是连接其acceptedrejected信号; the latter will cancel the dialog, while the former will call a local function that generates a new value, and eventually accepts the dialog if it's valid:后者将取消对话框,而前者将调用生成新值的本地函数,并最终接受对话框(如果有效):

    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_()
        # ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM