1 - About PyQt5

PyQt is a 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 that 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:

pip3 install pyqt5-tools

3 - First PyQt5 graphics window

To create a graphics window in PyQt5, we must:

  1. Import the system module: sys 
  2. Import the 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: root = QWidget()  
  6. Display the window using the show() method: root.show() 
  7. Run the application using the exec_() method: app.exec_() 

Code first graphics window with PyQt5

    # imports necessary classes and modules 
    import sys
    from PyQt5.QtWidgets import QApplication, QWidget

    # create a Qt application with QApplication
    app = QApplication.instance()

    # we check if there is already an instance of QApplication
    if not app:
    # otherwise we create an instance of QApplication
    app = QApplication (sys.argv)

    # We create a window using the QWidget object
    root = QWidget ()

    # we set a title to the window
    root.setWindowTitle ("My first pyqt app")

    # we set the window size
    root.resize (500,250)

    # we visualize the window
    root.show ()

    # execution of the application, execution allows you to manage events

    Which displays a nice window after execution:

    Younes Derfoufi

    Leave a Reply