【发布时间】: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