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.
1 |
pip install PyQt5 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from PyQt5.QtWidgets import QApplication, QWidget import sys from PyQt5.QtGui import QIcon class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200, 400,300) self.setWindowTitle("Geekscoders Window in PyQt5") self.setWindowIcon(QIcon('geekscoders.png')) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) |
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.
1 |
self.setGeometry(200,200, 400,300) |
If you want to give a title than you can use this code.
1 |
self.setWindowTitle("Geekscoders Window in PyQt5") |
This is used for creating icon for our window.
1 |
self.setWindowIcon(QIcon('geekscoders.png')) |
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.
1 2 |
app = QApplication(sys.argv) app = QApplication([]) |
In here we need to create the object of our window, and also show the window.
1 2 |
window = Window() window.show() |
And this is the starting point of our event loop.
1 |
sys.exit(app.exec_()) |
Run the complete code and this is the result.

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.
1 |
btn1 = QPushButton("Click Me", self) |
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.
1 |
btn1.setGeometry(100,100, 100,100) |
You can add icon and also style sheet for the button using this code.
1 2 3 |
btn1.setIcon(QIcon("cricket.png")) btn1.setIconSize(QSize(40,40)) btn1.setStyleSheet('color:gray') |
Now this is the complete code for creating buttons in PyQt5.
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 |
from PyQt5.QtCore import QSize from PyQt5.QtWidgets import QApplication,QWidget, QPushButton import sys from PyQt5.QtGui import QIcon class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200, 400,300) self.setWindowTitle("Geekscoders.com - Creating Button") self.setWindowIcon(QIcon('geekscoders.png')) self.my_buttons() def my_buttons(self): btn1 = QPushButton("Click Me", self) btn1.setGeometry(100,100, 100,100) btn1.setIcon(QIcon("cricket.png")) btn1.setIconSize(QSize(40,40)) btn1.setStyleSheet('color:gray') btn1.setStyleSheet('background-color:green') btn2 = QPushButton("Click Two", self) btn2.setGeometry(200,100, 100,100) btn2.setIcon(QIcon("football.png")) btn2.setIconSize(QSize(40, 40)) btn2.setStyleSheet('color:blue') btn2.setStyleSheet('background-color:purple') app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) |
Run the complete code and this is the result.

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.
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 |
from PyQt5.QtWidgets import QApplication,QWidget, QVBoxLayout, QPushButton import sys from PyQt5.QtGui import QIcon class Window(QWidget): def __init__(self): super().__init__() #window title, icon and geometry self.setGeometry(200,200, 400,300) self.setWindowTitle("GeeksCoders - QVBoxLayout") self.setWindowIcon(QIcon('geekscoders.png')) #vboxlayout object vbox = QVBoxLayout() #creating QPushButton btn1 = QPushButton("GeeksCoders One") btn2 = QPushButton("GeeksCoders Two") btn3 = QPushButton("GeeksCoders Three") btn4 = QPushButton("GeeksCoders Four") #add widgets in the layout vbox.addWidget(btn1) vbox.addWidget(btn2) vbox.addWidget(btn3) vbox.addWidget(btn4) #set the layout for the main window self.setLayout(vbox) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) |
This is the place that we have created our QVBoxLayout object.
1 |
vbox = QVBoxLayout() |
We are going to add four QPushButton in our QVBoxLayout, first you need to create the object of QPushButton.
1 |
btn1 = QPushButton("Click One") |
After creating of your buttons, you need to add the buttons in VBoxLayout. you can use addWidget() method.
1 |
vbox.addWidget(btn1) |
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.
1 |
self.setLayout(vbox) |
Run the code and this is the result

-
QHBoxLayout
If you want to align your widget horizontally than you can use QHBoxLayout, this is the complete code for creating QHBoxLayout in PyQt5.
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 |
from PyQt5.QtWidgets import QApplication,QWidget,QHBoxLayout, QPushButton import sys from PyQt5.QtGui import QIcon class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200, 400,300) self.setWindowTitle("Geekscoder.com - QHBoxLayout") self.setWindowIcon(QIcon('geekscoders.png')) hbox = QHBoxLayout() btn1 = QPushButton("GeeksCoders One") btn2 = QPushButton("GeeksCoders Two") btn3 = QPushButton("GeeksCoders Three") btn4 = QPushButton("GeeksCoders Four") hbox.addWidget(btn1) hbox.addWidget(btn2) hbox.addWidget(btn3) hbox.addWidget(btn4) self.setLayout(hbox) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) |
You can use QHBoxLayout class for creating hboxlayout in Python GUI programming.
1 |
hbox = QHBoxLayout() |
We want to add four buttons in this hbox layout.
1 2 3 4 |
btn1 = QPushButton("GeeksCoders One") btn2 = QPushButton("GeeksCoders Two") btn3 = QPushButton("GeeksCoders Three") btn4 = QPushButton("GeeksCoders Four") |
After creating of your buttons, you need to add the buttons in HBoxLayout. you can use addWidget() method.
1 |
hbox.addWidget(btn1) |
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.
1 |
self.setLayout(hbox) |
Run the code and this is the result.

-
QGridLayout
By using gridlayout you can align your widgets in row and columns, now this is the complete code for QGridLayout.
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 |
from PyQt5.QtWidgets import QApplication,QWidget, QGridLayout, QPushButton import sys from PyQt5.QtGui import QIcon class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200, 400,300) self.setWindowTitle("Geekscoders.com - GridLayout") self.setWindowIcon(QIcon('geekscoders.png')) grid = QGridLayout() btn1 = QPushButton("GeeksCoders.com") btn2 = QPushButton("GeeksCoders.com") btn3 = QPushButton("GeeksCoders.com") btn4 = QPushButton("GeeksCoders.com") btn5 = QPushButton("GeeksCoders.com") btn6 = QPushButton("GeeksCoders.com") btn7 = QPushButton("GeeksCoders.com") btn8 = QPushButton("GeeksCoders.com") grid.addWidget(btn1, 0, 0) grid.addWidget(btn2, 0, 1) grid.addWidget(btn3, 0, 2) grid.addWidget(btn4, 0, 3) grid.addWidget(btn5, 1, 0) grid.addWidget(btn6, 1, 1) grid.addWidget(btn7, 1, 2) grid.addWidget(btn8, 1, 3) self.setLayout(grid) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) |
You can use QGridLayout class for creating of the grid layout in PyQt5.
1 |
grid = QGridLayout() |
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.
1 |
grid.addWidget(btn1, 0, 0) |
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.
1 |
self.setLayout(grid) |
Run the code and this is the result

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.
1 |
label = QLabel("Welcome To Geeks Coders") |
This is our vertical layout.
1 |
vbox = QVBoxLayout() |
Using this code you can set the font for the label.
1 |
label2.setFont(QtGui.QFont("Sanserif", 20)) |
This is used for changing the color of the label.
1 |
label2.setStyleSheet('color:red') |
Run the code and this is the result.

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.
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
from PyQt5.QtWidgets import QApplication,QHBoxLayout,QVBoxLayout,QLabel,QWidget, QPushButton import sys from PyQt5.QtGui import QIcon, QFont class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200, 400,200) self.setWindowTitle("Signal And Slots - Geekscoders.com") self.setWindowIcon(QIcon('geekscoders.png')) self.create_buttons() #self.show() def create_buttons(self): vbox = QVBoxLayout() hbox = QHBoxLayout() btn1 = QPushButton("Click Me", self) btn1.setIcon(QIcon("cricket.png")) btn1.setStyleSheet('color:red') btn1.setStyleSheet('background-color:green') hbox.addWidget(btn1) btn1.clicked.connect(self.clicked_btn) btn2 = QPushButton("Click Two", self) btn2.setIcon(QIcon("football.png")) btn2.setStyleSheet('color:yellow') btn2.setStyleSheet('background-color:purple') hbox.addWidget(btn2) btn2.clicked.connect(self.second_clicked) self.label = QLabel("Hello") self.label.setFont(QFont("Sanserif", 15)) vbox.addWidget(self.label) vbox.addLayout(hbox) self.setLayout(vbox) def clicked_btn(self): self.label.setText("Cricket Button Was Clicked") def second_clicked(self): self.label.setText("Football Button Was Clicked") app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) |
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.
1 |
btn1.clicked.connect(self.clicked_btn) |
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.
1 2 |
def clicked_btn(self): self.label.setText("Button One Was Clicked") |
Run the code and click on the button this will be the result.

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.
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
from PyQt5.QtWidgets import QApplication,QDialog, QGroupBox,\ QHBoxLayout,QLabel ,QVBoxLayout, QRadioButton import sys from PyQt5.QtGui import QIcon, QFont from PyQt5.QtCore import QSize class Window(QDialog): def __init__(self): super().__init__() #winow requirement self.setGeometry(200,200, 400,300) self.setWindowTitle("Geekscoders.com - QRadioButton") self.setWindowIcon(QIcon('geekscoders.png')) #our method call self.create_radiobutton() #we need to create vertical layout vbox = QVBoxLayout() vbox.addWidget(self.groupbox) #this is our label self.label = QLabel("") self.label.setFont(QFont("Sanserif", 14)) #add your widgets in the vbox layout vbox.addWidget(self.label) #set your main window layout self.setLayout(vbox) def create_radiobutton(self): #this is our groupbox self.groupbox = QGroupBox("What is your favorite Language ?") self.groupbox.setFont(QFont("Sanserif", 15)) #this is hbox layout hbox = QHBoxLayout() #these are the radiobuttons self.rad1 = QRadioButton("Python") self.rad1.setChecked(True) self.rad1.setIcon(QIcon('python.png')) self.rad1.setIconSize(QSize(40,40)) self.rad1.setFont(QFont("Sanserif", 14)) self.rad1.toggled.connect(self.on_selected) hbox.addWidget(self.rad1) self.rad2 = QRadioButton("C++") self.rad2.setIcon(QIcon('cpp.png')) self.rad2.setIconSize(QSize(40, 40)) self.rad2.setFont(QFont("Sanserif", 14)) self.rad2.toggled.connect(self.on_selected) hbox.addWidget(self.rad2) self.rad3 = QRadioButton("Java") self.rad3.setIcon(QIcon('java.png')) self.rad3.setIconSize(QSize(40, 40)) self.rad3.setFont(QFont("Sanserif", 14)) self.rad3.toggled.connect(self.on_selected) hbox.addWidget(self.rad3) self.groupbox.setLayout(hbox) #method or slot for the toggled signal def on_selected(self): radio_button = self.sender() if radio_button.isChecked(): self.label.setText("My Favorite Language Is : " + radio_button.text()) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) |
You can create groupbox by creating the object of QGroupBox class. also we have set the font size for the groupbox.
1 2 |
self.groupbox = QGroupBox("What is your favorite Language ?") self.groupbox.setFont(QFont("Sanserif", 15)) |
We want to add our all radiobuttons in the hboxlayout.
1 |
hbox = QHBoxLayout() |
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.
1 2 3 4 5 6 7 |
self.rad1 = QRadioButton("Python") self.rad1.setChecked(True) self.rad1.setIcon(QIcon('python.png')) self.rad1.setIconSize(QSize(40,40)) self.rad1.setFont(QFont("Sanserif", 14)) self.rad1.toggled.connect(self.on_selected) hbox.addWidget(self.rad1) |
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.
1 2 3 4 5 |
def on_selected(self): radio_button = self.sender() if radio_button.isChecked(): self.label.setText("My Favorite Language Is : " + radio_button.text()) |
Run the complete code and this is the result.

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.
1 |
spinbox = QSpinBox() |
This is the complete code for QSpinBox
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
from PyQt5.QtWidgets import QApplication, QDialog,\ QHBoxLayout, QSpinBox, QLabel, QLineEdit import sys from PyQt5.QtGui import QIcon class Window(QDialog): def __init__(self): super().__init__() #our window requirements like icon,title self.setGeometry(200,200,400,300) self.setWindowTitle("Geekscoders.com - PyQt5 SpinBox") self.setWindowIcon(QIcon("geekscoders.png")) self.create_spinbox() def create_spinbox(self): #cretae hboxlayout hbox = QHBoxLayout() #in here we have created our label label = QLabel("Laptop Price : ") #this is our linedit self.lineEdit = QLineEdit() #we need to create the object of QSpinBox class self.spinbox = QSpinBox() #we have connected valueChanged signal self.spinbox.valueChanged.connect(self.spin_selected) self.totalResult = QLineEdit() #add widgets to your hbox layout hbox.addWidget(label) hbox.addWidget(self.lineEdit) hbox.addWidget(self.spinbox) hbox.addWidget(self.totalResult) self.setLayout(hbox) def spin_selected(self): if self.lineEdit.text() != 0: price = int(self.lineEdit.text()) totalPrice = self.spinbox.value() * price self.totalResult.setText(str(totalPrice)) else: print("Wrong value") App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
In this PyQt5 application we need two QLineEdit, one QLabel and one QSpinBox, also an hbox layout for aligning horizontally our widgets.
1 2 3 4 5 |
hbox = QHBoxLayout() label = QLabel("Laptop Price : ") self.lineEdit = QLineEdit() self.spinbox = QSpinBox() self.totalResult = QLineEdit() |
We have connected the valueChanged() signal of spinbox with spin_selected() method that we are going to create.
1 |
self.spinbox.valueChanged.connect(self.spin_selected) |
You need to add all your widgets in the hbox layout.
1 2 3 4 |
hbox.addWidget(label) hbox.addWidget(self.lineEdit) hbox.addWidget(self.spinbox) hbox.addWidget(self.totalResult) |
And this is the method that we have already connected with the valueChanged() signal of QSpinBox.
1 2 3 4 5 6 7 8 9 10 |
def spin_selected(self): if self.lineEdit.text() != 0: price = int(self.lineEdit.text()) totalPrice = self.spinbox.value() * price self.totalResult.setText(str(totalPrice)) else: print("Wrong value") |
Run the code and this is the result.

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.
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 49 50 51 52 53 54 55 56 57 |
from PyQt5.QtWidgets import QApplication, QDialog,QLCDNumber, QVBoxLayout import sys from PyQt5.QtGui import QIcon from PyQt5.QtCore import QTime, QTimer class Window(QDialog): def __init__(self): super().__init__() #window requrements like geometry,icon and title self.setGeometry(200,200,400,200) self.setWindowTitle("Geekscoders.com - PyQt5 QLCDNumber") self.setWindowIcon(QIcon("geekscoders.png")) #we need to create QTimer object timer = QTimer() timer.timeout.connect(self.lcd_number) #start the timer, it takes meli second timer.start(1000) self.lcd_number() def lcd_number(self): #vbox layout object vbox = QVBoxLayout() #create QLCDNumber object lcd = QLCDNumber() #give background color for the lcd number lcd.setStyleSheet('background:yellow') #add lcd number to vertical box layout vbox.addWidget(lcd) #create time object time = QTime.currentTime() text = time.toString('hh:mm') #displat the system time in the lcd lcd.display(text) self.setLayout(vbox) App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
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.
1 2 3 |
timer = QTimer() timer.timeout.connect(self.lcd_number) timer.start(1000) |
This is used for creating the object of QLCDNumber.
1 |
lcd = QLCDNumber() |
Give background color for the LCDNumber.
1 |
lcd.setStyleSheet('background:yellow') |
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.
1 2 |
time = QTime.currentTime() text = time.toString('hh:mm') |
This is the code that we want to show our time in lcd. you can use display() method of QLCDNumber class.
1 |
lcd.display(text) |
Run the code and this is the result.

Creating QCalendarWidget in PyQt5
QCalendarWidget class provides a monthly based calendar widget allowing the user to select a date.
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 |
from PyQt5.QtWidgets import QApplication, QDialog,\ QVBoxLayout, QCalendarWidget, QLabel import sys from PyQt5.QtGui import QIcon, QFont class Window(QDialog): def __init__(self): super().__init__() #window requrements like geometry,icon and title self.setGeometry(200,200,400,200) self.setWindowTitle("Geekscoders.com - PyQt5 QCalendar") self.setWindowIcon(QIcon("geekscoders.png")) vbox = QVBoxLayout() self.calendar = QCalendarWidget() self.calendar.setGridVisible(True) self.calendar.selectionChanged.connect(self.calendar_date) self.label =QLabel("") self.label.setFont(QFont("Sanserif", 15)) self.label.setStyleSheet('color:green') vbox.addWidget(self.calendar) vbox.addWidget(self.label) self.setLayout(vbox) def calendar_date(self): dateselected = self.calendar.selectedDate() date_in_string = str(dateselected.toPyDate()) self.label.setText("Date Is : " + date_in_string) App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
You can create pyqt5 calendar by creating the object of QCalendarWidget.
1 |
self.calendar = QCalendarWidget() |
In here we have connected the selectionChanged() signal of the calendar with the calender_date() method that we create.
1 |
self.calendar.selectionChanged.connect(self.calendar_date) |
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.
1 2 3 4 5 |
def calendar_date(self): dateselected = self.calendar.selectedDate() date_in_string = str(dateselected.toPyDate()) self.label.setText("Date Is : " + date_in_string) |
Run the code and this is the result

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.
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 49 50 51 52 53 54 55 56 57 |
from PyQt5.QtWidgets import QApplication, QWidget,\ QVBoxLayout, QTableWidgetItem, QTableWidget import sys from PyQt5.QtGui import QIcon class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200,400,200) self.setWindowTitle("Geekscoders.com - QTableWidget") self.setWindowIcon(QIcon("geekscoders.png")) self.create_tables() def create_tables(self): vbox = QVBoxLayout() table_widget = QTableWidget() table_widget.setRowCount(3) table_widget.setColumnCount(3) table_widget.setItem(0, 0, QTableWidgetItem("Name")) table_widget.setItem(0, 1, QTableWidgetItem("Email")) table_widget.setItem(0, 2, QTableWidgetItem("Phone")) #adding items table_widget.setItem(1,0, QTableWidgetItem("GeeksCoders")) table_widget.setItem(1, 1, QTableWidgetItem("geekscoders@gmail.com")) table_widget.setItem(1, 2, QTableWidgetItem("0743445563")) table_widget.setItem(2, 0, QTableWidgetItem("Parwiz")) table_widget.setItem(2, 1, QTableWidgetItem("par@gmail.com")) table_widget.setItem(2, 2, QTableWidgetItem("0654993933")) vbox.addWidget(table_widget) self.setLayout(vbox) App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
In here we have created our QTableWidget object also we need to set the row and column count for the tablewidget.
1 2 3 |
table_widget = QTableWidget() table_widget.setRowCount(3) table_widget.setColumnCount(3) |
And using QTableWidgeItem class you can add items to the pyqt tablewidget.
1 2 3 |
table_widget.setItem(0, 0, QTableWidgetItem("Name")) table_widget.setItem(0, 1, QTableWidgetItem("Email")) table_widget.setItem(0, 2, QTableWidgetItem("Phone")) |
Run the code and this is the result.

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.
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
from PyQt5.QtWidgets import QApplication, QWidget,\ QHBoxLayout, QMessageBox, QPushButton, QVBoxLayout, QLabel import sys from PyQt5.QtGui import QIcon, QFont class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200,400,200) self.setWindowTitle("GeeksCoders.com - QMessageBox") self.setWindowIcon(QIcon("geekscoders.png")) self.setStyleSheet('background-color:yellow') self.create_messagebox() def create_messagebox(self): hbox = QHBoxLayout() btn1 = QPushButton("Warning") btn1.clicked.connect(self.warning_msg) btn2 = QPushButton("Information") btn2.clicked.connect(self.info_msg) btn3 = QPushButton("About") btn3.clicked.connect(self.about_msg) btn4 = QPushButton("Yes/No") btn4.clicked.connect(self.multi_choice_msg) hbox.addWidget(btn1) hbox.addWidget(btn2) hbox.addWidget(btn3) hbox.addWidget(btn4) vbox = QVBoxLayout() self.label = QLabel("") self.label.setFont(QFont("Sanserif", 14)) vbox.addLayout(hbox) vbox.addWidget(self.label) self.setLayout(vbox) def warning_msg(self): QMessageBox.warning(self, "Warning", "This is a warning message") def info_msg(self): QMessageBox.information(self, "Information", "This is information message") def about_msg(self): QMessageBox.about(self, "About", "This is about message") def multi_choice_msg(self): message = QMessageBox.question(self, "Choice Message", "Do You Like Python ?", QMessageBox.Yes | QMessageBox.No) if message == QMessageBox.Yes: self.label.setText("Yes I Like Python") else: self.label.setText("No I Dont Like Python") App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
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.
1 2 3 4 5 6 7 8 9 10 11 |
btn1 = QPushButton("Warning") btn1.clicked.connect(self.warning_msg) btn2 = QPushButton("Information") btn2.clicked.connect(self.info_msg) btn3 = QPushButton("About") btn3.clicked.connect(self.about_msg) btn4 = QPushButton("Yes/No") btn4.clicked.connect(self.multi_choice_msg) |
When you create layouts and widgets in your pyqt applications, you need to add all your widgets in the layout.
1 2 3 4 |
hbox.addWidget(btn1) hbox.addWidget(btn2) hbox.addWidget(btn3) hbox.addWidget(btn4) |
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.
1 |
self.setLayout(vbox) |
You can create pyqt5 messagebox by creating the object of QMessageBox class. so you can see that we have created four types of QMessageBox.
1 2 3 4 5 6 7 8 9 |
QMessageBox.warning(self, "Warning", "This is a warning message") QMessageBox.information(self, "Information", "This is information message") QMessageBox.about(self, "About", "This is about message") message = QMessageBox.question(self, "Choice Message", "Do You Like Python ?", QMessageBox.Yes | QMessageBox.No) |
So run the code and this is the result

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
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 49 50 51 52 53 54 55 56 57 |
from PyQt5.QtWidgets import QApplication, QWidget, \ QVBoxLayout, QComboBox, QLabel import sys from PyQt5.QtGui import QIcon, QFont class Window(QWidget): def __init__(self): super().__init__() #window requrements like geometry,icon and title self.setGeometry(200,200,400,200) self.setWindowTitle("GeeksCoders.com - QComboBox") self.setWindowIcon(QIcon("geekscoders.png")) self.setStyleSheet('background-color:green') #our vboxlayout vbox = QVBoxLayout() #create the object of combobox self.combo = QComboBox() self.combo.setStyleSheet('color:white') #add items to the combobox self.combo.addItem("Melon") self.combo.addItem("Apple") self.combo.addItem("Banana") self.combo.addItem("Pear") #connected combobox signal self.combo.currentTextChanged.connect(self.combo_selected) #create label self.label = QLabel("") self.label.setFont(QFont("Sanserif", 15)) self.label.setStyleSheet('color:white') #added widgets in the vbox layout vbox.addWidget(self.combo) vbox.addWidget(self.label) self.setLayout(vbox) def combo_selected(self): item = self.combo.currentText() self.label.setText("You selected : " + item) App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
So you can use QComboBox class object for creating combobox in pyqt.
1 |
self.combo = QComboBox() |
This is the currentTextChanged() signal that is connected with the combo_selected() method.
1 |
self.combo.currentTextChanged.connect(self.combo_selected) |
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.
1 2 3 |
def combo_selected(self): item = self.combo.currentText() self.label.setText("You selected : " + item) |
Run the code and this is the result.

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.
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 49 |
from PyQt5.QtWidgets import QApplication, QWidget, \ QSlider, QLabel, QHBoxLayout import sys from PyQt5.QtGui import QIcon, QFont from PyQt5.QtCore import Qt class Window(QWidget): def __init__(self): super().__init__() #window requrements like geometry,icon and title self.setGeometry(200,200,400,200) self.setWindowTitle("Geekscoders.com - QSlider") self.setWindowIcon(QIcon("geekscoders.png")) self.setStyleSheet('background-color:purple') hbox = QHBoxLayout() self.slider = QSlider() self.slider.setOrientation(Qt.Horizontal) self.slider.setTickPosition(QSlider.TicksBelow) self.slider.setTickInterval(10) self.slider.setMinimum(0) self.slider.setMaximum(100) self.slider.valueChanged.connect(self.changed_slider) self.label =QLabel("") self.label.setFont(QFont("Sanserif", 15)) self.label.setStyleSheet('color:white') hbox.addWidget(self.slider) hbox.addWidget(self.label) self.setLayout(hbox) def changed_slider(self): value = self.slider.value() self.label.setText(str(value)) App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
You can use QSlider class object for creating slider in pyqt.
1 |
self.slider = QSlider() |
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.
1 2 3 |
def changed_slider(self): value = self.slider.value() self.label.setText(str(value)) |
Run the complete code and this is the result.

Learn More on PyQt5
- How to Deploy PyQt5 Applications
- How to Integrate PyQt5 with OpenCV
- How to Use Stylesheets in PyQt5
- Learn PyQt5 Event Handling with Signals and Slots
- PyQt5 Tutorial: Creating Menus and Toolbars
- How to Open & Save Files in Python PyQt5
- PyQt5 Designer Tutorial: Complete Guide to Qt Designer
- PyQt5 vs Tkinter: Which GUI Library is Right for You
- How to Build Web Applications in PyQt5
- How to Integrate Python Flask with 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.