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. class App(QWidget):
  4. def __init__(self):
  5. super().__init__()
  6. self.title = 'PyQt5 messagebox - C# corner'
  7. self.left = 40
  8. self.top = 80
  9. self.width = 420
  10. self.height = 200
  11. self.messagebox()
  12. def messagebox(self):
  13. self.setWindowTitle(self.title)
  14. self.setGeometry(self.left, self.top, self.width, self.height)
  15. Reply = QMessageBox.question(self, 'PyQt5 message', "Do you like PyQt5?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  16. if Reply == QMessageBox.Yes:
  17. print('Yes clicked.')
  18. else:
  19. print('No clicked.')
  20. self.show()
  21. if __name__ == '__main__':
  22. app = QApplication(sys.argv)
  23. ex = App()
  24. sys.exit(app.exec_())
Source descriptions
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

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