pyqt5 message box

When creating a Python GUI, you may want to show a message box at some point.

Pyqt comes with messagebox support in both PyQt4 and PyQt5. The class to be used is QMessageBox.

In this tutorial you’ll learn how to show a message box on buton click.

qt message box

Related course:

QMessageBox example

Import QMessageBox from the PyQt5 widgets

from PyQt5.QtWidgets import QMessageBox

A messagebox can easily be added to the window using the code:

QMessageBox.about(self, "Title", "Message")

Complete code:

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtCore import QSize    
class MainWindow(QMainWindow):
    def __init__(self):
        self.setMinimumSize(QSize(300, 200))    
        self.setWindowTitle("PyQt messagebox example -") 
        pybutton = QPushButton('Show messagebox', self)
        pybutton.move(50, 50)        
    def clickMethod(self):
        QMessageBox.about(self, "Title", "Message")

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    sys.exit( app.exec_() )

Previous Post
Next Post

Leave a Reply

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