Python Engineering: GUI Programming with PyQt and Qt Designer

Posted by


Engineering Python 17A: PyQt and Qt Designer for GUI Programming

Introduction:
PyQt is a set of Python bindings for the Qt application framework developed by Digia and the Qt Project. PyQt allows you to create cross-platform applications with native look and feel using the Qt widgets and libraries. Qt Designer is a visual design tool provided with PyQt that enables you to design and create GUI applications quickly and easily. In this tutorial, we will explore how to use PyQt and Qt Designer for GUI programming in Python.

Prerequisites:
Before starting this tutorial, you should have some basic knowledge of Python programming and GUI programming concepts. You should also have PyQt installed on your system. PyQt can be installed using pip by running the following command:

pip install PyQt5

You also need to have Qt Designer installed on your system. Qt Designer is included in the PyQt package and can be launched by running the following command:

designer

Creating a PyQt Application:
To create a PyQt application, you first need to import the necessary modules. Create a new Python file and add the following code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget

Next, create a class that inherits from the QWidget class and add the necessary code to create a basic application window:

class MyApp(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('My PyQt Application')
        self.setGeometry(100, 100, 400, 300)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

Save the file and run it using the following command:

python myapp.py

You should see a window with the title "My PyQt Application" and dimensions of 400×300 pixels.

Using Qt Designer:
Qt Designer is a visual design tool that allows you to design GUI applications by dragging and dropping widgets onto a form. To create a new form using Qt Designer, launch Qt Designer from the command line and select "File > New…" from the menu. Choose the design template you want to use and start adding widgets to the form.

Once you have designed your form, save it as a .ui file. You can now convert this file into Python code using the pyuic5 tool that comes with PyQt. Run the following command to generate the Python code from the .ui file:

pyuic5 myform.ui -o myform.py

The generated Python code can be imported into your application and used to create the GUI form. Update your application code to import the generated Python file and create an instance of the form:

from myform import Ui_Form

class MyApp(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

Save the modified file and run it using the following command:

python myapp.py

You should see the same window as before, but this time it will have the widgets that you designed in Qt Designer.

Conclusion:
In this tutorial, we have explored how to use PyQt and Qt Designer for GUI programming in Python. We have created a basic PyQt application and used Qt Designer to design a GUI form that we then imported into our application. PyQt and Qt Designer make it easy to create cross-platform GUI applications with native look and feel, allowing you to focus on developing the functionality of your application. With practice and experimentation, you can create sophisticated and user-friendly applications using PyQt and Qt Designer.

0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@YongWang
1 month ago

Hi Guys, please comment and let me know what you think about this Engineering Python open course. If you enjoy the video, please subscribe and share with your friends. Thanks!

@diakitem.youssouf7916
1 month ago

thanks very much for this video .

But i have a question

After the convertion to a python file How to link the widgets and some functions

@ronfredericksbiophysicslab9984
1 month ago

Suggestion… To find QT Designer in Windows PC: I open "Anaconda Prompt" then type "Designer". No need to hunt for the program from the start menu.

PS… Great course and well done set of videos to learn Python on Youtube.