How to Add Image in Python Applications

In this Python tutorial we want to learn How to Add Image in Python Applications, when you want to add an image in Python, than you will need a third party library, for this we are going to use PyQt6 library, so PyQt6 is popular Python library for building GUI applications.

 

 

First of all you need to install PyQt6 library and you can use pip for the installation.

 

 

After that we creates a class that extends from the QWidget class. This class will serve as our main application window, where we want to display the image.

In the MainWindow class, we override __init__ method to perform some initial setup. we set the window title and dimensions using setWindowTitle and setGeometry methods.

We also creates a QLabel widget called label and position it at coordinates (50, 50) with a width of 300 and height of 200 pixels. This label will display the image.

 

 

 

For loading and displaying the image inside the application, we need to convert the image file into a QPixmap object and set it as the pixmap for the QLabel widget.

In the above code we creates a QPixmap object called pixmap by passing the image file path to it. after that we set the pixmap as the image for the QLabel widget using the setPixmap method.

 

 

For runing the application, we need to create an instance of the QApplication class and pass the command line arguments to it. we also creates an instance of the MainWindow class and make it visible using the show method.

 

 

 

This is the complete code for this article

 

 

 

 

Run the complete code and this will be the result

How to Add Image in Python Applications
How to Add Image in Python Applications

 

 

 

Learn More on Python GUI

Leave a Comment