Make Your First Desktop GUI Application Using Python PyQT

Adivardhan Maheshwari
The Startup
Published in
4 min readOct 1, 2020

Ever wondered — How to create GUI applications easily? Is it easy? How to get started? How can it be done using Python?
Well there are multiple Python modules that can be used to make GUI. The most popular ones are PyQT and Tkinter. We will be using PyQT5 in this tutorial.

PyQt is a Python binding of the cross-platform GUI toolkit Qt, implemented as a Python plug-in. Meaning, we can create Desktop GUI applications using Python by using PyQT. You can go through the documentation here.

For this tutorial, we will be using Anaconda which has PyQT5 installed by default. A basic understanding of Python is expected to follow this tutorial.

After installation, to launch the GUI version of PyQT, go to Anaconda3/library/bin/designer.exe

Initial screen on Launch

We will select a blank window for this execution.

Our playground

Let’s the make the hello world version of Calculators. We will take two numbers as input, add 4 functions — add, sub, mul, div

On the left hand side, you will see basic drag and drop templates that can be modified as to your requirement.

Let’s start of with two input text box Text Edit. Simply drag and drop them on the center window.

In pyQT everything is an object. So we need to name the boxes to program later. Let’s just name them as number1 and number2 respectively. To rename them, look at the right side of the screen.

Change the MainWindow as number1. Repeat the same for the other box.

We will need 4 buttons to do different objects. Just like Text Edit widget, you will find PushButton. Drag and drop them to the center screen.

Select the boxes you want to align in a row, on the top right go in form section and layout as you want. Gives it a better unified look.

I have done two things here:

  1. renamed Object Names ( just like last time)
  2. Changed the label names ( by double clicking on the button <it is editable>)

Now to display the final answer, let’s add another Text Edit and name it as answer. Let’s add Label as well for number1, number2 and answer boxes.

Finally, save it in a folder (simpleCalc.ui) and we will move to Python finally.

I will be using VS code for this bit. I have also added an extension PyQT integration as well.

This is how your folder directory will look.

Let’s create a new file main.py

This is how you should initialize the code. We will not be going into the details here. This code will remain same for most of your applications.

Now, let’s add life to this code. We know the object names which we have already in GUI. We will need to perform all the operations inside MyApp class.

An action is performed when the Push Button is clicked. We have four such buttons, so we will add four statements, one for each

self.addNumbers.clicked.connect(self.add)

addnumbers is the object name and self.add is pointing towards the function add.

number1 = int(self.number1.toPlainText())

This is done to convert the input text to Integer so as to perform the mathematical operations.

self.answer.setText(solution)

answer is yet another object to which we are pushing the solution.

This is how the final iteration of your code will look like.

Save everything and then run using the command ( remember to enter this command after going to the path in cmd) else just press F5 in VsCode.

python main.py

Checkout the complete repo here : https://github.com/adivardhan1000/pyQT/tree/main/SimpleCalc_Medium

Connect with me: LinkedIn

--

--

Adivardhan Maheshwari
The Startup

Cybersecurity Researcher. Python enthusiast. Tech explorer. Dabbling with Automation.