PyQt5 Tutorial for Beginners

Welcome to PyQt5 Tutorial for Beginners, in this complete article we want to learn PyQt5 from basic up to advanced topics. You will learn different concepts on this article, from installation of PyQt5 up to building different widgets in PyQt5.

 

 

 

What is PyQt5 ?

PyQt5 is a comprehensive set of Python bindings for Qt v5. It is implemented as more than 35 extension modules and enables Python to be used as an alternative application development language to C++ on all supported platforms including iOS and Android. PyQt5 is used to write all kinds of GUI applications, from accounting applications, to visualization tools used by scientists and engineers. and Qt is set of cross-platform C++ libraries that implement high-level APIs for accessing many aspects of modern desktop and mobile systems. These includes location and positioning services, multimedia, NFC and Bluetooth connectivity, a Chromium based web browser, as well as traditional UI development.

 

 

PyQt5 Installation

You an simply use pip for the installation.

 

 

How to Create Window in PyQt5

OK now after installation we are going to create our first window in PyQt5, now this is the complete code for creating window in PyQt5.

You can see in the above code we have created a class that extends from QWidget, now there are three types of window class that you can use. 

  • QWidget: The QWidget class is the base class of all user interface
    objects, The widget is the important point of the user interface:
    it receives mouse, keyboard and other events from the window
    system, and paints a representation of itself on the screen.
  • QDialog: The QDialog class is the base class of dialog window
    and A dialog window is a top-level window mostly used for
    short-term tasks and brief communications with the user.
    QDialogs may be modal or modeless.
  • QMainWindow: The QMainWindow class provides a main application window
    A main window provides a framework for building an
    application’s user interface. PyQt5 has QMainWindow and its
    related classes for main window management. QMainWindow has
    its own layout to which you can add QToolBars, QDockWidgets,
    a QMenuBar, and a QStatusBar.

 

__init__” is a reserved method in python classes. It is called as a constructor in object oriented programming. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class. The super() function is used to give access to methods and properties of a parent class. And the super() function makes class inheritance more manageable. The word ‘self’ is used to represent the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python.

 

 

Run the complete code and this is the result.

PyQt5 Tutorial for Beginners
PyQt5 Tutorial for Beginners

 

 

 

How to Create Button in PyQt5

OK now we want to create QPushButton in PyQt5, the QPushButton widget provides a command button. push button or command button is the most commonly used widget in any graphical user interface. Push (click) a button to command the computer to perform some action, or to answer a question. Typical buttons are OK, Apply, Cancel, Close, Yes, No and Help.

 

 

For creating of Button in Python PyQt5 with QPushButton, we need to create the object of QPushButton class.

 

 

Also if you want to set the size of the button than you can use setGeometry() function, you need to give the x and y position of the button also the width and height of the button.

 

 

You can add icon and also style sheet for the button using this code.

 

 

 

Now this is the complete code for creating buttons in PyQt5.

 

 

 

Run the complete code and this will be the result

PyQt5 Tutorial for Beginners
PyQt5 Tutorial for Beginners

 

 

 

Layout Management in PyQt5

In this part we want to talk about PyQt5 Layout Management, so there are different layout that you can use in PyQt5, we are going to learn about VBOXLayout, HBoxLayout, GridLayout.

 

 

QVBoxLayout

If you want to align your widget vertically than you can use QVBoxLayout, this is the complete code for creating VBoxLayout in PyQt5.

 

 

This is the place that we have created our QVBoxLayout object.

 

 

We are going to add four QPushButton in our QVBoxLayout, first you need to create the object of QPushButton.

 

 

After creating of your buttons, you need to add the buttons in VBoxLayout. you can use addWidget() method.

 

 

Now you need to set the layout for the main window, if you don’t do this you will not see any widgets in your window.

 

 

 

Run the code and this is the result

PyQt5 Tutorial for Beginners
PyQt5 Tutorial for Beginners

 

 

QHBoxLayout

If you want to align your widget horizontally than you can use QHBoxLayout, this is the complete code for creating QHBoxLayout in PyQt5.

 

 

You can use QHBoxLayout class for creating hboxlayout in Python GUI programming.

 

 

 

We want to add four buttons in this hbox layout.

 

 

After creating of your buttons, you need to add the buttons in HBoxLayout. you can use addWidget() method.

 

 

Run the code and this is the result.

PyQt5 Tutorial for Beginners
PyQt5 Tutorial for Beginners

 

 

 

QGridLayout

By using gridlayout  you can align your widgets in row and columns, now this is the complete code for QGridLayout.

 

 

You can use QGridLayout class for creating of the grid layout in PyQt5.

 

 

And you can addWidget() method for adding the items of widgets in your QGridLayout, for example in here we want to add some QPushButtons in our PyQt5 GridLayout, also you need to specify the row and column number for the widgets.

 

 

Now you need to set the layout for the main window, if you don’t do this you will not see any widgets in your window.

 

 

 

Run the code and this will be the result

PyQt5 Tutorial for Beginners
PyQt5 Tutorial for Beginners

 

 

 

Creating QLabel in PyQt5

All right guys, now let’s create label in PyQt5, you can use QLabel class for creating label in PyQt5. the QLabel widget provides a text or image display. QLabel is used for displaying text or an image.

 

 

You can create the label using QLabel object.

 

 

 

This is our vertical layout.

 

 

Using this code you can set the font for the label.

 

 

This is used for changing the color of the label.

 

 

This is the complete code 

 

 

 

Run the code and this will be the result

PyQt5 Tutorial for Beginners - QLabel
PyQt5 Tutorial for Beginners – QLabel

 

 

 

PyQt5 Signal & Slots

In this part we want to learn about PyQt5 Signal & Slots, Signal and Slots are used for communication between some objects. a Signal is emitted when a particular event occurs, and a Slot is called when its connected signal is emitted. so now we are going to create an example of PyQt5 Signal and Slots. so this is the complete code for this part.

 

 

This is the code for PyQt5 Signals and Slots

This code defines PyQt5 application that demonstrates the usage of signals and slots. It creates a custom window class Window that inherits from QWidget. Inside the Window class, the create_buttons method sets up the layout of the window by creating two buttons (btn1 and btn2) horizontally aligned using a QHBoxLayout. Each button is styled with custom colors and icons. Additionally, a QLabel (self.label) is created and added above the buttons to display messages when buttons are clicked. The clicked_btn and second_clicked methods are connected to the respective buttons’ clicked signals to handle button clicks and update the label text accordingly.

 

 

 

Run the code and this will the result

PyQt5 Tutorial for Beginners - Signals and Slots
PyQt5 Tutorial for Beginners – Signals and Slots

 

 

 

Create QRadioBox in PyQt5

Now we want to create QRadioBox in PyQt5, also we are going to learn how we can use toggled signal of RadioButton, in pyqt5 we can use QRadioButton class for creating of radiobutton, along with QRadioButton we will learn creating of QGroupBox in PyQt5. this is the complete code for creating QRadioButton and QGroupBox.

 

 

This is the code for PyQt5 QRadioButton

This code demonstrates the usage of QRadioButton in PyQt5 application. It creates a custom dialog window class Window that inherits from QDialog. Inside the Window class, the create_radiobutton method sets up a group box (QGroupBox) containing three radio buttons (QRadioButton) arranged horizontally using a QHBoxLayout. Each radio button represents a programming language option and is styled with custom icons and fonts. The on_selected method is connected to the toggled signal of each radio button to handle selection events. When a radio button is selected, the label text above the group box updates to display the selected language.

 

 

 

Run the code and this will be the result

PyQt5 Tutorial for Beginners - QRadioButton
PyQt5 Tutorial for Beginners – QRadioButton

 

 

 

Creating QSpinBox in PyQt5

Let’s create spinbox in pyqt5, QSpinBox is designed to handle integers and discrete sets of values (e.g., month names), use QDoubleSpinBox for floating point values. QSpinBox allows the user to choose a value by clicking the up/down buttons or pressing up/down on the keyboard to increase/decrease the value currently displayed. You can use QSpinBox class object for creating of the spinbox in PyQt5 GUI.

 

 

This is the code for PyQt5 QSpinBox

This code creates PyQt5 dialog window application that features a spin box (QSpinBox) for selecting the quantity of items, a line edit (QLineEdit) for entering the price of a laptop, and another line edit for displaying the total price. The window’s geometry, title, and icon are set accordingly. When selecting a quantity or changing the laptop price, the total price is calculated and displayed in real-time.

 

 

Run the complete code and this will be the result

PyQt5 GUI Development - QSpinBox
PyQt5 GUI Development – QSpinBox

 

 

 

Creating QLCDNumber in PyQt5

QLCDNumber widget displays a number with LCD-like digits. It can display a number in just about any size. It can display decimal, hexadecimal, octal or binary numbers. It is easy to connect to data sources using the display() slot, which is overloaded to take any of five argument types.

 

 

In this example we want to display our computer clock using QLCDNumber.

This code creates PyQt5 dialog window application with QLCDNumber widget, and it displays the current system time. also we have window’s geometry, title, and icon. QTimer object is utilized to update the displayed time every second (1000 milliseconds). lcd_number method updates the displayed time by retrieving the current system time and formatting it as hours and minutes. The time is then displayed in the QLCDNumber widget, which has a green background color.

 

 

 

Run the code and this will be the result

Python GUI Development - PyQt5 QLCDNumber
Python GUI Development – PyQt5 QLCDNumber

 

 

 

Creating QCalendarWidget in PyQt5

QCalendarWidget class provides a monthly based calendar widget allowing the user to select a date.

 

This is the code for PyQt5 Calendar

This code creates PyQt5 dialog window application with QCalendarWidget for selecting dates. Also we have added window’s geometry, title, and icon to the window. QCalendarWidget displays a calendar where the user can select a date. When a date is selected, the selected date is displayed below the calendar in a QLabel widget. The displayed date is updated dynamically as the user selects different dates.

 

 

Run the complete code and this will be the result

Python GUI Development - QCalendarWidget
Python GUI Development – QCalendarWidget

 

 

 

PyQt5 Creating QSlider

Using QSlider widget you can create vertical or horizontal slider, and  it lets the user move a slider handle along a horizontal or vertical groove and translates the handle’s position into an integer value within the legal range.

 

 

This is the code for PyQt5 QSlider

This code creates PyQt5 window application with QSlider widget for selecting values within a range. Also we have window’s geometry, title, and icon in with a green background color in our window. QSlider is horizontal with tick marks displayed below it. As the user moves the slider, the selected value is displayed in a QLabel widget next to it.

 

 

Run the code and this will be the result

Python GUI Development - QSlider
Python GUI Development – QSlider

 

 

 

Creating QComboBox in PyQt5

A QComboBox provides a means of presenting a list of options to the user in a way that takes up the minimum amount of screen space. A combobox is a selection widget that displays the current item, and can pop up a list of selectable items.

 

This is the code for PyQt5 ComboBox

This code creates PyQt5 window application with QComboBox widget for selecting items from a dropdown list. Also we have added window’s geometry, title, and icon with a black background color to the window. QComboBox has customized text color and font. As the user selects an item from the combo box, the selected item is displayed in a QLabel widget below it.

 

 

Run the code and this will be the result

Python GUI Development - QComboBox
Python GUI Development – QComboBox

 

 

 

FAQs

 

Q: What is PyQt5?

A: PyQt5 is a set of Python bindings for the Qt application framework developed by Riverbank Computing. It allows Python programmers to create desktop applications with a graphical user interface (GUI) using the powerful Qt toolkit.

 

 

Q: Why use PyQt5 for GUI development?

A: PyQt5 provides different GUI components and features, and it allows you to create cross platform applications with a native look and feel. It offers high performance, flexibility and extensive documentation, and this is one of the best choice for both small and large-scale projects.

 

 

Q: Which platforms does PyQt5 support?

A: PyQt5 supports multiple platforms, including Windows, macOS, Linux and different Unix systems. It provides a consistent API across different platforms, and it allows you to write code that works easily across different operating systems.

 

 

Q: How do I install PyQt5?

A: PyQt5 can be installed using pip. You can install PyQt5 by running pip install PyQt5. Also you may need to install PyQt5 tools and other dependencies based on your specific requirements.

 

 

Q: Can I use Qt Designer with PyQt5?

A: Yes, Qt Designer is a powerful visual design tool for building GUIs with Qt, and it can be used with PyQt5. You can design your GUIs visually using Qt Designer and then load them into your PyQt5 application using PyQt’s uic module or by converting them to Python code.

 

 

Q: How to create GUI using PyQt5?

A: For creating a GUI using PyQt5, you first need to install PyQt5 using pip. after the installation you can import the necessary PyQt5 modules in your Python script and start building your GUI by creating QWidget or QMainWindow instances, also you can add different widgets like buttons, labels, text boxes, etc., and you can organize them using layouts such as QVBoxLayout or QHBoxLayout, and connecting signals and slots to handle user interactions. You can also use Qt Designer, a visual design tool, to create your GUIs visually and then integrate them into your PyQt5 application.

 

 

Q: Is PyQt good for GUI?

A: Yes, PyQt is excellent for GUI development. It provides different GUI components and features, and it allows you to create cross-platform desktop applications with a native look and feel. PyQt offers high performance, flexibility and big documentation.

 

 

Is Python suitable for GUI development?

Yes, Python is suitable for GUI development, especially when used in conjunction with frameworks like PyQt, Tkinter, wxPython and Kivy. Python’s simplicity, readability, and extensive libraries make it an excellent choice for developing GUI applications. You can build desktop, web or mobile platforms using Python.

 

 

Learn More on PyQt6:

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×