How to Build a Calculator in Tkinter


Python provides lots of tools for data science, web, and android development. But one of the best tools when it comes to the Python programming language is Tkinter for Graphical User Interface. The Tkinter library in python provides GUI widgets that help create user-friendly applications. Tkinter is easy to use, lightweight, and fast. You don't have to install Tkinter explicitly as it comes preinstalled with Python.

You should know the basics of the Tkinter package in Python before proceeding with this tutorial.

Basic Setup for the App

1. Importing the module

The first step for building the app is importing the module. While importing the Tkinter module, you'll need to instantiate the ttk object.

import tkinter as ttk 

2. Creating the Window Variable

To create a window, you need to create a window object using ttk. After creating a window object, you can assign a title and geometry to the window. The geometry will set the window's height and width.

win = ttk.Tk()win.title('Simple Calculator')win.geometry('500x500')

3. MainLoop

The mainloop() method runs the window in an infinite loop. It runs continuously unless the user closes the window manually.


You can run the Python file using the python command. The application looks like this:

A simple calculator using python Tkinter

Adding Functionality to Buttons:

As we mentioned earlier, the Button widget has a command attribute that accepts a function or method to be called. The methods passed when buttons are clicked use the lambda function.

1. Numeric and Arithmetic Buttons

The numeric buttons consist of values from 0-9 and, the arithmetic buttons are +, -, x, / for calculation purposes.

The addToEq() method gets called upon clicking the button. This method passes numbers or arithmetic operators depending on the button you click. After passing the value, the numbers or operators get stored in the calcValue variable. Before storing the value or operator in the calcValue variable, you must convert it to string type using the str() method.

def addToEq(x):   calcValue = calcValue + str(x)   numericEq.set(calcValue)

2. Calculate Buttons

The button with the label cal calculates the entire string stored in the calcValue variable. The eval() function helps to perform arithmetic operations on the calcValue variable and return the total. After retrieving the value, set the total value in the numericEq variable. The numericEq variable displays this value in the Entry box.

def calculateEq():   total = str(eval(calcValue))   numericEq.set(total)

3. Clear Button

The clear button clears the Entry box. Upon clicking the clear button, the clearInput() method is called. The variable calcValue is set to an empty string, and it's stored in the Entry box.

def clearInput():  calcValue = ""  numericEq.set("")

Once you implement all these methods, the output of the code looks like this:

The calculator with output after calculation

Projects are the Best Way to Improve Your Coding Skills

Now that you've learned how to build a simple calculator using GUI in Python Tkinter, it's time for you to explore other Python packages. Python has a variety of packages that help you build any application you can dream up.

As you likely already know, projects are the best way to showcase your skills. Building projects will help you get a good grasp of the language and build your resume.


Amazon’s Astro: Are the Privacy Concerns Justified?

Previous article

How to Delete Node Modules Folder With npkill

Next article

You may also like


Leave a reply

Your email address will not be published. Required fields are marked *


Login/Sign up