簡體   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