How to create graphic user interfaces and desktop applications in Python

You can create desktop applications for example by using the Python package "tkinter". But there is also another better way to create easier and better desktop user interfaces in Python.

In this tutorial we will see how to use Qt to create a desktop user interfaces that will be used in a Python application. I created my desktop application "BMI Tracker" like this through Qt and Python. This tutorials shows only how it can be done on the operating system Windows. This could be similar in other operating systems.

1. We need to install the Python package "pyqt5"

It can be installed of course through the Python package manager:

pip install pyqt5

Install also this package too:

pip install pyqt5-tools

 

2. Now we need to create the graphical user interface (GUI)

This program will be used to design the graphical user interface:

designer.exe (QtDesigner)


It is located in this folder "pyqt5_tools", which has this path (should be a little bit similar to yours):

C:\Users\XXXX\AppData\Local\Programs\Python\Python36\Lib\site-packages\pyqt5_tools

 

3. Convert the QtDesigner ui file into Python code

If you are done with the designing of the graphical user interface, then you can convert the QtDesigner ui file into a Python program code.
This can be done with this module:

python -m PyQt5.uic.pyuic -x [FILENAME].ui -o [FILENAME].py

Please put instead of "[FILENAME"] your filename that you want to use. This will create you a new Python file with the created user interface in Python programing code.

 

4. Import your new created Python file into your python project

Now we have reached the last step. Copy the new created python file to your Python project folder.
After that you can import your Python file and use it in your programing code.  
Here an example on how to import and use this Python file, if your Python file is in the root path of your Python project folder and it has the name "mainwindow":

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from mainwindow import Ui_MainWindow
import sys

class MainApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainApplicationWindow, self).__init__()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


def main():
    app = QtWidgets.QApplication(sys.argv)
    application = MainApplicationWindow()
    application.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

The packages "QtCore", "QtGui", "QtWidgets" are needed for the components and styling of the graphical user interface. The package "QIcon" is needed the icon of this application. You can also copy instead the content of the Python file of the user interface into your Python program.
 
Please check out also my other tutorial on how to distribute python applications, which will show how to create a runnable and installable Python application.

Cookies make it easier for us to provide you with our services. With the usage of our services you permit us to use cookies.
Ok