【问题标题】:How to put an event on the buttons of QMessageBox如何在 QMessageBox 的按钮上放置一个事件
【发布时间】:2021-07-22 15:22:37
【问题描述】:
我只想在消息框的“是”按钮上设置一个事件,但是当我单击“是”按钮时,它返回 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_()
【问题讨论】:
标签:
python
pyqt
pyqt5
qmessagebox
【解决方案1】:
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")