Here's an example below:
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.
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!!!"
I hope that helps
The following works correctly for me (the final print statement is executed):
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication([])
win = QtGui.QMainWindow()
win.show()
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!!!"
Usually most PyQt applications have such lines in main:
app = QApplication(sys.argv)
myclass = MyClass()
myclass.show()
sys.exit(app.exec_())
What you can do is define a function that does all the above, and then do whatever you want after the call to app.exec_():
def appExec():
app = QApplication(sys.argv)
# and so on until we reach:
app.exec_()
print("my message...")
# other work...
All what you would have in main is:
sys.exit(appExec())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With