关闭 PyQt 窗口后如何执行更多代码? [英] How do I execute more code after closing a PyQt window?

查看:56
本文介绍了关闭 PyQt 窗口后如何执行更多代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个例子:

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()
    print "you just closed the pyqt window!!! you are awesome!!!"

当窗口打开或关闭窗口后,上面的打印语句似乎没有被执行.我希望它在关闭窗口后打印.

The print statement above doesn't seem to get executed while the window is open or after I close the window. I want it to print after I close the window.

推荐答案

我通过重新定义 closeEvent 方法来做到这一点,如下所示:

I do that by redefining the closeEvent method, like this:

class YourMainWindow(QtGui.QMainWindow):

    (...)

    def closeEvent(self, *args, **kwargs):
        super(QtGui.QMainWindow, self).closeEvent(*args, **kwargs)
        print "you just closed the pyqt window!!! you are awesome!!!"

希望能帮到你

这篇关于关闭 PyQt 窗口后如何执行更多代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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