0% found this document useful (0 votes)
29 views14 pages

PyQt5 tutorial learn GUI programming with Python and PyQt5 | by Haider Imtiaz | Level Up Coding

PyQt5 tutorial learn GUI programming with Python and PyQt5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views14 pages

PyQt5 tutorial learn GUI programming with Python and PyQt5 | by Haider Imtiaz | Level Up Coding

PyQt5 tutorial learn GUI programming with Python and PyQt5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

This is your last free member-only story this month. Upgrade for unlimited access.

PyQt5 tutorial: learn GUI programming with


Python and PyQt5
Haider Imtiaz Follow
Mar 30 · 6 min read

Photo by Fotis Fotopoulos on Unsplash

Pyqt5 is the Graphical User interface widget toolkit. It is one of the most powerful and
popular python interfaces. It's a combination of the Qt module and python
programming. Qt itself is a C++ binding. Pyqt5 is a free open-source widget toolkit that
can be implemented on cross-platform application development. PyQt is free software

1 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

developed and maintained by Riverbank Computing, a company based in England,


whereas Qt is developed by a Finnish firm called The Qt Company. In this article, we will
walk through how we build a desktop application using Pyqt5.

Features of Pyqt5:
1. Graphical User interfaces

2. XML Handling

3. Network communication

4. Multimedia

5. Databases

6. Web browsing

7. Multi-Threading

Installation:
You need the latest version of python installed in your system. If you already installed
python then follow the following command in your command prompt for windows and
Terminal for Mac and Linux.

pip install PyQt5

Basic Concepts of PyQt5:


Basic concepts you will see how we can build a window for our desktop application.

1 #importing libraries
2 import sys
3 from PyQt5.QtWidgets import QApplication, QWidget
4 if __name__ == "__main__":
5 app = QApplication(sys.argv)
6 w = QWidget()
7 w.resize(300,300)
8 w.setWindowTitle('Medium Pyqt5')
9 w.show()
10 sys.exit(app.exec_())

basics44.py hosted with ❤ by GitHub view raw

Congratulation you build your first desktop application window. Let take look on each

2 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

of the program. First two lines we are just importing the modules in a python file. Sys

module is a Python built-in module that had operating system interaction functions.
Next, we had the Pyqt5 module from which we import the selected classes QtWidget,
QApplication, QWidget.

QWidget is the base class of the UI in pyqt5, everything you see in a desktop application
like Buttons, search bar, drop-down menu and etc are widgets so you can say that
widgets are built in a widget. This will help you to build complex user interfaces by
nesting the widgets.

w.resize(300,300) is a Qwidget class method that resizes your window to any height
and width. They take two parameters, the first is the x which denotes width and the
second is y which denotes height.

w.setwindowtitle(“Basic pyqt5”) is a Qt widget class method that takes a string


argument as a parameter and changes your desktop application title bar.

w.show() is the Qtwidget method that shows the window on your screen.

sys.exit(app.exec_()) will start the Qt/C++ event loop as we know the Qt is purely
built-in C++ language and app.exec_() will simply start the loop mechanism of the app
and you cannot close the desktop application by simply pressing ctrl + c as you do in
other applications you need to click on the close button of the application.

Output:

3 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

PyQt5 component modules


These are the fundamental modules used by Python’s Qt binding, specifically PyQt5.
Following are the modules and their explanation.

1. QtWidget is a pyqt5 module class that contains most of the widgets of pyqt5

2. QtCore is the core module of the pyqt5 class that contains non-graphical user-
interface components like signals, slot-connectivity, event loop and etc.

3. QtMultimedia is an essential class of PyQt5 that handles multimedia content it also


provides the necessary API to get access to your camera functionality.

4. QtGui is a pyqt5 module class that contain Gui components and extend the QtCore
module

5. QtSql is a pyqt5 module that handles the SQL Databases. It Supports ODBC,
MySQL, Oracle, SQLite, and PostgreSQL.

6. QtNetwork is a pyqt5 module that is used to implement network programming. It


supports TCP servers, TCP sockets, UDP sockets, SSL handling, network sessions,
and DNS lookups.

Window Layout and OOP


Pyqt5 user interface programming can be done in object-oriented programming which

4 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

is easier to understand and maintain code. If you saw the code below, on line 4 we
implemented a Class name App() with argument QWidget and then def __ini__() we
had declared instances of the Gui class title, left, top, width, height. We had another
method name def initUI(self) you can name this class method on your own also in that
method I used setWindowTitle() which take string argument but i pass an instance of
the class that I created in __init__() method and next, we use setGeometry() that set
the size of our window self.left and self.top is showing where to display the Gui
window on your screen, in last self.show() is showing the Pyqt5 Gui window.

1 import sys
2 from PyQt5.QtWidgets import QApplication, QWidget
3
4 class App(QWidget):
5 def __init__(self):
6 super().__init__()
7 self.title = 'Pyqt5 window'
8 self.left = 10
9 self.top = 10
10 self.width = 300
11 self.height = 300
12 self.initUI()
13
14 def initUI(self):
15 self.setWindowTitle(self.title)
16 self.setGeometry(self.left, self.top, self.width, self.height)
17 self.show()
18
19 if __name__ == '__main__':
20 app = QApplication(sys.argv)
21 ex = App()
22 sys.exit(app.exec_())

app.py hosted with ❤ by GitHub view raw

TextBox
In many User interface programs, there will be an input box where you can input a text
into the software and they provide the result according to their functionality. Pyqt5
Qwidget had a method to make TextBox in your Gui programs. QLineEdit() class which
is inside the QWidget class. You can set the position of the Textbox using move() method

by passing x,y coordinates, and also you can resize the Textbox using resize() by
passing height and width into it. QLineEdit() has setText() method that set the text of

5 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

your Textbox and getText() method will get the input text in TextBox.

self.textbox = QLineEdit(self)
self.textbox.move(20, 20)
self.textbox.resize(280,40)

1 import sys
2 from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit
3
4 class App(QWidget):
5 def __init__(self):
6 super().__init__()
7 self.title = 'Pyqt5 window'
8 self.left = 10
9 self.top = 10
10 self.width = 300
11 self.height = 300
12 self.initUI()
13
14 def initUI(self):
15 self.setWindowTitle(self.title)
16 self.setGeometry(self.left, self.top, self.width, self.height)
17 self.textbox = QLineEdit(self)
18 self.textbox.move(80, 100)
19 self.textbox.resize(150,40)
20 self.show()
21
22
23 if __name__ == '__main__':
24 app = QApplication(sys.argv)
25 ex = App()
26 sys.exit(app.exec_())

textbox24.py hosted with by GitHub view raw

Output:

6 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

Text Labels
Text labels are simple showing String text showing on any user interface software, Pyqt5
had a Class name Qlabel() that can be used to display text on the screen. You can use
move method it to set the position of your text on the program screen and setText

method to set the label text.

self.label = QLabel(self)
self.label.move(100, 100)
self.label.setText("Hello Pyqt5")

7 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

1 import sys
2 from PyQt5.QtWidgets import QApplication, QWidget, QLabel
3 from PyQt5.QtCore import pyqtSlot
4
5 class App(QWidget):
6 def __init__(self):
7 super().__init__()
8 self.title = 'Pyqt5 window'
9 self.left = 50
10 self.top = 50
11 self.width = 300
12 self.height = 300
13 self.initUI()
14
15 def initUI(self):
16 self.setWindowTitle(self.title)
17 self.setGeometry(self.left, self.top, self.width, self.height)
18 self.label = QLabel(self)
19 self.label.move(100, 100)
20 self.label.setText("Hello Pyqt5")
21 self.show()
22
23 if __name__ == '__main__':
24 app = QApplication(sys.argv)
25 ex = App()
26 sys.exit(app.exec_())

textlabel234.py hosted with by GitHub view raw

Output:

8 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

Buttons
Buttons in pyqt5 are implemented with Qpushbutton class. As we know Buttons are an
important part of any software. QPushButton class takes a string button name
argument. Next, we can use the move function to set the position of our button in the
GUI Screen. First Update your code with the following code lines.

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton


from PyQt5.QtCore import pyqtSlot

We had imported the Buttons classes and Pyqtslot class this will use in Button Action
which you will see in the below upcoming code.

self.button = QPushButton('PyQt5 Button', self)


self.button.move(100,70)

9 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

1 import sys
2 from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
3
4 class App(QWidget):
5 def __init__(self):
6 super().__init__()
7 self.title = 'Pyqt5 window'
8 self.left = 10
9 self.top = 10
10 self.width = 300
11 self.height = 300
12 self.initUI()
13
14 def initUI(self):
15 self.setWindowTitle(self.title)
16 self.setGeometry(self.left, self.top, self.width, self.height)
17 self.button = QPushButton('PyQt5 button', self)
18 self.button.move(100,70)
19 self.show()
20
21
22 if __name__ == '__main__':
23 app = QApplication(sys.argv)
24 ex = App()
25 sys.exit(app.exec_())

button234.py hosted with ❤ by GitHub view raw

Output:

10 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

Button Action:
We implemented a button on our Gui window screen but if we click on it, its do nothing.
We need to make it useful for that we need to set their action that means if we click on
the button their something should be happening. For that purpose, if you remember the
upward code block we imported a PyQt 5 class name pyqtslot that will work as if the
button is pressed then run pyqtslot type method. Check the below code we had made
changes in the code block and added a new line
self.button.clicked.connect(self.on_click) and method type of @pyqtslot .When the
button is pressed Pyqt5 know it is connected to the method name on_click() of type
pyqtslot and execute that method code lines.

@pyqtSlot()
def on_click(self):
print('Button click')

11 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

1 import sys
2 from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
3 from PyQt5.QtCore import pyqtSlot
4
5 class App(QWidget):
6 def __init__(self):
7 super().__init__()
8 self.title = 'Pyqt5 window'
9 self.left = 10
10 self.top = 10
11 self.width = 300
12 self.height = 300
13 self.initUI()
14
15 def initUI(self):
16 self.setWindowTitle(self.title)
17 self.setGeometry(self.left, self.top, self.width, self.height)
18 self.button = QPushButton('PyQt5 button', self)
19 self.button.move(100,70)
20 self.button.clicked.connect(self.on_click)
21 self.show()
22
23 @pyqtSlot()
24Sign up
deffor Top Stories
on_click(self):
25By Level Up Coding
print('Button click')
26
A monthly summary of the best stories shared in Level Up Coding Take a look.
27 if __name__ == '__main__':
28 Emails will be sent to [email protected].
app = QApplication(sys.argv)
Get this newsletter Not you?
29 ex = App()
30 sys.exit(app.exec_())

Programming Python Technology Software Development Coding


MessageBox
Message Box is useful when you want to alert the user by any message or asking user
question. Taking a simple example to understand if you try to close any software on
which you are working and without saving software will ask you in the question
message box that do you want to save the work? Perhaps as i mentioned it can be used
for any purpose in your software. If any process in software is done it shows a message
box telling the user process is done. In PyQt5 we had a message box class that takes a
self argument, String Text, and Question Yes and Question No argument so that it makes
Question messages box and on the next line if you see the code below we had
implemented a if-else logic to print out the result of user activity on the message box.

12 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

1 import sys
2 from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
About Write Help Legal
3 from PyQt5.QtCore import pyqtSlot
4
5 class App(QWidget):
Get6 the Medium app
def __init__(self):
7 super().__init__()
8 self.title = 'Pyqt5 window'
9 self.left = 50
10 self.top = 50
11 self.width = 300
12 self.height = 300
13 self.initUI()
14
15 def initUI(self):
16 self.setWindowTitle(self.title)
17 self.setGeometry(self.left, self.top, self.width, self.height)
18 self.button = QPushButton('PyQt5 button', self)
19 self.button.move(100,70)
20 self.button.clicked.connect(self.on_click)
21 self.show()
22
23 @pyqtSlot()
24 def on_click(self):
25 self.reply = QMessageBox.question(self, 'Message', "Do you like Pyqt5", QMessageBox
26 if self.reply == QMessageBox.Yes:
27 print('Yes clicked.')
28 else:
29 print('No clicked.')
30
31
32 if __name__ == '__main__':
33 app = QApplication(sys.argv)
34 ex = App()
35 sys.exit(app.exec_())

messagebox34.py hosted with ❤ by GitHub view raw

Output:

13 of 14 12/12/21, 20:30
PyQt5 tutorial: learn GUI programming with Python an... https://levelup.gitconnected.com/pyqt5-tutorial-learn-g...

Conclusion:
We had learned many things so far in PyQt5, this feature we are the common one that can be
used in any software development and Simple Gui programs. You can learn more new things
and discover further Classes of PyQt5 just click this Doc URL. You will be redirected to the
PyQt5 documentation page. This is the beginner's guide to PyQt5 to give you a boost on how
PyQt5 works and how you can use it. I hope this article will find you useful in the future and
feel free to share your response

14 of 14 12/12/21, 20:30

You might also like