检查单击QMessageBox中的哪个按钮

如何检查以下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()
iCMS 回答:检查单击QMessageBox中的哪个按钮

您必须将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()
本文链接:https://www.f2er.com/2053540.html

大家都在问