Top 10 Programming Languages to Learn in 2019

PyQt5Python GUI

PyQt5 – Message Box Widget

pyqt5-message-box-widget

PyQt5 is a set of Python bindings for the cross-platform application framework that combines all the advantages of Qt and Python. With PyQt5, you can include Qt libraries in Python code, enabling you to write GUI applications in Python. In other words, PyQt allows you to access all the facilities provided by Qt through Python code. Since PyQt5 depends on the Qt libraries to run, when you install PyQt5, the required version of Qt is also installed automatically on your machine.

Before starting please ensure that you have installed PyQt5 or install it by running this command.

sudo pip3 install PyQt5

It will install PyQt5 libraries as well as the PyQt5-sip library.

PyQt5 Message Box

Let’s start by importing all the required libraries and classes.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

The PyQt5 QMessageBox is the class that we need to display the message box. Next, we will make a MessageBox class and override its constructor function.

class MessageBox(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 messagebox - pyblog.in'
        self.left = 10
        self.top = 10
        self.height = 400
        self.width = 640
        self.initUI()
        
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        buttonReply = QMessageBox.question(self, 'PyQt5 message box', "Do you like PyQt5?", QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, QMessageBox.No)
        if buttonReply == QMessageBox.Yes:
            print("Yes clicked")
        elif buttonReply == QMessageBox.Cancel:
            print("Cancel Clicked")
        else:
            print("No clicked")

        self.show()

here as you can see that we have implemented various outcomes depending upon the output of the MessageBox. Lastly we will make an object of the class and run the file.

if __name__ == '__main__':
    app = QApplication(sys.argv)
    message = MessageBox()
    sys.exit(app.exec_())  

Now we run the file using.

python3 message.py

Output

pyqt5-message-widget
pyqt5-message-widget
pyqt5-message-widget-2
pyqt5-message-widget-2

Other PyQt5 MessageBox Classes

QMessageBox.CancelQMessageBox.OkQMessageBox.Help
QMessageBox.OpenQMessageBox.SaveQMessageBox.SaveAll
QMessageBox.DiscardQMessageBox.CloseQMessageBox.Apply
QMessageBox.ResetQMessageBox.YesQMessageBox.YesToAll
QMessageBox.NoQMessageBox.NoToAllQMessageBox.NoButton
QMessageBox.RestoreDefaultsQMessageBox.AbortQMessageBox.Retry
QMessageBox.Ignore

How it worked?

  • It imports the necessary modules. QWidget is the base class of all user interface objects in PyQt5.
  • It creates a new MessageBox class that inherits from the base class, QWidget.
  • It provides the default constructor for QWidget. The default constructor has no parent, and a widget with no parent is known as a window.
  • It creates an application object with the name app through the QApplication() method. Every PyQt5 application must create sys.argv application object which contains a list of arguments from the command line, and it is passed to the method while creating the application object. The sys.argv parameter helps in passing and controlling the startup attributes of a script.
  • An instance of the MessageBox class is created with the name window.
  • The MessageBox gets activated first and asks for our opinion.
  • Now based on the response provided further actions occurs.
  • The show() method will display the widget on the screen.
  • The sys.exit() method ensures a clean exit, releasing memory resources.

Please Note that The exec_() method has an underscore because exec is a Python keyword.

Code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class MessageBox(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 messagebox - pyblog.in'
        self.left = 10
        self.top = 10
        self.height = 400
        self.width = 640
        self.initUI()
        
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        buttonReply = QMessageBox.question(self, 'PyQt5 message box', "Do you like PyQt5?", QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, QMessageBox.No)
        if buttonReply == QMessageBox.Yes:
            print("Yes clicked")
        elif buttonReply == QMessageBox.Cancel:
            print("Cancel Clicked")
        else:
            print("No clicked")

        self.show()
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    message = MessageBox()
    sys.exit(app.exec_())  
Related posts
PyQt5Python GUI

PyQt5 - PushButton

PyQt5Python GUI

PyQt5 - Window Widget

Python GUI

Start Python GUI Programming with Tkinter

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.