zoukankan      html  css  js  c++  java
  • a message box to confirm the action

    当点击窗口的X按钮时,弹出确认退出消息框,继续点击Yes,退出。否则,窗口继续处于打开状态

    代码:

     1 """
     2 This program shows a confirmation
     3 message box when we click on the close
     4 button of the application window.
     5 """
     6 import sys
     7 from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication
     8 
     9 
    10 class Example(QWidget):
    11 
    12     def __init__(self):
    13         super().__init__()
    14 
    15         self.initUI()
    16 
    17     def initUI(self):
    18 
    19         self.setGeometry(300, 300, 250, 150)
    20         self.setWindowTitle('Message box')
    21         self.show()
    22 
    23     def closeEvent(self, event):
    24 
    25         reply = QMessageBox.question(self, 'Message',
    26             "Are you sure to quit?", QMessageBox.Yes |
    27             QMessageBox.No, QMessageBox.No)
    28 
    29         if reply == QMessageBox.Yes:
    30             event.accept()
    31         else:
    32             event.ignore()
    33 
    34 
    35 if __name__ == '__main__':
    36 
    37     app = QApplication(sys.argv)
    38     ex = Example()
    39     sys.exit(app.exec_())

    If we close a QWidget, the QCloseEvent is generated. To modify the widget behaviour we need to reimplement the closeEvent() event handler.

    reply = QMessageBox.question(self, 'Message',
                "Are you sure to quit?", QMessageBox.Yes |
                QMessageBox.No, QMessageBox.No)

    The first string appears on the titlebar. 即上图中的'Message'

    The second string is the message text displayed by the dialog. 即上图中的"Are you sure to quit?"

    The third argument specifies the combination of buttons appearing in the dialog. 即上图中的Yes和No组合

    The last parameter is the default button. It is the button which has initially the keyboard focus.

    默认选中button,如上图是No,直接enter即退出窗口

    The return value is stored in the reply variable.

  • 相关阅读:
    Naive Bayesian classification 朴素贝叶斯分类
    svm
    CentOS 7上的性能监控工具
    Elasticsearch .net 客户端条件拼接查询
    centos7 搭建elk
    Elasticsearch 快照和恢复
    Elasticsearch .net client NEST使用说明 2.x
    ELK 日志系统搭建配置
    用微软makecert.exe生成一个自签名的证书
    (从零开始java开发) IDEA+MAVEN构建一个webapp骨架项目(解决一直downloading问题)
  • 原文地址:https://www.cnblogs.com/fuqia/p/8698296.html
Copyright © 2011-2022 走看看