Python PyQt5 documentation and installation Windows
There are many packages provided by Python for developing the GUI application. The Pyqt is most popular among them. It is one of the most popular and powerful cross-platform GUI library. It is a blend of the Python programming language and the Qt library. It is available in both GNU General Public License (GPL) and commercial license.
Before we provide more overview about PyQt5, you should first know about the Qt. Qt is a free and open-source widget toolkit for creating graphical user interfaces as well as cross-platform applications that is platform independent and run on various software and hardware platforms such as Linux, Windows, macOS, Android or embedded systems.
The Pyqt5 is a standard cross-platform package for creating graphical user interfaces (GUIs). It has implemented around 440 classes and over 6,000 functions and methods. These classes cover a range of features, like -
- Graphical User Interfaces
- XML processing, Rendering HTML
- SQL Databases
- Networking
- OpenGL support and much more
There are two versions of PyQt generally in use - PyQt4 and PyQt5. PyQt5 is only compatible with the Python version 3.*. It is recommended to use PyQt4 for older versions of Python.
Install PyQt5
Before installing the PyQt5, it is recommended to install the virtual environment (virtualenv). It provides a new virtual environment for your project and creates your own directory.
c:\Python38\Scripts\projects>virtualenv env
Once installed, activate the virtual environment using the following command -
c:\Python38\Scripts\projects>cd env/Scripts
c:\Python38\Scripts\projects\env\Scripts>activate
(env) c:\Python38\Scripts\projects\env\Scripts>
The above command modifies the shell prompt and indicates which virtual environment is currently active. Next, run the command to install the PyQt5 module with the pip tool.
pip install PyQt5
Install PyQt5 Tools
The PyQt5 package does not provide QtDesigner tools that were included in older versions, but we can install this separately using the pip tool. The QtDesigner comes with the pip package pyqt5-tools.
pip install pyqt5-tools
PyQt5 Hello World Example
Creating a simple PyQt gui application, first we need to import the QApplication and QWidget modules. Then, we create an application object. The QWidget() creates a top level widget. We can define the size, title of the window using the widget object. At last, we have used the app.exec_() method to enter the main loop of the application.
The following code opens a blank window -
import sys
from PyQt5.QtWidgets import QApplication, QWidget
app = QApplication(sys.argv)
w = QWidget()
w.resize(300,300)
w.setWindowTitle('etutorialspoint')
w.show()
sys.exit(app.exec_())
Here is the simple example of PyQt5 to print 'Hello World' -
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
app = QApplication(sys.argv)
w = QWidget()
w.resize(300,200)
w.setWindowTitle('etutorialspoint')
l = QLabel(w)
l.setText("Hello World!")
l.move(50,50)
w.show()
sys.exit(app.exec_())
PyQt5 Widgets
Widgets are the components of any user interface that the user can interact with. These are the some PyQt5 widgets -
QLabel | It is used to display text or image. |
QLineEdit | It allows one line of text to be entered by the user. |
QTextEdit | It allows the user to enter multi-line text. |
QPushButton | It is a command button and used to invoke action. |
QRadioButton | It is used to select one from multiple options. |
QComboBox | It is used to display a dropdown list of selectable items. |
QCheckBox | It is used to select multiple choices from the options. |
QMenuBar | It is used to display a horizontal menu bar at the top of a window. |
QToolBar | It is a horizontal bar or pane that may contain buttons and other widgets. |
QTab | It consists of tab bar and tab page. It break down the page content into multiple pages and link each page with the tabs at the top of window. |
QScrollBar | It is used to create scroll bar. |
QSplitter | It splits the content both horizontally and vertically. |
PyQt5 Widgets Example
These are the following widgets example -
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
class displaywidget(QMainWindow):
def __init__(self, *args, **kwargs):
super(displaywidget, self).__init__(*args, **kwargs)
self.setWindowTitle("PyQt5 Widgets")
layout = QVBoxLayout()
widgets = [QCheckBox,
QComboBox,
QDateEdit,
QDateTimeEdit,
QRadioButton,
QSlider,
QSpinBox,
QTimeEdit]
for w in widgets:
layout.addWidget(w())
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
app = QApplication(sys.argv)
window = displaywidget()
window.show()
sys.exit(app.exec_())
Layout
The PyQt5 provides a layout management system to create advance user interfaces for application. It sets the positioning of widgets on the application window. These are the following mostly used layouts of PyQt5 -
QHBoxLayout | It lines up the widgets horizontally. |
QVBoxLayout | It lines up the widgets vertically. |
QGridLayout | It divides the space into rows and columns. |
QVBoxLayout
The window contains one or more widgets. If we want to align all the widgets vertically, the QVBoxLayout class is the only solution.
import sys
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
w = QWidget()
w.resize(300,300)
w.setWindowTitle('etutorialspoint')
l = QLabel(w)
l.setText("Hello World!")
l.move(50,10)
b1 = QPushButton("Button1")
b2 = QPushButton("Button2")
vbox = QVBoxLayout()
vbox.addWidget(b1)
vbox.addWidget(b2)
w.setLayout(vbox)
w.show()
sys.exit(app.exec_())
QHBoxLayout
The QHBoxLayout class is used to align all the widgets horizontally.
import sys
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
w = QWidget()
w.resize(300,300)
w.setWindowTitle('etutorialspoint')
l = QLabel(w)
l.setText("Hello World!")
l.move(50,10)
b1 = QPushButton("Button1")
b2 = QPushButton("Button2")
hbox = QHBoxLayout()
hbox.addWidget(b1)
hbox.addWidget(b2)
w.setLayout(hbox)
w.show()
sys.exit(app.exec_())
QGridLayout
The QGridLayout class is used to present the objects in a grid of rows and columns.
import sys
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
w = QWidget()
w.resize(400,400)
w.setWindowTitle('etutorialspoint')
qgrid = QGridLayout()
for i in range(1,5):
for j in range(1,5):
qgrid.addWidget(QPushButton("A"+str(i)+str(j)),i,j)
w.setLayout(qgrid)
w.show()
sys.exit(app.exec_())
Related Articles
Python multiline string with variablesGlob Python install
Python heapq
Zip in Python for loop
Remove element from list Python
Check if list is empty Python
Python remove element from list by value
Python SMTP server
How to generate QR code in Python 3
Palindrome program in Python using recursion
CRUD operations in Python using MySQL Flask
Fibonacci series in Python using function
Python flock
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml
Convert Python list to numpy array
Convert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window