Build a simple calculator using python and PyQt5
- Tejas Achar
- May 24, 2020
- 2 min read
Updated: Oct 8, 2021

The full code of this project on git :https://github.com/Tejas-Achar/Simple-Calculator
Hi again!!
That root cause of writing this post is to have a glimpse of how UI development using python looks like.
The simplest application that I could think of, to demonstrate, is a simple calculator.
So lets dive in and see how to go about this.
Prerequisites :
PyQt5 [ pip install PyQt5 ]
Qt designer [ download from : https://tinyurl.com/yc3xmac5 ]
Once you setup PyQt5 and Qt designer, you're all set to go.
Before we start with the GUI, lets write a simple script, which performs the calculator operation.
Lets take user input of the equation ( Ex : 1+2*3 ) as a string
userInput = input("enter the equation here")
2. Now define a function to perform the calculation part.
def calculate(userInput):
return eval(userInput)
NOTE : eval() is an inbuilt method of python which parses the string parameter that i pass into an expression. The string can be a python code or an arithmetic or algebraic expression.
More details on eval method please refer :
3. Now that we have the function definition in place, lets pass the user input to the function call.
calculate(userInput)
## or
print(calculate(userInput)) to see the answer.
And that's it you have the calculator functionality set up.
Now lets build a project in QT for making this a GUI application.
This is what we are building today

1. Create a new project on QT designer.

2. Select the option with "Window ( UI file )".

3. Enter project name and path.

4. Enter the class name as below :

5. Click of Finish to end the setup!
6. On the left panel you can see the three files generated [ main.pyproject, form.ui, main.py]

7. Double click on form.ui which opens a drag and drop environment using which u can create input fields and buttons.
8. Follow the below video to setup the button layout :
Now lets convert the form.ui file to a python file
1. Go to the directory where the "form.ui" file is present.
2. Open command prompt in the same directory.
3. Run this command :
pyuic5 form.ui > calculator.py
4. Now "calculator . py " file must be generated in the same directory.
When you open the .py file in a text editor you can see the code.
Now we need to link all the buttons to type particular values in the input field
Lets start with the clear button :
Add this line at the end of "setupUi" method in the auto generated python code
self.pushButton_9.clicked.connect(self.lineEdit.clear)
Note : ( pushButton_9 might be a different button in your case check the labels and then use the push buttons for method calls )
Now let us write two methods at the beginning of the class "Ui_calculator"
def calculate(self,x):
self.lineEdit.clear()
def calci():
self.lineEdit.setText(self.lineEdit.text()+ x)
return calci
def equate(self):
self.lineEdit.setText(str(eval(self.lineEdit.text())))
Now for the other remaining buttons :
Go back to "setupUi" method and connect them all to "calculate" method :
self.pushButton_16.clicked.connect(self.calculate('1'))
Similarly write for all the other remaining lines.
And that's it!!
Now u can run the .py file and get an output similar to this :

The full code of this project on git : https://github.com/Tejas-Achar/Simple-Calculator
If you want to convert this into a .exe file, follow this post :
Peace out!!
Comentarios