In this PyQt5 Tutorial we are going to learn about Create Python GUI Applications with Qt, when you want to build GUI applications with Python, there are different and powerful libraries that you can use for building Python GUI Application, one of them are PyQt5,
What is PyQt5?
PyQt5 is Python module that allows you to create desktop applications with graphical user interfaces (GUI) using Qt framework. Qt framework is powerful and widely used C++ toolkit for building cross platform applications with native look and feel. In this tutorial we will learn how to use PyQt5 to create Python GUI applications with Qt.
How to Install PyQt5?
Before we start building our python applications with PyQt5, we need to install the module. you can install it using pip by running this command in your command prompt:
1 |
pip install PyQt5 |
PyQt5 Tutorial – Build Basic GUI Application
For creating basic PyQt5 application, first of all we need to import necessary modules and create QApplication object, QApplication manages the application’s event loop and handles user input. we also need to create QMainWindow object which serves as the main window for our application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import sys from PyQt5.QtWidgets import QApplication, QMainWindow #create QApplication object app = QApplication(sys.argv) #create your QMainWindow instance window = QMainWindow() #show the window window.show() #start the loop sys.exit(app.exec_()) |
This code creates basic application with main window that is initially hidden. the sys.exit() function is used to exit the application when the user closes the window.
Run the code and this will be the result
Adding Widgets to PyQt5 Window
Now that we have basic python gui application window, we can add widgets to it. PyQt5 provides different widgets that we can use to create our user interface. this is an example code to add label widget to the main window:
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 |
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel from PyQt5.QtGui import QIcon #create QApplication instance app = QApplication(sys.argv) #create QMainWindow instance window = QMainWindow() #set the window title window.setWindowTitle('Codeloop') window.setWindowIcon(QIcon("icon.png")) #set the geometry of the window (x,y,width and height) window.setGeometry(100, 100, 400, 300) #create a label label = QLabel('Codeloop.org', window) #move the label label.move(50, 50) #show the window window.show() #start the loop sys.exit(app.exec_()) |
This code creates label widget with the text “Codeloop.org” and adds it to the main window at the position (50, 50). setWindowTitle() function is used to set the title of the main window, and the setGeometry() function is used to set its size and position.
Run the complete code and this will be the result.
Handling User Events in PyQt5 (Signals & Slots)
In PyQt5 we can handle user events such as button clicks or keyboard presses using signals and slots. Signals are emitted when particular event occurs and slots are functions that are executed in response to these signals. this is an example code to handle button clicks using signals and slots:
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 |
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel from PyQt5.QtGui import QIcon # Initialize the PyQt application app = QApplication(sys.argv) # Create the main window window = QMainWindow() # Set window title window.setWindowTitle('Codeloop.org') # Set window icon window.setWindowIcon(QIcon("codeloop.png")) # Set window geometry (position and size) window.setGeometry(100, 100, 400, 300) # Create a button in the window button = QPushButton('Click Me', window) button.move(50, 50) # Set button position # Create a label in the window with initial text label = QLabel('Initial Text', window) label.move(50, 100) # Set label position # Function to be called when the button is clicked def button_clicked(): # Change label text when button is clicked label.setText('Welcome to codeloop.org') # Connect the button "clicked" signal to the # function button_clicked button.clicked.connect(button_clicked) # Show the main window window.show() # Start the application event loop sys.exit(app.exec_()) |
This code creates button widget with the text “Click Me” and adds it to the main window at the position (50, 50). button_clicked() function is executed when the button is clicked and it show a text in the label. clicked signal of the button is connected to the button_clicked() slot using the connect() function.
Run the code and this will be the result
Layout Management in PyQt5
Now let’s integrate layout management in our example, Layout management in PyQt helps in arranging widgets inside window dynamically. Instead of explicitly setting the position of each widget, you add them to layouts, and the layout manager takes care of their positioning and resizing based on predefined rules.
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 |
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QVBoxLayout, QWidget from PyQt5.QtGui import QIcon # Initialize the PyQt application app = QApplication(sys.argv) # Create the main window window = QMainWindow() # Set window title window.setWindowTitle('Codeloop') # Set window icon window.setWindowIcon(QIcon("codeloop.png")) # Set window geometry (position and size) window.setGeometry(100, 100, 400, 300) # Create a central widget to hold the layout central_widget = QWidget() window.setCentralWidget(central_widget) # Create a vertical layout to arrange widgets vertically layout = QVBoxLayout(central_widget) # Create a button and label button = QPushButton('Click Me') label = QLabel('Initial Text') # Add the button and label to the layout layout.addWidget(button) layout.addWidget(label) # Function to be called when the button is clicked def button_clicked(): # Change label text when button is clicked label.setText('Welcome to codeloop.org') # Connect the button's "clicked" signal to # the function button_clicked button.clicked.connect(button_clicked) # Show the main window window.show() # Start the application event loop sys.exit(app.exec_()) |
In this modified example:
- We have created QWidget as the central widget of the QMainWindow.
- We have created QVBoxLayout to arrange widgets vertically.
- Both the button and label are added to the layout.
- The layout takes care of positioning and resizing the widgets.
- When the button is clicked, the label text changes as before.
Run the code and this will be the result
In this article we have learned how to use PyQt5 to create Python GUI applications with Qt. we have started by installing the module and creating basic application window. after that we added widgets to the window and learned how to handle user events using signals and slots, also we have talked about layout management.
Subscribe and Get Free Video Courses & Articles in your Email