微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在 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_()

解决方法

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")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。