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.
Could not create pixmap from (the path)
Bro you didn’t explain more
thanks bro
Excellente explications. Merci pour cette vidéo très utile. 😊
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?
Man, three years later, and this video is just amazing and extremely helpful and straight to the point. Thank you so much sir!!
This was very helpful, thanks mate
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 😀
How to resize the window
Good tutorial man! Thank you!
é 22 porr
simple and straight forward. thanks for the help bru
adamsın abi
Help you so much!
Thank you very much~!!!!!!!!!!!!!!!!!!!!!!!!
awesome. thanks
Obrigado. De: Brasil
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 😭😭
how can i make buttons from png image or icons?
Thanku So much sir