Prompt on exit in PyQt application

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 11:05:28

问题


Is there any way to promt user to exit the gui-program written in Python?

Something like "Are you sure you want to exit the program?"

I'm using PyQt.


回答1:


Yes. You need to override the default close behaviour of the QWidget representing your application so that it doesn't immediately accept the event. The basic structure you want is something like this:

def closeEvent(self, event):

    quit_msg = "Are you sure you want to exit the program?"
    reply = QtGui.QMessageBox.question(self, 'Message', 
                     quit_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

    if reply == QtGui.QMessageBox.Yes:
        event.accept()
    else:
        event.ignore()

The PyQt tutorial mentioned by las3rjock has a nice discussion of this. Also check out the links from the PyQt page at Python.org, in particular the official reference, to learn more about events and how to handle them.



来源:https://stackoverflow.com/questions/1414781/prompt-on-exit-in-pyqt-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!