【转】PyQt4 精彩实例分析 实例 5 各种消息框的使用
在实际的程序开发中,经常会用到各种各样的消息框来给用户一些提示或提醒,Qt 提供了 QMessageBox 类
来实现此项功能。在本实例中,分析了各种消息框的使用方式及之间的区别。各种消息框的使用如图所示:

实现代码如下:
1 #!/usr/bin/python 2 # -*- coding: utf-8 -*- 3 4 from PyQt4.QtGui import * 5 from PyQt4.QtCore import * 6 import sys 7 8 9 QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8")) 10 11 class MessageBoxDialog(QDialog): 12 13 def __init__(self, parent=None): 14 super(MessageBoxDialog, self).__init__(parent) 15 16 self.setWindowTitle("MessageBox") 17 18 self.label_0 = QLabel(self.tr("About Qt MessageBox")) 19 20 self.btn_question = QPushButton("Question") 21 self.btn_information = QPushButton("Information") 22 self.btn_warning = QPushButton("Warning") 23 self.btn_critical = QPushButton("Critical") 24 self.btn_about = QPushButton("About") 25 self.btn_about_qt = QPushButton("About Qt") 26 self.btn_custom = QPushButton("Custom") 27 28 self.Layout() 29 self.ConnectSignalSlot() 30 31 def Layout(self): 32 gridLayout = QGridLayout(self) 33 gridLayout.addWidget(self.label_0, 0, 0, 1, 2) 34 gridLayout.addWidget(self.btn_question, 1, 0) 35 gridLayout.addWidget(self.btn_information, 1, 1) 36 gridLayout.addWidget(self.btn_warning, 2, 0) 37 gridLayout.addWidget(self.btn_critical, 2, 1) 38 gridLayout.addWidget(self.btn_about, 3, 0) 39 gridLayout.addWidget(self.btn_about_qt, 3, 1) 40 gridLayout.addWidget(self.btn_custom, 4, 0) 41 # self.setLayout(layout) 42 43 def ConnectSignalSlot(self): 44 self.connect(self.btn_question, SIGNAL("clicked()"), self.slotQuestion) 45 self.connect(self.btn_information, SIGNAL("clicked()"), self.slotInformation) 46 self.connect(self.btn_warning, SIGNAL("clicked()"), self.slotWarning) 47 self.connect(self.btn_critical, SIGNAL("clicked()"), self.slotCritical) 48 self.connect(self.btn_about, SIGNAL("clicked()"), self.slotAbout) 49 self.connect(self.btn_about_qt, SIGNAL("clicked()"), self.slotAboutQt) 50 self.connect(self.btn_custom, SIGNAL("clicked()"), self.slotCustom) 51 52 def slotQuestion(self): 53 button = QMessageBox.question(self, 54 "Question", 55 self.tr("Has reached the end of the document, whether from scratch to find?"), 56 QMessageBox.Ok|QMessageBox.Cancel, 57 QMessageBox.Ok) 58 if button == QMessageBox.Ok: 59 self.label_0.setText("Question button/Ok") 60 elif button == QMessageBox.Cancel: 61 self.label_0.setText("Question button/Cancel") 62 else: 63 return 64 65 def slotInformation(self): 66 QMessageBox.information(self,"Information", 67 self.tr("Write any information!")) 68 self.label_0.setText("Information MessageBox") 69 70 def slotWarning(self): 71 button = QMessageBox.warning(self,"Warning", 72 self.tr("Save changes to the document?"), 73 QMessageBox.Save|QMessageBox.Discard|QMessageBox.Cancel, 74 QMessageBox.Save) 75 if button == QMessageBox.Save: 76 self.label_0.setText("Warning button/Save") 77 elif button == QMessageBox.Discard: 78 self.label_0.setText("Warning button/Discard") 79 elif button == QMessageBox.Cancel: 80 self.label_0.setText("Warning button/Cancel") 81 else: 82 return 83 84 def slotCritical(self): 85 QMessageBox.critical(self,"Critical", 86 self.tr("Remind the user a fatal error!")) 87 self.label_0.setText("Critical MessageBox") 88 89 def slotAbout(self): 90 QMessageBox.about(self,"About",self.tr("About Example")) 91 self.label_0.setText("About MessageBox") 92 93 def slotAboutQt(self): 94 QMessageBox.aboutQt(self,"About Qt") 95 self.label_0.setText("About Qt MessageBox") 96 97 def slotCustom(self): 98 customMsgBox = QMessageBox(self) 99 customMsgBox.setWindowTitle("Custom message box") 100 lockButton = customMsgBox.addButton(self.tr("Lock"), 101 QMessageBox.ActionRole) 102 unlockButton = customMsgBox.addButton(self.tr("UnLock"), 103 QMessageBox.ActionRole) 104 cancelButton = customMsgBox.addButton("cancel",QMessageBox.ActionRole) 105 customMsgBox.setText(self.tr("Custom MessageBox!")) 106 customMsgBox.exec_() 107 button = customMsgBox.clickedButton() 108 if button == lockButton: 109 self.label_0.setText("Custom MessageBox/Lock") 110 elif button == unlockButton: 111 self.label_0.setText("Custom MessageBox/Unlock") 112 elif button == cancelButton: 113 self.label_0.setText("Custom MessageBox/Cancel") 114 115 def main(): 116 app = QApplication(sys.argv) 117 form = MessageBoxDialog() 118 form.show() 119 app.exec_() 120 121 if __name__ == '__main__': 122 main()
本实例主要分析 7 种类型的消息框,包括 Question 消息框,Information 消息框,Warning 消息
框,Critical 消息框,About 消息框,About Qt 消息框以及 Custom 自定义消息框。
Question 消息框,Information 消息框,Warning 消息框和 Critical 消息框的用法大同小异,这些消
息框一般都包含一条提示信息,一个图标以及若干个按钮,它们的作用都是给用户提供一些提醒或一些简单的
询问。按图标的不同可区分为以下 4 个级另
Question:为正常的操作提供一个简单的询问。
Information:为正常的操作提供一个提示。
Warning:提醒用户发生了一个错误。
Critical:警告用户发生了一个严重错误。
下面分别对各种消息框的使用方法进行分析。
下图为 Question 消息框。

关于 Question 消息框,调用时直接使用 QMessageBox.question()即可。
第一个参数为消息框的父窗口指针。
第二个参数为消息框的标题栏。
第三个参数为消息框的文字提示信息,前 3 个参数对于其他几种消息框基本是一样的。
后面两个参数都是对消息框按钮的设定,QMessageBox 类提供了许多标准按钮,如
QMessageBox.Ok,QMessageBox.Close,QMessageBox.Discard 等,具体可查问 Qt 帮助。
第四个参数即填写希望在消息框中出现的按钮,可根据需要在标准按钮中选择,用“|”连写,默认为
QMessageBox.Ok。
第五个参数为默认按钮,即消息框出现时,焦点默认处于哪个按钮上。
函数的返回值为按下的按钮,当用户按下 Escape 键时,相当于返回 QMessageBox.Cancel。
如下图所示为 Information 消息框。

Information 消息框使用频率最高也最简单,直接调用 QMessageBox.information()即可。
第一个参数为消息框的父窗口指针。
第二个参数为消息框的标题栏。
第三个参数为消息框的文字提示信息。
后面的两个参数与 Qustion 消息框的用法一样,但在使用的过程中,经常会省略后两个参数,直接使用默认
的 QMessageBox.Ok 按钮。
Information 消息框和 Question 消息框可以通用,使用权 Question 消息框的地方都可以使用
Information 消息框替换。
如下图所示为 Warning 消息框。

Warning 消息框的最常用法为当用户进行了一个非正常操作时,提醒用户并询问是否进行某项操作,如关闭
文档,提醒并询问用户是否保存对文档的修改。实例中实现的即是此操作。
函数调用的方式与前面 Question 消息框的调用方式大致相同。
第一个参数为消息框的父窗口指针。
第二个参数为消息框的标题栏。
第三个参数为消息框的文字提示信息,
第四个参数为希望在消息框中出现的按钮,可根据需要在标准按钮中选择,用“|”连写,默认为
QMessageBox.Ok。
第五个参数为默认按钮,即消息框出现时,焦点默认处于哪个按钮上。
如下图所示为 Critical 消息框。

Critical 消息框是在系统出现严重错误时对用户进行提醒的。它的用法也相对简单,通常情况下和
Information 消息框一样,在调用时只填写前 3 个参数即可。
如下图所示为 About 消息框。

About 消息框一般用于提供系统的版本等信息。只需提供信息而并不需要用户反馈信息,因此它的用法相对
简单,直接调用 QMessageBox.about(),并只用指定消息框父窗口,标题栏以及信息的内容即可。
在介绍完以上几种基本消息框的用法后,还有两种特殊的消息框类型,分别是“About Qt 消息框”以及自定
义消息框。
如下图所示为 About Qt 消息框。

“About Qt 消息框”是 Qt 预定好的一种消息框,用于提供 Qt 的相关信息,只需直接调用
QMessageBox.aboutQt(),并提定父窗口和标题栏即可,其中显示的内容是 Qt 预定义好的。
最后,当以上所有的消息框都不能满足开发的需求时,Qt 还允许 Custom 自定义消息框。包括消息框的图
标,按钮,内容等都可根据需要进行设定。本实例中即实现了一个如下图所示的自定义消息框。

在 slotCustom()函数中,第 98 行首先创建一个 QMessageBox 对象 customMsgBox。第 99 行设置此消息
框的标题栏为 Custom message box。
第 100-104 行定义消息框所需的按钮,因此 QMessageBox 类提供了一个 addButton()函数来为消息框增加
自定义按钮,addButton()函数的第一个参数为按钮显示的文字,第二个参数为按钮类型的描述,具体可查
阅 QMessageBox.ButtonRole,当然也可使用 addButton()函数来加入一个标准按钮。如第 90 行在消息
框中加入了一个 QMessageBox.Cancel 按钮。消息框将会按调用 addButton()的先后次序在消息框中由左
至右依次插入按钮。
第 105 行调用 setText 设置自定义消息框中显示的提示信息内容。
第 106 行调用 exec()显示此自定义消息框。
后面几行代码完成的都是实例中一些显示的功能,此处不再讨论。
通过本实例的分析可见,Qt 提供的消息框类型基本涵盖了开发应用中使用的各种情况,并且提供了自定义消
息框的方式,满足各种特殊的需求,在实际应用中关键是分析实际的应用需求,根据不同的应用环境选择最合
适的消息框,以使程序简洁而合理。

浙公网安备 33010602011771号