Python PyQt5 Tutorial for Beginners

Do you need Python PyQt5 Tutorial for Beginners, so in this PyQt5 Tutorial we are going learn about building Python GUI applications with PyQt5. there will be different concepts on this that how you can install PyQt5 , how you can create your first window in PyQt5 and how you can work with some 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.

 

 

 

Python PyQt5 Tutorial for Beginners

Now let’s create some PyQt5 examples

 

 

Creating First 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.

 

 

Using this line of code we are setting the x and y position of the window, also width and height of the window.

 

 

If you want to give a title than you can use this code.

 

 

This is used for creating icon for our window.

 

 

In every PyQt5 application you need to create the object of Application, and sys.argv parameter is optional, if you want to use command line utility than you can add sys.argv, if you don’t want to use than you can leave it as a blank list. 

 

 

In here we need to create the object of our window, and also show the window.

 

 

And this is the starting point of our event loop.

 

 

Run the complete code and this is the result.

Python PyQt5 Tutorial for Beginners
Python PyQt5 Tutorial for Beginners

 

 

 

Creating QPushButton in Python PyQt5

OK now we want to create QPushButton in PyQt5, the QPushButton widget provides a command button. The push button, or command button, is perhaps 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 is the result.

Python PyQt5 Tutorial for Beginners
Python 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

Python PyQt5 Tutorial for Beginners
Python 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.

 

 

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.

Python PyQt5 Tutorial for Beginners
Python 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 is the result

PyQt5 Tutorial - QGridLayout
PyQt5 Tutorial – QGridLayout

 

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.

 

 

 

Run the code and this is the result.

PyQt5 Tutorial
PyQt5 Tutorial

 

 

 

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.

 

 

 

For button there is a signal that is called clicked signal, so you need to connect this clicked signal with method that you want to create. basically by clicking the button i want to change the text of the label.

So you can see we have connected the clicked signal of the button with the clicked_btn() method that we are going to create.

 

 

 

This is just a simple method or slot that we have already connected, and we want to change our label text.

 

 

 

Run the code and click on the button this will be the result.

Signal And Slots in PyQt5
Signal And Slots in PyQt5

 

 

 

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.

 

 

 

You can create groupbox by creating the object of QGroupBox class. also we have set the font size for the groupbox.

 

 

We want to add our all radiobuttons in the hboxlayout.

 

 

 

Basically we want to create three radiobuttons,  and you can see that i have created icon for my radiobutton, make sure that you have already added some icons in your working directory. also i have have connected radiobutton toggled() signal with the on_selected()  method that i have already created, basically this method is used for the user interaction, i want when a user clicks on the radiobutton, my label text should be changed.

 

 

 

And this is the method or slot that we have already connected with the toggled signal of QRadioButton. basically in here we are checking the state of the radiobutton and based on that we want to change our label text.

 

 

 

Run the complete code and this is the result.

PyQt5 Tutorial - Create GUI in Python & PyQt5
PyQt5 Tutorial – Create GUI in Python & PyQt5

 

 

 

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 complete code for QSpinBox

 

 

 

In this PyQt5 application we need two QLineEdit, one QLabel and one QSpinBox, also an hbox layout  for aligning horizontally our widgets.

 

 

 

We have connected the valueChanged() signal of spinbox with spin_selected() method that we are going to create.

 

 

You need to add all your widgets in the hbox layout.

 

 

And this is the method that we have already connected with the valueChanged() signal of QSpinBox.

 

 

 

Run the code and this is the result.

PyQt5 Tutorial - QSpinBox
PyQt5 Tutorial – 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.

 

 

So in here we are going to create a QTimer class object, the QTimer class provides repetitive and single-shot timers. the QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its timeout() signal to the meli seconds.

 

 

This is used for creating the object of QLCDNumber.

 

 

Give background color for the LCDNumber.

 

 

In here we have created the object of QTime, because we want to get the system time. Also you need to convert the time to string.

 

 

This is the code that we want to show our time in lcd. you can use display() method of QLCDNumber class.

 

 

Run the code and this is the result.

PyQt5 Tutorial - Create GUI in Python & PyQt5
PyQt5 Tutorial – Create GUI in Python & PyQt5

 

 

Creating QCalendarWidget in PyQt5

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

 

 

You can create pyqt5 calendar by creating the object of QCalendarWidget.

 

 

In here we have connected the selectionChanged() signal of the calendar with the calender_date() method that we create.

 

 

 

This is the method that we have connected with the selectionChanged() signal of the QCalendarWidget, so in this method first we have got the date from Calendar and we have set the date in the QLabel.

 

 

 

Run the code and this is the result 

PyQt5 Tutorial QCalendar
PyQt5 Tutorial QCalendar

 

 

 

 

Creating QTableWidget in PyQT5

The QTableWidget class provides an item-based table view with a default model. Table widgets provide standard table display facilities for applications. The items in a QTableWidget are provided by QTableWidgetItem.

 

 

 

In here we have created our QTableWidget object also we need to set the row and column count for the tablewidget.

 

 

And using QTableWidgeItem class you can add items to the pyqt tablewidget. 

 

 

 

Run the code and this is the result.

QTableWidget in PyQt5
QTableWidget in PyQt5

 

 

 

Creating QMessageBox in PyQt5

QMessageBox class provides a modal dialog for informing the user or for asking the user a question and receiving an answer. there are different types of PyQt5 message box that you can use, for example we have about messagebox,information messagebox, warning messagebox and multichoice messagebox.

 

 

 

These are our four buttons and we have already connected the clicked signal of these buttons with the slots or methods that we want to create.

 

 

 

When you create layouts and widgets in your pyqt applications, you need to add all your widgets in the layout.

 

 

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

 

 

You can create pyqt5 messagebox by creating the object of QMessageBox class. so you can see that we have created four types of QMessageBox.

 

 

 

So run the code and this is the result

PyQt5 QMessageBox
PyQt5 QMessageBox

 

 

 

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

 

 

 

So you can use QComboBox class object for creating combobox in pyqt.

 

 

 

This is the currentTextChanged() signal that is connected with the combo_selected() method.

 

 

 

This is the method that we have already connected with the currentTextChanged signal of combobox. so in this method first we need to get the item value from pyqt combobox and after that we set the value in pyqt label.

 

 

 

Run the code and this is the result.

PyQt5 QComboBox
PyQt5 QComboBox

 

 

 

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.

 

 

 

You can use QSlider class object for creating slider in pyqt.

 

 

 

And this is the method that we have already connected with the signal, in this method we are going to just get the text from the slider and we set the text in the label using setText() method.

 

 

Run the complete code and this is the result.

PyQt5 Tutorial - Create GUI in Python & PyQt5
PyQt5 Tutorial – Create GUI in Python & PyQt5

 

 

Learn More on PyQt5

 

 

In result we can say that learning how to use PyQt5 for Python GUI development is an essential skill for beginners to master. by following PyQt5 tutorial and learning how to use signals and slots, widgets and other PyQt5 features, beginners can quickly create powerful and responsive graphical user interfaces for their Python applications.

In this tutorial, we have covered basics of PyQt5 installation and walked through several PyQt5 examples including creating dialogs, message boxes and table widgets. we have also explored how to use PyQt5 signals and slots to create interactive and responsive user interfaces.

If you are an experienced Python programmer or just starting with Python, learning how to use PyQt5 is an essential skill that can help you create powerful and intuitive GUIs for your applications. 

 

 

 

Leave a Comment