PyQt - QMessageBox

QMessageBox 是一个常用的模式对话框,用于显示某些信息性消息,并可选择通过单击其上的任何一个标准按钮来要求用户进行响应.每个标准按钮都有一个预定义的标题,一个角色并返回一个预定义的十六进制数.

下表中给出了与QMessageBox类相关的重要方法和枚举 :

Sr.No.方法&描述
1

setIcon()

显示与消息严重程度对应的预定义图标

问题问题

信息信息

警告警告

严重严重

2

setText()

设置要显示的主要消息的文本

3

setInformativeText()

显示附加信息

4

setDetailTe xt()

对话框显示详细信息按钮.单击时出现此文本

5

setTitle()

显示对话框的自定义标题

6

setStandardButtons()

要显示的标准按钮列表.每个按钮与

QMessageBox.Ok 0x00000400

QMessageBox.Open 0x00002000

QMessageBox.Save 0x00000800

QMessageBox.Cancel 0x00400000

QMessageBox.Close 0x00200000

QMessageBox.Yes 0x00004000

QMessageBox.No 0x00010000

QMessageBox.Abort 0x00040000

QMessageBox.Retry 0x00080000

QMessageBox.Ignore 0x00100000

7

setDefaultButton()

将按钮设置为默认值.如果按Enter键,它会发出点击的信号

8

setEscapeButton()

设置按下转义键时被视为单击的按钮

示例

在以下示例中,单击信号顶层窗口上的按钮,连接的功能显示消息框对话框.

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:")

setStandardButton()函数显示所需的按钮.

 
 msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)

buttonClicked()信号连接到一个插槽功能,w hich识别信号源的标题.

 
 msg.buttonClicked.connect(msgbtn)

示例的完整代码如下 :

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()

以上代码产生以下输出 :

QMessageBox Output1 QMessageBox Output2