原因
消息框会消失,因为代码中调用的是QMessageBox
的show
方法,该方法并没有保留对它的引用,所以一旦函数返回,它就会被垃圾回收
解决办法
调用QMessageBox
的exec_()
代替show
方法显示弹窗
代码示例
box = QMessageBox()
box.setIcon(1)
box.setWindowTitle("温馨提示")
box.setText("运行环境检测ok!")
# 添加按钮,可用中文
yes = box.addButton('确定', QMessageBox.YesRole)
no = box.addButton('取消', QMessageBox.NoRole)
# 设置消息框中内容前面的图标
box.setIcon(1)
box.exec_()
if box.clickedButton() == yes:
print('确定')
else:
print('取消')