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.

  • 相关阅读:
    72.2801 LOL-盖伦的蹲草计划(广搜)
    71.双向最短路径:聚会
    POJ 2531 Network Saboteur
    POJ 3274 Gold Balanced Lineup(哈希)
    POJ 3349 Snowflake Snow Snowflakes(哈希)
    (重)POJ 3020Antenna Placement
    HDU 1098 Ignatius's puzzle
    中国海洋大学第四届朗讯杯高级组 I Cuckoo for Hashing
    中国海洋大学第四届朗讯杯高级组 A Rocky
    CF 217 B. Berland Bingo
  • 原文地址:https://www.cnblogs.com/fuqia/p/8698296.html
Copyright © 2011-2022 走看看