Python GUI Programming Tutorial using PyQt and Qt Designer

Posted by


Introduction
Qt Designer is a visual design tool for creating PyQt GUI applications. PyQt is a set of Python bindings for the Qt application framework and runs on all platforms supported by Qt including Windows, Mac OS X and Linux. In this tutorial, we will show you how to use Qt Designer to create a simple GUI application using PyQt.

Step 1: Install PyQt
Before you can start using Qt Designer, you need to install PyQt on your system. You can install PyQt using pip by running the following command:

pip install pyqt5

Step 2: Open Qt Designer
Once you have PyQt installed, you can open Qt Designer by running the following command in your terminal:

designer

This will open the Qt Designer window where you can design your GUI application.

Step 3: Create a New GUI Application
In Qt Designer, go to File > New and select the type of form you want to create. For this tutorial, we will create a simple dialog-based application.

Step 4: Design the GUI
Once you have created a new form, you can start designing the GUI by dragging and dropping widgets from the widget box onto the form. You can customize the properties of each widget in the property editor on the right side of the window.

Step 5: Save the Form
Once you have finished designing your GUI, you can save the form by going to File > Save or by pressing Ctrl + S. Save the form with a .ui extension.

Step 6: Generate Python Code
To use the form in your PyQt application, you need to convert the .ui file to a .py file. You can do this by running the following command in your terminal:

pyuic5 -x your_form.ui -o your_form.py

This will generate a Python file that you can use to create your GUI application.

Step 7: Create a PyQt Application
Now that you have the Python file generated from Qt Designer, you can create a PyQt application that uses the form. Here is an example of a simple PyQt application that uses the form you created:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from your_form import Ui_MainWindow

class MyDialog(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

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

Replace your_form with the name of the .py file generated from Qt Designer. This code creates an instance of your form and shows it as a dialog window.

Step 8: Run the Application
To run the application, save the Python code to a file and run it using Python. You should see your GUI application open with the form you created in Qt Designer.

Conclusion
In this tutorial, we have shown you how to create a simple GUI application using Qt Designer and PyQt. Qt Designer makes it easy to design complex GUI applications by providing a visual design tool that generates Python code for you. With PyQt, you can create cross-platform GUI applications that run on Windows, Mac OS X and Linux. We hope this tutorial has been helpful in getting you started with Qt Designer and PyQt GUI programming.

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

Im just wondering, after you create ur app. and u add some button functionality. Can you edit it again using qtdesigner ?

@maxescobar1249
1 month ago

un aporte si no funciona el pyuic5 en windows https://www.youtube.com/watch?v=2GtYO3KY2HI

@tenderesthobby4767
1 month ago

sentdex, without you im noting…

@johncolasurdo8165
1 month ago

I'm lost when your trying to retrieve it as a batch file. Should I be in the qt designer dir or the python dir?

@Sahonym
1 month ago

After 5 years, your "out dated" tutorial works even in Ubuntu 20.04 ! (Obviously had make so adjusts due to updated versions)
THANK YOU!

@pujilestarigaming
1 month ago

Success

@hermanusbrynard2151
1 month ago

Great stuff thank you. I searched for hours on the web to find a solution to the problem of being unable to launch the uic module to view the code whereas in this video it took 5 minutes to solve this vexing problem. I just wonder why the developers have not rectified it.

@parthgoyal7387
1 month ago

Hi there! I follow your videos and they serve as a great source to learn. Thanks.
Recently I tried this PyQt Designer. After designing the main window and running the preview of ui file its alright. But when I convert into .py file the window size is getting reduced. I dont know why. Can you please help me figure it out?
Size Policy of all the components is set to fixed.

@DasJev
1 month ago

Is Snowden teaching me Python here?

@MrAnim8orVideos
1 month ago

PAGE is the Tkinter version of this: https://www.youtube.com/watch?v=oULe0h0Jl3g

@ruslan.zhumabai
1 month ago

Can i make a software which is able to connect with an arduino bouard. What do you think? I need more skills?

@sbksdg7019
1 month ago

thank you so much

@AhmedAdelSaleh
1 month ago

to convert the GUI to python code 8:00

@orion_222
1 month ago

why did you not include where the code should be typed

@iaggocapitanio7909
1 month ago

Would be a project evolving a deep approach with numerical functions, interactions with matplotlib, pandas and stuffs like that. But, Those videos were very useful

@sksahil4374
1 month ago

how to convert pyqt5 app to APK for Android?

@alielmahmudi5452
1 month ago

A great explanation.
How to create a standalone python "application" using PyCharm with many files in the project?

@bernardferrer
1 month ago

Very clear tuto and very pleasant. Thanks.
As you mention, however, to bad there is no way to generate some logic associated to the objects in the ui. Like onclick button1, display panel 2, etc.
Do you think xcode + Python would be a good solution ?

@luismora3698
1 month ago

every time i need help you already got a vid for it, much thanks for all you do

@aidenfrost9997
1 month ago

Fun Fact : That error still exists even after 4 years xD