目录

PyQt - QMessageBox( QMessageBox)

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

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

Sr.No. 方法和描述
1

setIcon()

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

题

信息 信息

警告 警告

危急 危急

2

setText()

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

3

setInformativeText()

显示其他信息

4

setDetailText()

对话框显示“详细信息”按钮。 单击时会显示此文本

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

如果按下转义键,则将按钮设置为单击

例子 (Example)

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

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()信号连接到槽功能,该功能识别信号源的标题。

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 Output1QMessageBox Output2
↑回到顶部↑
WIKI教程 @2018