Qt Designer and Python: Build Your GUI Applications Faster
Tags: pyqt, python
Published: 2021-04-04 12:28:37

[Designer](https://pythonbasics.org/qt-designer-python/) aims to provide a lightweight, yet powerful solution for creating  PyQt5 applications.

It makes it possible to create **GUI applications** with drag and drop. It is based on the [Qt framework](https://qt.io) using Qt Designer, which is a visual layout and forms editor.

For those familiar with GUI programming in Python, you may have used the [tkinter](https://python-commandments.org/tkinter-basics/) GUI library.

PyQt applications can be written in multiple ways: [pure Python](https://pythonspot.com/pyqt5/), QML (and its tools) or from Qt Designer's forms.

![pyqt designer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2y5vb9uc75rwfdcx6ra.png)

Some of the benefits of PyQt Designer:

* The generated code is fully Pythonic. 
* It makes use of the latest Python features such as Decorators, Generators and Lambdas. 
* It makes use of Python 3.
* It uses the Qt styles and themes for maximum consistency with your application design. 
* It can generate code compatible with both Qt4 and Qt5.

It features a simple and powerful forms editor, allowing non-programmers to create complex layouts efficiently.

You can use PyQt with any IDE, including [PyCharm](https://pythonpyqt.com/how-to-install-pyqt5-in-pycharm/) or simply from vim.

If you are new to PyQt, I recommend this course: 
[Create Desktop Apps with Python PyQt5](https://gumroad.com/l/pysqtsamples)

## What is Designer

**Qt Designer is a graphical user interface (GUI) layout and forms editor, which is part of the Qt Framework.**

Qt Designer is a powerful tool for designing [user interfaces](https://en.wikipedia.org/wiki/User_interface). Its primary purpose is to create widget and GUI layout specifications that can be compiled and used in applications written in [C++](https://en.wikipedia.org/wiki/C%2B%2B). However, Qt Designer is also perfectly capable of *generating code for Python applications*. 

### What is PyQt?

PyQt is a set of **Python bindings for the Qt cross-platform GUI framework**. It provides Python applications with a way to quickly create user interfaces that closely resemble those constructed using Qt Designer.

PyQt also includes tools to deploy applications written in Python. However, it makes no attempt to hide the fact that it generates Python code or that it is not as fast as a natively written Python application. 

While PyQt applications are generally faster than their alternatives, they can’t compare well to native applications where the entire application has been written in C++.


## Designer or Python?

There are many differences between code generated by a GUI builder and manual [PyQt code](https://pythonprogramminglanguage.com/pyqt5-hello-world/), and this is one of the strengths of Qt: you have the power to choose what works best for each situation.

There are certainly some situations where an existing Qt Designer UI can be easily converted into Python code. But there are also situations where it makes sense to write directly in Python.

### When Should You Use What?

Most of your GUI code should be created using a GUI builder. This is because a GUI builder provides a convenient, visual way to keep all elements aligned and to see the overall structure of your GUI as you build it.

![pyqt designer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phgrzfb4ftbdhx2f2wdn.png)

The GUI builder also manages the layout for you, so that you don't need to worry about trying to work out just how big each element should be compared to all the others when it comes time to actually draw them onto a screen.

## Load ui files

**The .ui files are XML files that contain user interface definitions along with the layout of widgets within the interface.**

In order to use Qt Designer .ui files directly in your code, you need to use Python to load the data from the .ui file. You can do this with a simple Python script or a more complex program.

PyQt-Python makes it easy to create GUI applications with Python. However, if you want to use Qt Designer .ui files directly in your code, you need to use Python to load the data from the .ui file. You can do this with a simple Python script or a more complex program.

```python
from PyQt5 import QtWidgets, uic
import sys

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('basic.ui', self)
        self.show()

app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()
```

The following simple Python script demonstrates loading a .ui file and displaying the contents of one of the widgets in the designer. Saves the script as userinterface.py.