接下来这个专题介绍PyQt的一些内容 教程翻译自: [https://www.tutorialspoint.com/pyqt/pyqt_introduction.htm](https://www.tutorialspoint.com/pyqt/pyqt_introduction.htm) 由于本人也是学习状态,翻译可能不准确,请及时指出,我会很快修正 一些关键字会直接使用英文 目前该专题为纯理论,实际操作在完成后有演示 PyQt版本: PyQt4 ##1. 什么是QMessageBox QMessageBox是一个经常用到的modal 对话框,用来显示一些信息 还可以用来响应用户的选择,这些选择是经过预先定义的 ## 2. QMessageBox的一些方法 **setIcon()** 显示一些预先定义的图标用来表示消息的级别 [image:145 size:orig] [image:146 size:orig] [image:147 size:orig] [image:148 size:orig] **setText()** 设置主消息的内容 **setInformativeText()** 设置额外信息的内容 **setDetailText()** 设置详细信息的内容 **setTitle()** 设定标题 **setStandardButtons()** 设置预先定义的按钮,如OK Cancel等 **setDefaultButton()** 设置默认的按钮,即当按回车时点击的按钮 **setEscapeButton()** 设置按 escape 时点击的按钮 ##3. 应用举例 ``` import sys from PyQt4.QtGui import * from PyQt4.QtCore import * def window(): app = QApplication(sys.argv) w = QWidget() b = QPushButton(w) b.setText("Show message!") b.move(50,50) b.clicked.connect(showdialog) w.setWindowTitle("PyQt Dialog demo") w.show() sys.exit(app.exec_()) def showdialog(): msg = QMessageBox() msg.setIcon(QMessageBox.Information) msg.setText("This is a message box") msg.setInformativeText("This is additional information") msg.setWindowTitle("MessageBox demo") msg.setDetailedText("The details are as follows:") msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) msg.buttonClicked.connect(msgbtn) retval = msg.exec_() print "value of pressed message box button:", retval def msgbtn(i): print "Button pressed is:",i.text() if __name__ == '__main__': window() ``` [image:149 size:orig] [image:150 size:orig]