检查单击了 QMessageBox 中的哪个按钮

Check which button in an QMessageBox was clicked

如何查看点击了以下QMessageBox中的哪个按钮? button == 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()