PyQt5 messagebox QMessageBox

by Atakan

In applications, we may sometimes want to show messages such as questions, warnings, information or errors with users.
This is a very common situation.
In Python, the QMessageBox class in the pyqt library can help our needs for this kind of work.

Let’s talk about some necessary methods.

setIcon() 

This method specifies the type with the parameter it will take.

  • QMessageBox.Question
  • QMessageBox.Information
  • QMessageBox.Warning
  • QMessageBox.Critical
setText() 

It is an informative message written in the middle of the message box.

setWindowTitle() 

Information to be written in the header section above the message box.

setStandardButtons() 

The button types to be added here are used to capture the return.

  • QMessageBox.Ok
  • QMessageBox.Cancel
  • QMessageBox.Yes
  • QMessageBox.No
  • etc ..

Let’s make an example.
I want to create a message box asking a question. With yes and no options. For this

    def question(self):
        msgbox = QMessageBox()
        msgbox.setIcon(QMessageBox.Question)
        msgbox.setText("Message box pop up window")
        msgbox.setWindowTitle("www.langpy.com | QMessageBox")
        msgbox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)

        #You have to call the exec method.
        result = msgbox.exec()
        if result == QMessageBox.Yes:
            print('Yes button pushed')
        elif result == QMessageBox.No:
            print('No button pushed')
Result:

Full code

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
import sys

W_WIDTH = 400
W_HEIGHT = 250
W_X = 400
W_Y = 150


# button creator class
class CreateButton:
    def __init__(self, window, text, width, height, x, y):
        self.button = QtWidgets.QPushButton(window)
        self.button.setText(text)
        self.button.resize(width, height)
        self.button.move(x, y)

    def get_instance(self):
        return self.button


class MainFrame(QMainWindow):
    def __init__(self):
        super(MainFrame, self).__init__()
        self.init()
        self.show()

    def question(self):
        msgbox = QMessageBox()
        msgbox.setIcon(QMessageBox.Question)
        msgbox.setText("Middle area of message..")
        msgbox.setWindowTitle("www.langpy.com | Example MessageBox")
        msgbox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)

        #You have to call the exec method.
        result = msgbox.exec()
        if result == QMessageBox.Yes:
            print('Yes button pushed')
        elif result == QMessageBox.No:
            print('No button pushed')


    def init(self):
        self.resize(W_WIDTH, W_HEIGHT)
        self.move(W_X, W_Y)
        self.setWindowTitle("www.langpy.com | Python GUI Tutorial")

        self.button_question = CreateButton(self, "Question Button", 120, 40, 10, 10).get_instance()

        self.button_question.clicked.connect(self.question)


app = QApplication(sys.argv)
win = MainFrame()
sys.exit(app.exec_())

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. OK Read More