PySide / PyQt detect if user trying to close window

Override the closeEvent method of QWidget in your main window. For example: class MainWindow(QWidget): # or QMainWindow … def closeEvent(self, event): # do stuff if can_exit: event.accept() # let the window close else: event.ignore() Another possibility is to use the QApplication‘s aboutToQuit signal like this: app = QApplication(sys.argv) app.aboutToQuit.connect(myExitHandler) # myExitHandler is a callable