Install PyQt5 & First Program 'Hello World !'

1 - About PyQt5 Library

PyQt is a Python library considered to be a link of the Python language with the Qt GUI toolkit, which can be quickly installed with the pip utility and implemented as a Python module. PyQt is a free software developed by the firm Riverbank Computing. It is available under conditions similar to earlier Qt versions and this means a variety of licenses, including the GNU General Public License (GPL) and the Commercial License, but not the GNU Lesser General Public License (LGPL). PyQt supports Microsoft Windows as well as various versions of UNIX, including Linux and MacOS (or Darwin). PyQt implements around 440 classes and over 6,000 functions and methods.

2 - Installation of PyQt5 and first program

2.1 - Installation of PyQt5

The PyQt5 library can be easily installed via the pip utility:

pip3 install pyqt5 

After installing the PyQt5 library, we need to install the auxiliary tools using the command prompt:

pip install pyqt5-tools 

2.2 - First PyQt5 graphics window

To create a graphics window in PyQt5, we must: 

  1. Import the system module: sys 
  2. Import the QApplication class that generates the application: QApplications from the PyQt5.QtWidgets package 
  3. Import the QWidget class from the PyQt5.QtWidgets package 
  4. Create an application using the instance method of the QApplication class 
  5.  Create a window with the QWidget() method: win = QWidget () 
  6.  View the window using the show() method: () 
  7.  Run the application using the exec_() method: app.exec_ ()

Code of first graphics window with PyQt5:

import sys
from PyQt5.QtWidgets import QApplication, QWidget

# create a PyQt5 application
app = QApplication(sys.argv)

# create a QWidget object
win = QWidget()

# Make a window title
win.setWindowTitle("This is a first PyQt5 app !")

# define a geometry of the window
win.setGeometry(100 , 100 , 500 , 400)

# apply the show method to view the window


3 - First PyQt5 window according to the object approach

import sys
from PyQt5.QtWidgets import QApplication, QWidget

class MyWindow(QWidget):

def __init__(self , win ):
super().__init__() = win

def build(self):"PyQt5 from object approach") , 100 , 400 , 150)

if __name__ == '__main__':
app = QApplication(sys.argv)
root = QWidget()
mywin = MyWindow(root)
Younes Derfoufi

Leave a Reply