代码:
1 #coding: utf-8 2 from PyQt4.QtGui import * 3 from PyQt4.QtCore import * 4 import sys 5 6 QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8")) 7 8 class MessageBoxDlg(QDialog): 9 def __init__(self, parent=None): 10 super(MessageBoxDlg, self).__init__(parent) 11 self.setWindowTitle("Messagebox") 12 self.label = QLabel("About Qt MessageBox") 13 questionButton = QPushButton("Question") 14 informationButton = QPushButton("Information") 15 warningButton = QPushButton("Warning") 16 criticalButton = QPushButton("Critical") 17 aboutButton = QPushButton("About") 18 aboutqtButton = QPushButton("About Qt") 19 customButton = QPushButton("Custom") 20 21 gridLayout = QGridLayout(self) 22 gridLayout.addWidget(self.label, 0, 0, 1, 2) 23 gridLayout.addWidget(questionButton, 1, 0) 24 gridLayout.addWidget(informationButton, 1, 1) 25 gridLayout.addWidget(warningButton, 2, 0) 26 gridLayout.addWidget(criticalButton, 2, 1) 27 gridLayout.addWidget(aboutButton, 3, 0) 28 gridLayout.addWidget(aboutqtButton, 3, 1) 29 gridLayout.addWidget(customButton, 4, 0) 30 31 self.connect(questionButton,SIGNAL("clicked()"),self.slotQuestion) 32 self.connect(informationButton,SIGNAL("clicked()"),self.slotInformation) 33 self.connect(warningButton,SIGNAL("clicked()"),self.slotWarning) 34 self.connect(criticalButton,SIGNAL("clicked()"),self.slotCritical) 35 self.connect(aboutButton,SIGNAL("clicked()"),self.slotAbout) 36 self.connect(aboutqtButton,SIGNAL("clicked()"),self.slotAboutQt) 37 self.connect(customButton,SIGNAL("clicked()"),self.slotCustom) 38 39 def slotQuestion(self): 40 button = QMessageBox.question(self, "Question", self.tr("已到达文档结尾,是否从头查找?"), QMessageBox.Ok|QMessageBox.Cancel, QMessageBox.Ok) 41 if button == QMessageBox.Ok: 42 self.label.setText("Question button/Ok") 43 elif button == QMessageBox.Cancel: 44 self.label.setText("Question button/Cancel") 45 else: 46 return 47 48 def slotInformation(self): 49 QMessageBox.information(self, "Information", self.tr("填写任意想告诉于用户的信息!")) 50 self.label.setText("Information MessageBox") 51 52 def slotWarning(self): 53 button = QMessageBox.warning(self, "Warning", self.tr("是否保存对文档的修改?"), QMessageBox.Save|QMessageBox.Discard|QMessageBox.Cancel, QMessageBox.Save) 54 if button == QMessageBox.Save: 55 self.label.setText("Warning button/Save") 56 elif button == QMessageBox.Discard: 57 self.label.setText("Warning button/Discard") 58 elif button == QMessageBox.Cancel: 59 self.label.setText("Warning button/Cancel") 60 else: 61 return 62 63 def slotCritical(self): 64 QMessageBox.critical(self, "Critical", self.tr("提醒用户一个致命的错误!")) 65 self.label.setText("Critical MessageBox") 66 67 def slotAbout(self): 68 QMessageBox.about(self, "About", self.tr("About 示例")) 69 self.label.setText("About MessageBox") 70 71 def slotAboutQt(self): 72 QMessageBox.aboutQt(self, "About Qt") 73 self.label.setText("About Qt MessageBox") 74 75 def slotCustom(self): 76 customMsgBox = QMessageBox(self) 77 customMsgBox.setWindowTitle("Custom message box") 78 lockButton = customMsgBox.addButton(self.tr("锁定"), QMessageBox.AcceptRole) 79 unlockButton = customMsgBox.addButton(self.tr("解锁"), QMessageBox.ActionRole) 80 cancelButton = customMsgBox.addButton("cancel", QMessageBox.ActionRole) 81 customMsgBox.setText(self.tr("这是一个自定义的消息框!")) 82 customMsgBox.exec_() 83 84 button = customMsgBox.clickedButton() 85 if button == lockButton: 86 self.label.setText("Custom MessageBox/Lock") 87 elif button == unlockButton: 88 self.label.setText("Custom MessageBox/Unlock") 89 elif button == cancelButton: 90 self.label.setText("Custom MessageBox/Cancel") 91 92 app = QApplication(sys.argv) 93 MessageBox = MessageBoxDlg() 94 MessageBox.show() 95 app.exec_()