如何自定义一个pyqt消息框?

How to customise a pyqt message box?

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

这会显示一个消息框,但是当单击一个选项时,没有任何反应并且该框关闭。如何获得 运行 的下一个功能?

如果 docs 被审核:

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. When using QMessageBox with custom buttons, this function returns an opaque value; use clickedButton() to determine which button was clicked.

Note: The result() function returns also StandardButton value instead of 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().

因此,按照您的建议,您必须使用 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")