美文网首页
PyQt5 Message Box 学习

PyQt5 Message Box 学习

作者: _Mirage | 来源:发表于2020-04-01 04:25 被阅读0次

注意的是当我们要退出一个QWidget组件时, 会产生一个QCloseEvent事件,如果我们想要改变widget组件默认的行为,我们就需要去重写QWidget.closeEvent()这个函数。

然后QMessageBox.question:

"""
question(QWidget, str, str, 
buttons: Union[QMessageBox.StandardButtons,
QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), 
defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton) -> 
QMessageBox.StandardButton
"""
# coding='utf-8'


from PyQt5.QtWidgets import QWidget, QApplication, QMessageBox
import sys


class Gui(QWidget):
    def __init__(self):
        super().__init__()
        self.start()

    def start(self):
        self.setGeometry(250, 250, 500, 600)
        self.setWindowTitle('MessageBox 测试')
        self.show()

    def closeEvent(self, event) -> None:
        reply = QMessageBox.question(self, '提示', '是否离开?',
            QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.No:
            event.ignore()
        else:
            event.accept()


win = QApplication(sys.argv)
gun = Gui()
sys.exit(win.exec_())
运行结果: image.png

相关文章

网友评论

      本文标题:PyQt5 Message Box 学习

      本文链接:https://www.haomeiwen.com/subject/zmgnuhtx.html