简体   繁体   English

如何自定义pyqt消息框?

[英]How to customise a pyqt message box?

pyqt4 pyqt4

msgBox = QtGui.QMessageBox()
msgBox.setText('Which type of answers would you like to view?')
msgBox.addButton(QtGui.QPushButton('Correct'), QtGui.QMessageBox.YesRole)
msgBox.addButton(QtGui.QPushButton('Incorrect'), QtGui.QMessageBox.NoRole)
msgBox.addButton(QtGui.QPushButton('Cancel'), QtGui.QMessageBox.RejectRole)

if msgBox == QtGui.QMessageBox.YesRole:
     Type = 1
      Doc()
elif msgBox == QtGui.QMessageBox.NoRole:
     Type = 0
     Bank()
else:
    ret = msgBox.exec_()

This displays a message box however when an option is clicked, nothing happens and the box closes. 这将显示一个消息框,但是当单击某个选项时,没有任何反应,并且该框关闭。 How do I get the next function to run? 如何获得下一个要运行的功能?

If the docs are reviewed: 如果文档已审核:

int QMessageBox::exec() int QMessageBox :: exec()

Shows the message box as a modal dialog, blocking until the user closes it. 将消息框显示为模式对话框,直到用户将其关闭之前,该对话框一直处于阻塞状态。

When using a QMessageBox with standard buttons, this functions returns a StandardButton value indicating the standard button that was clicked. 当使用带有标准按钮的QMessageBox时,此函数返回一个StandardButton值,该值指示单击的标准按钮。 When using QMessageBox with custom buttons, this function returns an opaque value; 当使用带有自定义按钮的QMessageBox时,此函数返回一个不透明的值。 use clickedButton() to determine which button was clicked. 使用clickedButton()确定单击了哪个按钮。

Note: The result() function returns also StandardButton value instead of QDialog::DialogCode 注意:result()函数还会返回StandardButton值,而不是QDialog :: DialogCode

Users cannot interact with any other window in the same application until they close the dialog, either by clicking a button or by using a mechanism provided by the window system. 用户在单击按钮或使用窗口系统提供的机制关闭对话框之前,无法与同一应用程序中的任何其他窗口进行交互。

See also show() and result(). 另请参见show()和result()。

So as you recommend you must use clickedButton() , as I show below: 因此,按照您的建议,必须使用clickedButton() ,如下所示:

from PyQt4 import QtGui
import sys


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    msgBox = QtGui.QMessageBox()
    msgBox.setText('Which type of answers would you like to view?')
    correctBtn = msgBox.addButton('Correct', QtGui.QMessageBox.YesRole)
    incorrectBtn = msgBox.addButton('Incorrect', QtGui.QMessageBox.NoRole)
    cancelBtn = msgBox.addButton('Cancel', QtGui.QMessageBox.RejectRole)

    msgBox.exec_()

    if msgBox.clickedButton() == correctBtn:
        print("Correct")
    elif msgBox.clickedButton() == incorrectBtn:
        print("Incorrect")
    elif msgBox.clickedButton() == cancelBtn:
        print("Cancel")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM