How to make your own Calculator

Ashwin Kumar Ramasamy
5 min readSep 6, 2020

--

Hi everyone I hope all are fine!

I am hereby going to guide you to make a simple calculator. Yes, here we are going to build a mini GUI application with Python programming language.

If (you are an intermediate or Advance level in python):
print(“Go with another blogs or listen”)
else:
pass

Well, shall we move to?
Yeah, first of all, open your favorite compiler or a text editor like Visual Studio code, Pycharm, notepad++, etc.

Step 1: Open a new file called Calc.py here .py is an extension for python files.

Step 2: Install the Tkinter library on your compiler or command prompt. if you are using a compiler, inside the compiler you can install that. or if you are using text editor install the library using command prompt.

pip install Tkinter

Step 3: Now Import Tkinter library or module.

Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit. Import the Tkinter module.

here we importing all(*) modules from the Tkinter library. Make sure that python is case sensitive so keep your eyes on letter case.

Step 3: Now we are going to create a GUI interface
first, you have to create a variable called root or window anything then you are going to initialize the Tkinter module with that variable

root=Tk()

after that create the main loop function with a root variable.

root.mainloop

Now its time to fix the attributes to our calculator window.
First fix the frame size by using the geometry keyword and give a suitable title for your application using the title keyword.
Then give the color attributes for your application by using the bg keyword.

GUI part

Give the attributes like font color, size, family, etc. inside the Label keyword.
here grid and place keyword is used to place your attributes or whatever you are like to place into your window.

The grid is used to place in the X and Y-axis.
The entry keyword is used to create a text box and the text variable keyword is used to place it into your text box. Frame keyword is used to give a frame line to the user text box.

Step 4: In this part, we are going to fixing the numeric and other operator buttons on our window.

Button Attributes

Under the variable create a button type object using the Button keyword and give the attributes like text, width, height. And place those buttons using the Place keyword on your window.
Here now you don’t give command and lambda functions later we will do that.
Step 5: Good I hope you can now see the simple calculator window on your compiler.

Now it's a time to give a function to our calculator.
First, you have to initialize an exp variable with “ “ globally, It will assign the given expressions like 2+5 or 8*9, etc. on your text box.

function for Clicking values

Now create a function called press() inside the function give a parameter call num so it seems to be press(num):

In this function, we are going to give a function to number clicking and displaying.
Invite the exp variable by using a global keyword, then assign the expression exp with num value. Don’t forget that we have to typecast the integer num value to a string using the str keyword.

exp=exp+str(num).

Now pass the argument exp using a set keyword with the equation , It will denote that the expression you are giving in to the calculator will be stored as an equation.

equation.set(exp).

Step 6: Now we are going to do a function called equalpress(): for calculating the expression.

Calculation part

Under that give an exception for users' situation by trying and except keywords.
This is an important section,
Under the try, block invite that exp globally and create an object for calculating your expression values using the eval keyword.

eval is a built-in- function used in python, eval function parses the expression argument and evaluates it as a python expression. In simple words, the eval function evaluates the “String” like a python expression and returns the result as an integer.

total=str(eval(exp))
equation.set(total)
exp=” ”

here we pass an exp argument with total variable and set the total value to an equation.

Step 7: Now create a function for clear the values by clicking CE button.
under that function we are going to invite exp as globally and set empty (“ “) for clearing the text box.

Clearing the values

Step 8: Now we have completed both GUI and function parts, now just create a command keyword at each variable under Step 4 . And pass the function name without using () brackets.

Eg. seven=Button(text=”7",width=10,height=2,command = lambda : press(7))

Here using lambda function to a precise method of clicking the values.

Python allows you to create anonymous function i.e function having no names using a facility called lambda function. Lambda functions are small functions usually not more than a line. It can have any number of arguments just like a normal function. … Also there is no need for any return statement in lambda function.

Hip Hip Orae !! 🥳🥳
We are completed our mini project 🙌

Final output

Thank You !!!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ashwin Kumar Ramasamy
Ashwin Kumar Ramasamy

Written by Ashwin Kumar Ramasamy

Passionate in software development, network security and technical training.

Responses (2)

Write a response