在PyQt应用程序中退出时提示 [英] Prompt on exit in PyQt application

查看:141
本文介绍了在PyQt应用程序中退出时提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以提示用户退出用Python编写的gui程序?

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?"

我正在使用PyQt.

推荐答案

是.您需要覆盖代表您的应用程序的QWidget的默认关闭行为,以便它不立即接受该事件.您想要的基本结构是这样的:

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()

教程/1414781/prompt-on-exit-in-pyqt-application/1414828#1414828> las3rjock 对此进行了很好的讨论.另外,请访问Python.org上 PyQt页面中的链接,尤其是官方参考,以了解有关事件及其处理方式的更多信息.

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.

这篇关于在PyQt应用程序中退出时提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆