问题描述
我只想在消息框的“是”按钮上设置一个事件,但是当我单击“是”按钮时,它会返回 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")