PyQt5 消息对话框QMessageBox

2019-08-13 22:58:33 浏览数 (1)

本篇讲解PyQt中预置的5种消息对话框:

  • QMessageBox.about 关于
  • QMessageBox.ctitical危险
  • QMessageBox.information 信息框
  • QMessageBox.question 询问框
  • QMessageBox.warning 警告

具体的运用详见代码:

代码语言:javascript复制
import sys
from PyQt5.QtWidgets import *
class App(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle("PyQt5 消息对话框")
        vlayout  =QVBoxLayout()
        bt1 = QPushButton("about 对话框")
        bt2 = QPushButton("critical 对话框")
        bt2_ = QPushButton("information 对话框")
        bt3 = QPushButton("question 对话框")
        bt4 = QPushButton("warning 对话框")
        vlayout. addWidget(bt1)
        vlayout. addWidget(bt2)
        vlayout. addWidget(bt2_)
        vlayout. addWidget(bt3)
        vlayout. addWidget(bt4)
        self.setLayout(vlayout)
 
        bt1.clicked.connect(self.about_dlg)
        bt2.clicked.connect(self.critical_dlg)
        bt2_.clicked.connect(self.info_dlg)
        bt3.clicked.connect(self.question_dlg)
        bt4.clicked.connect(self.warning_dlg)
        self.resize(200,200)
    def about_dlg(self):
        reply = QMessageBox.about(self, "about 对话框 标题", "关于消息 内容")
        #about (QWidget parent, QString caption, QString text)
        #print(reply) # QMessageBox.about()返回None
    def critical_dlg(self):
        reply = QMessageBox.critical(self, "critical 对话框 标题", "危险消息 内容")
        #print(reply)
        #print (reply == QMessageBox.Ok)
 
    def info_dlg(self):
        reply = QMessageBox.information(self, "information 对话框 标题", "信息 内容n新年快乐!")
        #print(reply)
        #print (reply == QMessageBox.Ok)
 
    def question_dlg(self):
        reply = QMessageBox.question(self, "information 对话框 标题", "询问 内容n你是猴子派来的吗?",
                                     QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel)
        #点右上角的叉关闭对话框,则相当于点最后一个按钮(默认为 “cancle”)
        if reply == QMessageBox.Yes:
            print("Yes")
        elif reply == QMessageBox.No:
            print("No")
        elif reply == QMessageBox.Cancel:
            print("Cancled")
    def warning_dlg(self):
        reply = QMessageBox.warning(self, "warning 对话框 标题", "警告消息 内容")
        #print(reply)
        #print (reply == QMessageBox.Ok)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())

0 人点赞