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.

  • 相关阅读:
    相对路径和绝对路径的问题"/"带不带斜杠
    El表达式获取项目名称
    struts 中的创建Action的三种方法
    maven中的profile文件的解析
    Maven中模块的聚合以及对jar包的继承
    Maven中解决依赖冲突的问题
    maven中的传递依赖和传递依赖的解除
    String类为什么是final的
    jdbc 报错解决办法
    org.hibernate.PropertyValueException: not-null property references a null or transient value:
  • 原文地址:https://www.cnblogs.com/fuqia/p/8698296.html
Copyright © 2011-2022 走看看