In this tutorial, we're going to cover how to create a pop up window for messages like warnings to the user in PyQT.
There may be all sorts of reasons why you may want to warn the user regarding an action they are trying to make. We're going to add a sort of "are you sure you want to exit" message when the user attempts to quit our application. Maybe the user just miss-clicked and doesn't actually want to exit. Maybe the user is trying to delete the entire database of their contacts. There are many reasons why we may want to double check that the user isn't messing up.
Since we are going to have this message pop up when the user tries to exit, let's put this code in the close_application method:
def close_application(self): choice = QtGui.QMessageBox.question(self, 'Extract!', "Get into the chopper?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) if choice == QtGui.QMessageBox.Yes: print("Extracting Naaaaaaoooww!!!!") sys.exit() else: pass
First, we use the QtGui.QMessageBox.question
. This is a pre-made message box window that comes with PyQT. There are many message boxes, this question version comes with a question mark image to denote that the window is a question. There are exclamation point versions, and so on.
The parameters here are self, window title, window message, and then finalyl the window options. Here, the | denotes an either-or situation. Only one can be chosen.
After that, we have some simple if else logic that checks to see what the user picked. If they said no, then we just pass and the application resumes. If the user choses yes, meaning they really did want to quit, we go ahead and issue the sys.exit as before.
The code all put together:
import sys from PyQt4 import QtGui, QtCore class Window(QtGui.QMainWindow): def __init__(self): super(Window, self).__init__() self.setGeometry(50, 50, 500, 300) self.setWindowTitle("PyQT tuts!") self.setWindowIcon(QtGui.QIcon('pythonlogo.png')) extractAction = QtGui.QAction("&GET TO THE CHOPPAH!!!", self) extractAction.setShortcut("Ctrl+Q") extractAction.setStatusTip('Leave The App') extractAction.triggered.connect(self.close_application) self.statusBar() mainMenu = self.menuBar() fileMenu = mainMenu.addMenu('&File') fileMenu.addAction(extractAction) self.home() def home(self): btn = QtGui.QPushButton("Quit", self) btn.clicked.connect(self.close_application) btn.resize(btn.minimumSizeHint()) btn.move(0,100) extractAction = QtGui.QAction(QtGui.QIcon('todachoppa.png'), 'Flee the Scene', self) extractAction.triggered.connect(self.close_application) self.toolBar = self.addToolBar("Extraction") self.toolBar.addAction(extractAction) self.show() def close_application(self): choice = QtGui.QMessageBox.question(self, 'Extract!', "Get into the chopper?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) if choice == QtGui.QMessageBox.Yes: print("Extracting Naaaaaaoooww!!!!") sys.exit() else: pass def run(): app = QtGui.QApplication(sys.argv) GUI = Window() sys.exit(app.exec_()) run()
The result is:
In the next tutorial, we'll talk about adding a toggle-able check box.