In this article we want to learn How to Add Text and Image in PyQt5 Label, first of all let’s talk about PyQt5. so PyQt5 is popular Python library for creating desktop applications, and Label is one of the important widget in PyQt5 that we can use for displaying text or adding images. even you can use PyQt5 label for displaying GIF images.
First of all if you have not installed PyQt5, you need to install and you can use pip for that.
1 |
pip install PyQt5 |
To start, we need to import the required modules from PyQt5, these are our imports.
1 2 3 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel from PyQt5.QtGui import QPixmap import sys |
After that we need to create an instance of the QApplication class to manage the GUI event loop. and than we creates a QMainWindow instance as the main window container, also if you see we have added some methods like setWindowTitle(), it is used for setting the window title, also setGeometry(), it is used for setting the x,y, width and height of the window.
1 2 3 4 5 6 7 8 9 |
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Codeloop.org - Label Example") self.setWindowIcon(QIcon('codeloop.png')) self.setGeometry(100, 100, 400, 300) app = QApplication(sys.argv) window = MainWindow() |
This code is used for creating the label and setting the x,y,width and height of the window.
1 2 |
self.label_text = QLabel("Hello Codeloop.org", self) self.label_text.setGeometry(100, 50, 200, 30) |
For displaying an image, we need to use QPixmap class from the QtGui module. this is an example that how you can add an image to QLabel:
1 2 3 4 |
self.label_image = QLabel(self) self.label_image.setGeometry(100, 100, 500, 500) pixmap = QPixmap("python.png") self.label_image.setPixmap(pixmap) |
For displaying the main window and start the application’s event loop, you can use this code.
1 2 |
window.show() sys.exit(app.exec_()) |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel from PyQt5.QtGui import QPixmap, QIcon import sys class MainWindow(QMainWindow): def __init__(self): # Initialize the superclass (QMainWindow) super().__init__() # Set the window title self.setWindowTitle("Codeloop.org - Label Example") # Set the window icon using an image file self.setWindowIcon(QIcon('codeloop.png')) # Set the window's position and size self.setGeometry(100, 100, 400, 300) # Create a QLabel to display text self.label_text = QLabel("Hello Codeloop.org", self) # Set the position and size of the text label self.label_text.setGeometry(100, 50, 200, 30) # Create a QLabel to display an image self.label_image = QLabel(self) # Set the position and size of the image label self.label_image.setGeometry(100, 100, 500, 500) # Load the image file into a QPixmap object pixmap = QPixmap("python.png") # Set the QPixmap object to the image label self.label_image.setPixmap(pixmap) if __name__ == "__main__": # Create a QApplication object app = QApplication(sys.argv) # Create an instance of the MainWindow class window = MainWindow() # Show the main window window.show() # Start the application's event loop sys.exit(app.exec_()) |
Run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email