繁体   English   中英

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

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

单击其中一个按钮以重试在第一个实例中打开此框的 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()

谢谢堆!

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

暂无
暂无

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

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