如何在 QMessageBox 的按钮上放置事件

我只想在消息框的“是”按钮上设置一个事件,但是当我单击“是”按钮时,它会返回 None 值并且不满足条件。

def question(self):
    msg = QMessageBox()
    result = msg.setStandardButtons(msg.Yes | msg.No)
    msg.setIcon(msg.Question)
    msg.setText(self.text)
    msg.setWindowTitle('Console')

    if result == msg.Yes:
        print('Yes')

    msg.exec_()
fairhu 回答:如何在 QMessageBox 的按钮上放置事件

setStandardButtons() 是一个 setter 并且不返回任何内容,其逻辑实际上是让按钮被点击,然后获取关联的 QMessageBox::StandardButton

def question(self):
    msg = QMessageBox()
    msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
    msg.setIcon(msg.Question)
    msg.setText(self.text)
    msg.setWindowTitle('Console')
    msg.exec_()
    button = msg.clickedButton()
    sb = msg.standardButton(button)
    if sb == QMessageBox.Yes:
        print("YES")
本文链接:https://www.f2er.com/52801.html

大家都在问