Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prompt on exit in PyQt application

Tags:

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

I'm using PyQt.

like image 717
Kirill Titov Avatar asked Sep 12 '09 10:09

Kirill Titov


People also ask

How do I close a PyQt window?

The simplest way to close a window is to click the right (Windows) or left (macOS) 'X' button on the title bar.

Why use PySide instead of PyQt?

Advantages of PySide PySide represents the official set of Python bindings backed up by the Qt Company. PySide comes with a license under the LGPL, meaning it is simpler to incorporate into commercial projects when compared with PyQt. It allows the programmer to use QtQuick or QML to establish the user interface.

What is dialog in PyQt?

Dialogs are useful GUI components that allow you to communicate with the user (hence the name dialog). They are commonly used for file Open/Save, settings, preferences, or for functions that do not fit into the main UI of the application.


1 Answers

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

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.

like image 191
ire_and_curses Avatar answered Nov 08 '22 09:11

ire_and_curses