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.

  • 相关阅读:
    iOS下JS与OC互相调用(三)--MessageHandler
    在xcode6中使用矢量图(iPhone6置配UI)
    UITextField增加textDidChange回调功能
    IOS开发之格式化日期时间(转)
    ios中将事件同步到系统日历
    xcode中一些便捷用法@literals简写
    JavaScript
    fuzz for test of the Net::HTTP::GET
    perl6 中将 字符串 转成十六进制
    Net::HTTP 一次添加 cookie, body 发送post请求
  • 原文地址:https://www.cnblogs.com/fuqia/p/8698296.html
Copyright © 2011-2022 走看看