In this PyQt5 Tutorial for Beginners we are going to Learn PyQt5 from Scratch in 2023, PyQt5 is cross platform GUI library for Python, which is based on popular C++ toolkit Qt. It allows you to build powerful and customized desktop applications using Python. In this tutorial we will cover the basics of PyQt5 and provide step by step guide for beginners to learn PyQt5 from scratch.
Install PyQt5
Before we start of PyQt5 Tutorial we need to install PyQt5. You can install PyQt5 using pip, which is the package installer for Python. Open your command prompt or terminal and type the following command:
1 |
pip install PyQt5 |
Creating a Window
For creating basic window using PyQt5, we need to import the QtWidgets module and create QApplication and QMainWindow object. this is an example:
1 2 3 4 5 6 7 |
from PyQt5 import QtWidgets app = QtWidgets.QApplication([]) window = QtWidgets.QMainWindow() window.show() app.exec_() |
In the above example first of all we have imported QtWidgets module, which contains all the widgets for building GUIs. after that we have created QApplication object, which is required for any PyQt5 application. and finally we created QMainWindow object and display it using the show() method. The app.exec_() method is used to start the main event loop of the application.
Run the complete code and this will be the result
Adding Widgets
Widgets are building blocks of any PyQt5 application. they are visual elements that make up the user interface. in PyQt5, widgets are created using classes. this is is an example of how to create QLabel widget and add it to a window:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from PyQt5 import QtWidgets #created QApplication object app = QtWidgets.QApplication([]) #created QMainWindow object window = QtWidgets.QMainWindow() #created QLabel object label = QtWidgets.QLabel("Hello GeeksCoders.com") #set the label in the centeral of the window window.setCentralWidget(label) #show the window window.show() app.exec_() |
In the above example we have created QLabel widget with the text “Hello GeeksCoders.com”. after that we set the label as central widget of the window using setCentralWidget() method. central widget is the main widget of the window and takes up the entire area of the window.
Run the complete code and this will be the result
Connecting Signals and Slots
Signals and slots are used to communicate between widgets in a PyQt5 application. signal is emitted by widget when particular event occurs, such as button being clicked. slot is function that is called in response to a signal. this is is an example of how to connect button click signal to slot:
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 |
from PyQt5 import QtWidgets #created QApplication object app = QtWidgets.QApplication([]) #created QMainWindow object window = QtWidgets.QMainWindow() #created QLabel object button = QtWidgets.QPushButton("Click me") #set the label in the centeral of the window window.setCentralWidget(button) #this is our slot or function def on_button_click(): print("Button clicked!") #in here we have connected the clicked signal with the slot button.clicked.connect(on_button_click) #show the window window.show() app.exec_() |
In the above example we have created QPushButton widget with the text “Click me”. after that we set the button as central widget of the window. we have defined function called on_button_click() that prints “Button clicked” to the console. and finally we have connected clicked signal of the button to the on_button_click() slot using clicked.connect() method.
Run the complete code and this will be the result
We have created some basic window using PyQt5, now let’s add some features and talk about PyQt5 Layout Management, Layout management is an essential aspect of building PyQt5 GUIs. It helps you to arrange and position widgets in a way that looks visually appealing and is easy to use. in this article we are going to cover different types of layout managers available in PyQt5 and provide step by step guide for using them in your GUIs.
Types of Layout Managers
There are three types of layout managers available in PyQt5:
1. QVBoxLayout and QHBoxLayout
QVBoxLayout and QHBoxLayout are two of the most commonly used layout managers in PyQt5. QVBoxLayout arranges widgets in vertical column, while QHBoxLayout arranges them in horizontal row.
This is an example of how to use QVBoxLayout to arrange two labels vertically:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
from PyQt5 import QtWidgets app = QtWidgets.QApplication([]) window = QtWidgets.QMainWindow() layout = QtWidgets.QVBoxLayout() label1 = QtWidgets.QLabel("GeeksCoders One") label2 = QtWidgets.QLabel("GeeksCoders Two") layout.addWidget(label1) layout.addWidget(label2) window.setCentralWidget(QtWidgets.QWidget()) window.centralWidget().setLayout(layout) window.show() app.exec_() |
In the above example, we have create QVBoxLayout object and added two QLabel widgets to it using the addWidget() method. after that we set central widget of the window to new QWidget object and set the layout of the QWidget to QVBoxLayout object.
Run the complete code and this will be the result
2. QGridLayout
QGridLayout arranges widgets in grid of rows and columns. It is useful when you want to align widgets in specific pattern.
This is an example of how to use QGridLayout to arrange two buttons in a grid:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
from PyQt5 import QtWidgets app = QtWidgets.QApplication([]) window = QtWidgets.QMainWindow() layout = QtWidgets.QGridLayout() button1 = QtWidgets.QPushButton("Button 1") button2 = QtWidgets.QPushButton("Button 2") layout.addWidget(button1, 0, 0) layout.addWidget(button2, 0, 1) window.setCentralWidget(QtWidgets.QWidget()) window.centralWidget().setLayout(layout) window.show() app.exec_() |
In the above example we have created QGridLayout object and add two QPushButton widgets to it using addWidget() method. first argument of the addWidget() method specifies the widget to be added, while the second and third arguments specify the row and column of the grid.
Run the complete code and this will be the result
3. QFormLayout
QFormLayout arranges widgets in two column layout, with labels on the left and widgets on the right. It is useful for creating forms and input dialogs.
This is an example of how to use QFormLayout to create form:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from PyQt5 import QtWidgets app = QtWidgets.QApplication([]) window = QtWidgets.QMainWindow() layout = QtWidgets.QFormLayout() name_label = QtWidgets.QLabel("Name:") name_edit = QtWidgets.QLineEdit() layout.addRow(name_label, name_edit) email_label = QtWidgets.QLabel("Email:") email_edit = QtWidgets.QLineEdit() layout.addRow(email_label, email_edit) window.setCentralWidget(QtWidgets.QWidget()) window.centralWidget().setLayout(layout) window.show() app.exec_() |
In the above example, we have created QFormLayout object and add two rows to it using addRow() method. Each row consists of QLabel widget on the left and QLineEdit widget on the right.
Run the complete code and this will be the result
Final Thoughts
PyQt5 is powerful GUI library for Python that allows you to build desktop applications easily. In this tutorial we covered basics of PyQt5, including creating window, adding widgets, connecting signals and slots and layout management. with these skills, you can start building your own custom desktop applications using PyQt5.
Learn More on TKinter
- How to Create Conutdown Timer with Python & TKinter
- Create GUI Applications with Python & TKinter
- Python TKinter Layout Management
- How to Create Label in TKinter
- How to Create Buttin in Python TKinter
- Build Music Player in Python TKinter
- Python GUI Programming with TKinter
- TKinter VS PyQt, Which one is Good
- Creating Custom Widgets in TKinter