Python Calculator – Create A Simple GUI Calculator Using Tkinter

Hey python developers, in Python Calculator tutorial you will learn to create a simple GUI calculator. In this post i will show you how to develop a simple calculator using tkinter module in python. So let’s move further to create a simple python calculator.

Python offers a lot of options to develop  GUI applications, but Tkinter is the most usable module for developing GUI(Graphical User Interface). Since Tkinter is cross-platform so it works on both windows and Linux. So here i am using Tkinter module to create a simple python calculator.

Python Calculator Tutorial – Getting Started With Tkinter

The fastest and easiest way of developing GUI applications in python is working with Tkinter. So let’s took a quick look on Tkinter.

What is Tkinter

  • The Tkinter module (“Tk interface”) is the standard Python interface to the Tk GUI toolkit.
  • Both Tk and Tkinter are available on most Unix platforms, as well as on Windows systems.
  • If you work on Linux/Ubuntu then you have to install Tkinter module on your machine for running programs, but on windows you don’t need to install it because it comes with the default python installation.

Prerequisite For Python Calculator

For developing a simple GUI calculator in python you must have to prior knowledge of following –

Look Of A Simple GUI Python Calculator

Look at this calculator. Yup this is looking very cool. So now we have to create this calculator in python.

Python Calculator
Python Calculator

On this calculator we can perform following simple mathematical calculations –

  • Multiplication
  • Addition
  • Subtraction
  • Division

And now we will start writing codes for making this. To create this we have to do four things –

  • Importing Tkinter module
  • Creating the main interface(window for calculator)
  • Adding any number of widgets to the main interface
  • Applying the event trigger on widgets

Creating Window For Calculator

First of all we will create a window of calculator. So write the following code.

Let’s see the output.

Python Calculator
Python Calculator

We have created this frame successfully, now lets move on ahead.

Adding Display Widget

For creating display widget write the following code inside the class app.

So now the output is –

Python Calculator
Python Calculator

Adding Clear Button Widget

Now we will create a clear button. Whenever this button will be pressed all the stuffs from display will be erased. So write the following code for this.

And now you can see the output.

Python Calculator
Python Calculator

Adding Numbers And Symbols Widget

To add numbers and symbols inside the frame, you have to write the following code.

And now you can see the output. It’s looking cool.

Python Calculator
Python Calculator

Adding Equal Button

For implementing Equal button write the following code.

So now our calculator looks like as below.

Python Calculator
Python Calculator

Applying Event Trigger On Widgets

Now the last thing but very important is to apply event triggers on widgets. It means whenever you click any widget some function will be performed. So write the following code

Complete Code For Python Calculator

So now i am collecting and keeping  all the above codes in one place. So our entire code for making a simple calculator in python is below.

So now its time to see the calculator and you can perform any operation on it. So let’s start your calculation.

Python Calculator
Python Calculator

Congratulations, We have created python calculator successfully, that’s pretty cool, and i hope you have really enjoyed while making this. So surely comment your feelings about this.

Recommended Articles

So Guys this was all about the Python Calculator Tutorial. I hope, you liked it and if it is helpful for you then please share it with others and help them in learning python. And if you have any question regarding this post then leave your queries in comment section. In upcoming posts i will come with some interesting python applications, and if you want to learn making any particular application then suggest me your idea, i will try to fulfill your need. Thanks All of you.

9 thoughts on “Python Calculator – Create A Simple GUI Calculator Using Tkinter”

  1. Gulsanober Saba I have a question that how can we add more functions in that calculator such as square-root, under-root or some more functions???

    Reply

Leave a Comment