PyQt5 - Message Box And Load Image Source Example

Introduction

 
In this article, we are discussing how to create PyQt5 Message Box and how we load the image in labels from our local directory with source example.
 
Prerequisite
 
Python IDLE 3.7
 

PyQt5 - Message Box

 
The PyQt5 message dialog is used to provide warning information to ask users to respond by clicking any one of the standard buttons on it.
 
Source example
  1. import sys  
  2. from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox  
  3.   
  4. class App(QWidget):  
  5.   
  6.     def __init__(self):  
  7.         super().__init__()  
  8.         self.title = 'PyQt5 messagebox - C# corner'  
  9.         self.left = 40  
  10.         self.top = 80  
  11.         self.width = 420  
  12.         self.height = 200  
  13.         self.messagebox()  
  14.           
  15.     def messagebox(self):  
  16.         self.setWindowTitle(self.title)  
  17.         self.setGeometry(self.left, self.top, self.width, self.height)  
  18.   
  19.         Reply = QMessageBox.question(self'PyQt5 message'"Do you like PyQt5?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)  
  20.         if Reply == QMessageBox.Yes:  
  21.             print('Yes clicked.')  
  22.         else:  
  23.             print('No clicked.')  
  24.   
  25.         self.show()  
  26.           
  27. if __name__ == '__main__':  
  28.     app = QApplication(sys.argv)  
  29.     ex = App()  
  30.     sys.exit(app.exec_()) 
Source descriptions
  • First of all, we need to import PyQt5 packages for handling application window, push-button, and Message Box from the PyQt5 Widgets.
  • The function initiation has carried the basic configurations values of building Windows such as title, X, and Y coordination.
  • Next, the “messagebox()” function performs to handle the window along with their desired values.
  • The Replay variable contains the message box question body of the message box and is followed by the action Yes or No triggers. Then the if the condition will take action according to user response (if the user clicks” Yes” then the dialog will print Yes clicked or if they click “no” then it will display No clicked.
Output
 
 

PyQt5 - Load image from local directory

 
Int his section, we are discussing how we load an "image" from our local system to display in the Label widget.
 
Source example
  1. import sys  
  2. from PyQt5.QtWidgets import QApplication,QWidget,QLabel  
  3. from PyQt5.QtGui import QIcon,QPixmap  
  4. import os  
  5. os.chdir('F:\\Demo')  
  6. class App(QWidget):  
  7.     def __init__(self):  
  8.         super().__init__()  
  9.         self.title='Hello, world!'  
  10.         self.left=10  
  11.         self.top=70  
  12.         self.width=640  
  13.         self.height=480  
  14.         self.initUI()  
  15.     def initUI(self):  
  16.         self.setWindowTitle(self.title)  
  17.         self.setGeometry(self.left,self.top,self.width,self.height)  
  18.         label=QLabel(self)  
  19.         pixmap=QPixmap('python-img.jpg')  
  20.         label.setPixmap(pixmap)  
  21.         self.resize(pixmap.width(),pixmap.height())  
  22.         self.show()  
  23. if __name__=='__main__':  
  24.     app=QApplication(sys.argv)  
  25.     ex=App() 

Source descriptions

  • Let’s begin by importing some necessary packages from PyQt5 widgets for handling Window and label box widgets.
  • The chgdir() method is used to navigate the directory for loading a picture from the local system to our applications.
  • The "initial()" function can be determining the window configurations. Then the "intUi()" function performs to configure window title and geometry positions of the app window.
  • Then create a label (QLabel) for displaying images. Next, create a pixmap (QPixmap) widget for holding images from our previously created directory.
  • Next, the "resize()" method used to resize the image. Then the "show()" method will display the window with our desired image.
Output
 
 

Summary

 
Here, we have learned how the message box works and how we can load images from our local directory for building Python applications.
 
References
  • https://www.tutorialspoint.com/pyqt/pyqt_qmessagebox.htm
  • https://pythonspot.com/pyqt5-messagebox
    https://pythonspot.com/pyqt5-image
    https://www.programcreek.com/python/example/106694/PyQt5.QtGui.QImage


Recommended Free Ebook
Similar Articles