Python 检查在QMessageBox中单击了哪个按钮

Python 检查在QMessageBox中单击了哪个按钮,python,button,pyqt,pyqt5,qmessagebox,Python,Button,Pyqt,Pyqt5,Qmessagebox,我应该如何检查单击了以下QMessageBox中的哪个按钮?按钮==QMessageBox.Ok不工作 class WarningWindow(QMessageBox): nextWindowSignal = pyqtSignal() def __init__(self): super().__init__() self.setWindowTitle('A title') self.setText("Generic

我应该如何检查单击了以下QMessageBox中的哪个按钮?
按钮==QMessageBox.Ok
不工作

class WarningWindow(QMessageBox):

    nextWindowSignal = pyqtSignal()

    def __init__(self):
        super().__init__()

        self.setWindowTitle('A title')
        self.setText("Generic text")
        self.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        self.buttonClicked.connect(self.nextWindow)

    def nextWindow(self, button):
        if button == QMessageBox.Ok:
            print('ok')
        elif button == QMessageBox.Cancel:
            print('cancel')
        else:
            print('other)

        self.nextWindowSignal.emit()

您必须将QAbstractButton转换为QMessageBox::StandardButton,然后进行比较:

def nextWindow(self, button):
    sb = self.standardButton(button)
    if sb == QMessageBox.Ok:
        print("ok")
    elif sb == QMessageBox.Cancel:
        print("cancel")
    else:
        print("other")
    self.nextWindowSignal.emit()

非常感谢你!