如何在python qt designer中使用两个按钮显示消息

问题描述 投票:0回答:1

我正在构建一个小的扫雷器克隆,并且我在此处具有一个功能,用于用户单击炸弹时该按钮显示“ boom”的功能,但是我想添加一个功能,其中会弹出诸如菜单的提示用户他们迷路了,并为他们提供了两个按钮,一个继续操作,另一个要求开始新游戏。

def buttonClickedkill(self):
    # sender() tells us who caused the action to take place
    clicked = self.sender()
    #letter=clicked.text()  # the buttons have the letters on them
    #print(f"Button -{letter}- was clicked!")
    # 1) Disable the button
    clicked.setEnabled(False)
    clicked.setText("boom")
    QMainWindow.__init__(self)

所以我想在弹出的东西到来的地方添加另一个功能,例如:

抱歉,您炸弹炸死了!

继续吗?新游戏!

“继续”和“新游戏”是两个按钮我有一个新的游戏功能。

您还可以向我提供必要的脚本,该脚本将在单击按钮之一后立即关闭窗口吗?

python qt pyqt5 designer
1个回答
1
投票
这是QMessageBox的确切用例。例如:

QMessageBox

此行将弹出一个窗口,并阻塞主GUI,直到用户单击按钮。由于我选择了reply = QMessageBox.question(self, 'Title', 'You lost! Continue?')
,因此默认按钮为“是”和“否”。您可以询问QMessageBox.question变量是用户单击了“是”(reply)还是“否”(QMessageBox.Yes)按钮。

工作示例:

QMessageBox.No

哪个生成:

import sys from PyQt5.QtWidgets import (QApplication, QLabel, QMainWindow, QMessageBox, QPushButton, QVBoxLayout, QWidget) class MyApp(QMainWindow): def __init__(self): super().__init__() self.widget = QWidget(self) self.setCentralWidget(self.widget) layout = QVBoxLayout() self.widget.setLayout(layout) self.button = QPushButton(parent=self, text="Click Me!") self.button.clicked.connect(self.button_clicked_kill) self.text = QLabel(parent=self, text='') layout.addWidget(self.button) layout.addWidget(self.text) def button_clicked_kill(self): reply = QMessageBox.question(self, 'Title', 'You lost! Continue?') if reply == QMessageBox.Yes: self.text.setText('User answered yes') if reply == QMessageBox.No: self.text.setText('User answered no') if __name__ == '__main__': app = QApplication() gui = MyApp() gui.show() sys.exit(app.exec_())

© www.soinside.com 2019 - 2024. All rights reserved.