How to display message box in PyQt5?

You can display message boxes in PyQt5 using the QMessageBox class. Here is a simple example:

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

class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Message box example')

        self.showMessageBox()

    def showMessageBox(self):
        QMessageBox.information(self, 'Information', 'This is an information message box')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

In this example, we have created a basic window and displayed an information message box within it. Different types of message boxes can be shown by calling the static methods of QMessageBox, such as information, question, and warning.

Leave a Reply 0

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