Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python qt设计器中使用两个按钮显示消息_Python_Qt_Pyqt5_Designer - Fatal编程技术网

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

如何在python qt设计器中使用两个按钮显示消息,python,qt,pyqt5,designer,Python,Qt,Pyqt5,Designer,我正在构建一个小型扫雷舰克隆,我在这里有一个功能,用于用户点击炸弹时按钮显示“boom”,但我还想添加一个功能,其中弹出一个菜单,提示用户他们丢失了,并为他们提供两个按钮,一个按钮继续,另一个要求开始一个新游戏 def buttonClickedkill(self): # sender() tells us who caused the action to take place clicked = self.sender() #letter=clicked.text()

我正在构建一个小型扫雷舰克隆,我在这里有一个功能,用于用户点击炸弹时按钮显示“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)
然后我想添加另一个函数,其中会出现一个弹出窗口,并显示如下内容:

很抱歉你撞到炸弹死了

继续?新游戏

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


另外,请您为我提供必要的脚本,以便在单击其中一个按钮时立即关闭窗口。

这正是。例如:

reply = QMessageBox.question(self, 'Title', 'You lost! Continue?')
这一行将弹出一个窗口并阻止主GUI,直到用户单击按钮。由于我选择了
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_())
由此产生:


我不知道你是怎么做到的,但我对你感激不尽!我被困了两个小时,allready在Youtube上看了一堆印度视频,我甚至创建并导入了一个不同的类,创建了一个几乎全新的设计和东西,我不敢相信这有那么简单。非常感谢你!很高兴我能帮忙!请记住,大多数“常见”GUI功能,如文本框、进度条、选项卡等,在Qt上都有一些现成的实现。当你遇到问题时,试着在(公认的混乱和不完整)文档中挖掘更多的内容。@ YESITSME也考虑了原始C++文档,它们比PySad更全面和更可读。即使是面向C++的,在99%种情况下函数名、参数和返回数据类型都是相同的,并且可以在python shell中检查pyths/doc或使用<代码>帮助())/<代码>的不一致性。最后,从研究基类(QObject/QWidget)开始,并从中读取每个继承的类(例如:查看从QDialog继承的所有类,这些类又从QWidget继承)。再次感谢您,您的回答绝对专业。