PyQt5 - Hello World Example

In this article, I am going to show you how we create a simple Python GUI application using Qt Designer and run it on the Python IDLE.

Introduction

 
PyQt is a Python binding cross-platform C++ framework GUI toolkit that can be used to develop the feature-rich user interface of Python applications. It was developed by Riverbank computing and released under the GNU GPL, and its commercial license can run on Windows, Mac, and all Unix platforms. PyQt5 supports to make desktop and mobile applications.
 
This article demonstrates how to create a "Hello World" Python GUI application by using Qt Designer and standard Python IDLE.
 
Requirements
  • PyQt5 Tool (Qt Designer)
  • Python IDLE
Steps to be followed,
 
Step 1 - Create a new form
  • Open the Qt Designer on your local machine.
  • The number of available templates will appear. As I said, we will design to create a simple desktop app, so start with the "main" template. Chose the "Main Window" template.
  • Click on "Create".

    PyQt5 - Hello World Example
Step 2 - Insert widgets
  • Choose “label” under the Display widgets and place it on the main window.

    PyQt5 - Hello World Example

  • On the main window, drag the push button from the Buttons widget.
  • To change the Button name, go to Properties >>QApstractButton>>Text>> ”OK”.

    PyQt5 - Hello World Example
Step 3 - Save .ui file
 
Click File >> Save >> “main.ui” and hit the Save button.
 
PyQt5 - Hello World Example
 
Step 4 - Change .ui to .py file format
  • Now, we are ready to convert .ui to a .py file format. Thus, open the Qt resource file and run the CMD in it.
  • In cmd, type the below codes and hit Enter to change their format.

    PyQt5 - Hello World Example

  • After converting, the new “main.py” will get created.

    PyQt5 - Hello World Example
Step 5 - Programming with Python IDLE
  • Now, right-click the "main.py" to edit with Python IDLE.

    PyQt5 - Hello World Example

  • Let us add the following code in this Python file.
  1. self.pushButton.clicked.connect(self.on_click)    
  2.         
  3. def on_click(self,QLabel):    
  4.         self.label.setText("Hello World")    
In the above code, in line 1, when the Push button is clicked, the connect keyword will call our defining Function if the label object can be used to set Text to the QLabel widget with the help of the "setText()" function.
 
PyQt5 - Hello World Example
 
Step 6 - Run the application
 
Now, we are ready to run our project.
  • Click Run >> Run module.
  • Click the “OK” button then the “Hello World” text will appear at the label widget.

    PyQt5 - Hello World Example

Conclusion

 
In this demo, we have learned how we create the "Hello World" Python GUI app by using PyQt5 and Python IDLE. I hope the article will help people who are new to Python GUI development.