PyQt5 is a Python package that provides a wrapper for accessing the Qt framework libraries. Qt is a cross platform widget toolkit that allows creating graphical user interfaces. It supports Windows, Android, Linux and Mac. Several options exist for developing GUI applications using Python. For example Pyside, TKinter, PyGTK and Kivy are Python packages for creating GUI applications
The obvious benefit of GUI applications is that they allow the data to remain on the local computer. Also the GUI application is always available since it does not depend on internet connectivity. The drawback of GUI applications is that they are not as easy to access as Web based applications. For instance a GUI application needs to be installed on each computer where it is to run.
Qt 5 Developments Tools is a package that provides tools for the Qt5 framework. It includes the Qt5 Desginer which is a graphical designer for Qt5 applications.
Both the PyQt5 framework and PyQt5 Tools may be installed within a Python virtual environment. For example the command: virtualenv path-to-project will create a folder with standard Python libraries and binaries. This folder works as an isolated environment within which Python applications can be developed.
The benefit of using virtual environments is that they allow packages to be installed independently of other virtual environments or the system wide packages. Once a virtual environment has been created, it needs to activated using the command: source bin/activate. This will import the necessary environment.
To install the PyQt5 package, we need to run the command: pip install PyQt5. The PyQt5 tools package may be installed with the command: pip install qttools5-dev-tools. To test if the PyQt5 package has been successfully installed, enter the command: import PyQt5 from the Python shell. If the command does not give errors, then the PyQt5 has been successfully installed
Creating PyQt5 applications
Python GUI applications are run in the same way as command line applications. The main difference is that instead of output being shown on the console, a graphical window pops up.
Python applications written using the PyQt5 framework all have the same basic structure. The main steps in writing a PyQt5 application are:
- Import PyQt5 libraries with the command: from PyQt5.QtWidgets import QApplication. The import command should only contain the classes that need to be imported
- Create a class that derives from the QMainWindow class
- Create an instance of the QApplication class. After that create an instance of the class that is derived from QMainWindow.
The following code shows the "main" function of a typical PyQt5 application:
if __name__ == "__main__": app = QApplication(sys.argv) mw = MyWindow() my.show() sys.exit(app.exec_())
Using the Qt Designer
The Qt Designer allows generating user interface for Qt5 applications. It allows user interfaces to be created by dragging and dropping widgets. It supports several widgets such as ComboBoxes, Lists, Tables, Buttons, Labels and more. It supports four types of layouts which are Horizontal Layout, Vertical Layout, Grid Layout and Form Layout. These layouts allow widgets to be arranged correctly.
Each layout in the Qt Designer is saved in XML format as a separate file with .ui extension. The PyQt5 framework is a wrapper for the Qt5 Framework libraries which are written in C++. The PyQt5 framework provides classes in Python for creating desktop applications. It provides classes for widgets. For example the QApplication class implements the main window class. Classes such as QLabel and QPushButton implement labels and buttons.
Using Python code for arranging widgets can be difficult, since a preview of the user interface is not immediately available. Fortunately the PyQt5 framework contains a tool called pyuic5 which allows converting the .ui files of the Qt Designer to Python code.
For example the command: bin/pyuic5 -x -o MainWindow.py MainWindow.ui will generate the Python file MainWindow.py from the Qt Designer file: MainWindow.ui. The Python file can then be run using the command: python MainWindow.py. This should run the GUI application. The application should look exactly like the preview in the Qt Designer.
Generating executable files using pyqtdeploy
pyqtdeploy is a tool that allows deploying PyQt applications. It supports deploying applications to desktop platforms and mobile platforms. It supports Linux, Windows, MacOs and Android. pyqtdeploy creates a standalone binary file that can be run on the target platform.
pyqtdeploy can be installed as a Python package using the command: pip install pyqtdeploy. It requires the PyQt5 and Python 3.5 and later.
A useful way to learn about pyqtdeploy is to deploy the demo application that is packaged with pyqtdeploy. The demo application is a simple application written using PyQt5 and Python. The documentation for the demo application describes how to build the application on the target platform.
Generating a standalone binary file using pyqtdeploy requires the following steps:
Identify the PyQt modules and other third party components that are required by the application. These components are specified in the sysroot.json file. The sysroot.json file that is provided with the demo application is a good starting point.
The systemroot contains the Python libraries and third part components required by the application. The document Building a System Root Directory describes how to build a sysroot. Building a sysroot can take several hours depending on the components selected. The system root is then built using the command: pyqtdeploy-sysroot --sysroot sysroot-linux-64/ --source-dir lib --verbose sysroot.json. The pyqtdeploy-sysroot command is part of the pyqtdeploy package.
- The next step is to create a project file that identifies the application source code and all the components used by the application. The project file has a .pdy file extension. It can be created using the pyqtdeploy command. This command opens a graphical application that allows generating the project file. For example use the command: pyqtdeploy pyqt-demo.pdy to edit the project file for the demo application. The document Creating a pyqtdeploy Project describes how to create a pyqtdeploy project.
- The next step is to freeze the Python modules and generate a .pro file for qmake. This can be done using the command: pyqtdeploy-build --sysroot sysroot-linux-64/ --verbose pyqt-demo.pdy. The article Building the Application describes how to do generate the .pro file.
- The next step is to generate the MakeFile using the qmake command. This can be done using the command: [path-to-qmake] pyqt-demo.pro. This command should be run from the target build directory which was generated during the previous step. The path-to-qmake is the absolute path to the qmake binary file within the sysroot. Once the MakeFile has been generated, the next step is to generate the executable file using the command: make.