Adding an Image in PyQt5 Using Qt Designer and Python

Posted by


In PyQt5, you can easily add an image to your GUI using Qt Designer. Qt Designer is a powerful tool that allows you to design and create UIs with ease. In this tutorial, I will show you how to add an image to your PyQt5 application using Qt Designer.

Step 1: Create a new PyQt5 application

First, you need to create a new PyQt5 application. You can do this by creating a new Python file and importing the necessary libraries:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

Next, create a new class that inherits from QMainWindow:

class MyApp(QMainWindow):
    def __init__(self):
        super().__init__()

Inside the __init__ function, create a new instance of QApplication and set up the window:

        self.setWindowTitle('Image Example')
        self.setGeometry(100, 100, 800, 600)

Step 2: Using Qt Designer to add an image

Next, open Qt Designer and create a new form. Drag and drop a QLabel widget onto the form. This will be used to display the image.

Right-click on the QLabel widget and select Promote to… In the Promote to dialog, enter QLabel in the Base class name and the header file your QLabel Class.

Now, save the form as a .ui file.

Step 3: Converting .ui file to .py file

Open a terminal and run the following command to convert the .ui file to a .py file:

pyuic5 -x your_form.ui -o your_form.py

Step 4: Adding image to the QLabel widget

Open the generated .py file and locate the setupUi function. Inside this function, you can set the image to be displayed by setting the pixmap property of the QLabel widget:

from PyQt5.QtGui import QPixmap

self.label.setPixmap(QPixmap('path_to_your_image.jpg'))

Make sure to replace ‘path_to_your_image.jpg’ with the actual path to your image file.

Step 5: Connecting the PyQt5 application with the .ui file

Finally, in your main Python file, import the generated .py file and use it to set up the UI:

from your_form import Ui_MainWindow

class MyApp(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

Run your PyQt5 application, and you should see the image displayed in the QLabel widget.

That’s it! You have successfully added an image to your PyQt5 application using Qt Designer. I hope this tutorial was helpful, and you can now easily add images to your PyQt5 GUIs.

0 0 votes
Article Rating
36 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@PikaPikaKatzy
3 months ago

Could not create pixmap from (the path)

@user-xk5it1qg5z
3 months ago

Bro you didn’t explain more

@frdnztrk
3 months ago

thanks bro

@medjimedji9193
3 months ago

Excellente explications. Merci pour cette vidéo très utile. 😊

@pielumax3301
3 months ago

In my stylesheet, it doesn't have that pencil shows up when I was trying to add image to my resource…..anyone know what happened?

@Juicebox_x
3 months ago

Man, three years later, and this video is just amazing and extremely helpful and straight to the point. Thank you so much sir!!

@omargutierrez7660
3 months ago

This was very helpful, thanks mate

@wistyroamlands7495
3 months ago

I know this video is 3 years old but that IS a beautiful log in page, good job. Thank you so much for making this. It's a kindness to the world and I'm glad that after the kids go to bed, I can work on my hobby application that roles probabilities for physical character features from two parents, which makes our text based roleplays more interesting. 😀 Because of this video I got the lovely background I made in clip paint into my main menu 😀

@alishshrestha5422
3 months ago

How to resize the window

@gul_7days
3 months ago

Good tutorial man! Thank you!

@yurikauanflores6297
3 months ago

é 22 porr

@praiselikwanya6454
3 months ago

simple and straight forward. thanks for the help bru

@siberdeyiz
3 months ago

adamsın abi

@fertuht7654
3 months ago

Help you so much!

@user-zt1iu3we6p
3 months ago

Thank you very much~!!!!!!!!!!!!!!!!!!!!!!!!

@MrRONE777
3 months ago

awesome. thanks

@victor.alvesilva
3 months ago

Obrigado. De: Brasil

@AzizChandoul
3 months ago

Hello Mr
I need help. I create a project with Qt designer , but I didn't know how to exporte thé project to be a executed file ". Exe"
Please I need help 😭😭

@juanvillagran2294
3 months ago

how can i make buttons from png image or icons?

@techietutions448
3 months ago

Thanku So much sir