如何自定义pyqt消息框?

Den*_*nis 0 python pyqt 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_()
Run Code Online (Sandbox Code Playgroud)

这会显示一个消息框,但是当单击某个选项时,不会发生任何事情并且该框会关闭。如何让下一个函数运行?

eyl*_*esc 6

如果文档被审查:

int QMessageBox::exec()

将消息框显示为模式对话框,在用户将其关闭之前一直处于阻塞状态。

当使用带有标准按钮的 QMessageBox 时,此函数返回一个 StandardButton 值,指示单击的标准按钮。当使用带有自定义按钮的QMessageBox时,该函数返回一个不透明的值;使用 clickedButton() 来确定单击了哪个按钮。

注意:result() 函数还返回 StandardButton 值而不是 QDialog::DialogCode

在通过单击按钮或使用窗口系统提供的机制关闭对话框之前,用户无法与同一应用程序中的任何其他窗口进行交互。

另请参见 show() 和 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")
Run Code Online (Sandbox Code Playgroud)