Simple GUI Calculator using Tkinter in Python

Raghav Agrawal
4 min readFeb 13, 2021

In this tutorial, we are going to build a simple calculator using the Python Tkinter module. we will also add some additional features to make it a little bit complex. Tkinter is a built-in Python module for creating GUI applications. It is easy to use and comes with python, no need to install it separately.

As in our previous tutorial we have created a Dice Simulator using Tkinter in Python. If you have not done that project then, please first have a look at that because it covers the basic concepts, and you suitable to work with Tkinter. This Project contains a little bit of advancement compared to that and we are going to explore Tkinter to the next level.

The Python Calculator which we are going to build will look something like this:

Step-1) Import Libraries

The first step is to import the necessary libraries. so, we are going to import Tkinter, a parser for parsing the string, and some functions from the math module.

from math import factorial

Step-2) Make a top-level widget for your Calculator

Generating a top-level widget is very simple using Tkinter and can be made using the following steps.

The above code will make a blank window for your calculator with the title “Crazy_Tech Calculator”.

  • Geometry:- Geometry is used to set the calculator window geometry of particular width and height.
  • configure:- configure method is used to have some custom configuration to GUI window. here we have added a background color as light blue to make the calculator look attractive.
  • mainloop:- Call the main event loop so that actions can take place on the user’s computer screen.

Step-3) Designing a Buttons

Now, let’s build Buttons for the Calculator and put them on the main window. First, we will create an empty field where all the calculations will happen(Calculator Screen). Then, we will add all the buttons using the grid geometry method of Tkinter.

command=lambda : get_variable(1)).grid(row=2, column=0, sticky=N+E+W+S)

()).grid(row=2, column=5, sticky=N+E+W+S)

()).grid(row=3, column=5, sticky=N+E+W+S)

get_operation (“)”)).grid(row=4, column=5, sticky=N+E+W+S)

get_operation (“**2”)).grid(row=5, column=5, sticky=N+E+W+S)

In this Calculator program, the Entry function helps in making an input field(calculator screen) and we use the Button method to display a button. We used a .grid() method to position the buttons in the correct way on screen. We have also used a Label method to leave a blank line before displaying a calculator to make it look quite attractive.

If you have noticed an error after running this above code as, no function get_variable() or get_operation() found, then do not worry. we are now going to assign the functionality of each button we have used. till now we have only declared the functions, hence you are receiving an error. let’s quickly define them and map each button functionality.

Step-4) Mapping Buttons to their Functionalities

Mapping the digits with get_variable() method

# I keeps track of the current position of the input

The get_variable method takes digit as parameter input. the digit is inserted into the input field using the insert function with parameters i and num. i keeps the position of input where a digit is being inserted. the global variable i updates each time to insert the next digit or next operator.

def get_operation(operator):

We use the delete method to delete the characters from an input field. it takes the start and end position, so we have cleared the entire screen.

Mapping the undo(<-) button

The undo functions take the entire digits and operator from the input field using the get() method and generated the new string except for the last char of the old string if the string len is positive. And it clears the screen and inserts the new string with 1 char less and updates the input field. And if an input field is empty and we try to use this command then it displays an error.

Mapping the Calculate “=” button

we fetch the string from the input field using the .get() method. we make the use to parse module to scan the string using the expr() method which accepts a string as a parameter. we basically leave it to the parser to build an abstract syntax tree of string which is being evaluated by the eval() function.

When you execute the program, you will able to see the output screen with a calculator and all the functions are correctly working.

Summary

Yay! We have successfully designed a Python Calculator using Tkinter. It was an amazing experience and learning during this simple project. Now, that you have a better understanding of the Tkinter works and capable to work with them. Try to explore it more and what features you can add to this calculator and designs to make it look more attractive like add a justify parameter to the input field to take input from the right side or add a log function.

Originally published at https://rgcrazytech.blogspot.com.

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

No responses yet

Write a response