python 3.x - PyQt - Event Binding (calling a function when the QCloseEvent occurs) - Procedural Style -
i know how can done oop style, interested in understanding pyqt seeing how gui toolkit can programmed in both object orientated , procedural way.
my question how can bind application closing event (click app cross) function in code below? how can called when application quit?
import sys pyqt5.qtwidgets import (qwidget, qtooltip, qpushbutton, qapplication, qmessagebox) pyqt5.qtgui import qfont def closeevent(event): reply = qmessagebox.question(w, 'message', "are sure quit?", qmessagebox.yes | qmessagebox.no, qmessagebox.no) if reply == qmessagebox.yes: event.accept() else: event.ignore() app = qapplication(sys.argv) w = qwidget() w.setgeometry(300, 300, 300, 200) w.setwindowtitle('procedural event binding - pyqt') w.show() sys.exit(app.exec_())
thanks time.
you overwrite existing closeevent
of widget:
w.closeevent = closeevent
and work in example.
if want bound method, can do:
import types ... def closeevent(self, event): reply = qmessagebox.question(self, 'message', ... w.closeevent = types.methodtype(closeevent, w)