Simple Calculator In Python

Simple Calculator In Python Using Tkinter

In this tutorial, we are going to learn how to make a simple calculator in python using Tkinter. A calculator is a simple application that we use daily to compute our basic mathematical numbers. We use them to solve specific problems as we face them. The input field, buttons, and logic in our code defined in functions are used to create the appearance of our calculator. For example, if you want to add two numbers, there must be logic for addition purposes behind this action. Likewise, if you want to subtract two numbers, there must be logic for subtraction purposes behind this action. We have an input field where the user’s input will be shown along with the calculation’s outcome.

simple gui calculator program in python

Additionally, the buttons include 0 through 9, +, -, *, /, =,., and C. (clear button)

How do calculators work?

For those who are unaware, a calculator is essentially a computer application that mimics the functionality of any hand-held calculator used for conducting mathematical calculations. It is a pretty simple tool that we utilize on a daily basis. These days, a calculator application is available on all cell phones. This will be the same working mechanism as our simple calculator in python using Tkinter.

There are primarily two processes involved in designing any GUI application:

Making a User Interface is the initial stage.

The most crucial phase is the second one, which involves giving the GUI new functionality.

Let’s start by building a straightforward calculator application in Python using Tkinter, which is used for simple arithmetic computations.

What Is Tkinter

For the purpose of developing Graphical User Interfaces (GUI) for Desktop Applications, Python’s Tkinter standard library is used. Tkinter makes it possible to create desktop apps. This lesson will walk you through how the Tkinter module functions and all of its many parts, including Windows, Widgets, and Frames, before introducing you to all of the Tkinter widgets that are available for creating desktop applications. We have a few beginner-friendly Tkinter projects at the end of the lesson, including a calculator, text editor, and music player applications.

Learn Python first, and practice writing Python applications to hone your skills before moving on to Tkinter.

Simple Calculator In Python Using Tkinter

Now it’s time to take a look at the code to create a Calculator App using Tkinter. With the help of the many functions in Tkinter, it is simple and practical to create a straightforward calculator using only this small amount of code. In our code, we have defined the following functions in addition to the Tkinter widgets:

btn click() function: This function takes care of adding numerous numeric buttons to the action by handling button clicks on those buttons.

Function bt clear(): In the Calculator application, this function manages the clear operation to remove the previous input.bt_equal() Function:

bt_equal() Function: The equal button is handled by the bt equal() function, which performs the action and displays the outcome.

We will now display a screenshot as the result of the aforementioned code. And certainly, you can apply it to your system to make things clearer.

Implementation

from tkinter import *

win = Tk() # This is to create a basic window
win.geometry("312x324")  # this is for the size of the window 
win.resizable(0, 0)  # this is to prevent from resizing the window
win.title("Calculator")

###################Starting with functions ####################
# 'btn_click' function : 
# This Function continuously updates the 
# input field whenever you enter a number

def btn_click(item):
    global expression
    expression = expression + str(item)
    input_text.set(expression)

# 'bt_clear' function :This is used to clear 
# the input field

def bt_clear(): 
    global expression 
    expression = "" 
    input_text.set("")
 
# 'bt_equal':This method calculates the expression 
# present in input field
 
def bt_equal():
    global expression
    result = str(eval(expression)) # 'eval':This function is used to evaluates the string expression directly
    input_text.set(result)
    expression = ""
 
expression = ""
 
# 'StringVar()' :It is used to get the instance of input field
 
input_text = StringVar()
 
# Let us creating a frame for the input field
 
input_frame = Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2)
 
input_frame.pack(side=TOP)
 
#Let us create a input field inside the 'Frame'
 
input_field = Entry(input_frame, font=('arial', 18, 'bold'), textvariable=input_text, width=50, bg="#eee", bd=0, justify=RIGHT)
 
input_field.grid(row=0, column=0)
 
input_field.pack(ipady=10) # 'ipady' is internal padding to increase the height of input field
 
#Let us creating another 'Frame' for the button below the 'input_frame'
 
btns_frame = Frame(win, width=312, height=272.5, bg="grey")
 
btns_frame.pack()
 
# first row
 
clear = Button(btns_frame, text = "C", fg = "black", width = 32, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: bt_clear()).grid(row = 0, column = 0, columnspan = 3, padx = 1, pady = 1)
 
divide = Button(btns_frame, text = "/", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click("/")).grid(row = 0, column = 3, padx = 1, pady = 1)
 
# second row
 
seven = Button(btns_frame, text = "7", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(7)).grid(row = 1, column = 0, padx = 1, pady = 1)
 
eight = Button(btns_frame, text = "8", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(8)).grid(row = 1, column = 1, padx = 1, pady = 1)
 
nine = Button(btns_frame, text = "9", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(9)).grid(row = 1, column = 2, padx = 1, pady = 1)
 
multiply = Button(btns_frame, text = "*", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click("*")).grid(row = 1, column = 3, padx = 1, pady = 1)
 
# third row
 
four = Button(btns_frame, text = "4", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(4)).grid(row = 2, column = 0, padx = 1, pady = 1)
 
five = Button(btns_frame, text = "5", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(5)).grid(row = 2, column = 1, padx = 1, pady = 1)
 
six = Button(btns_frame, text = "6", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(6)).grid(row = 2, column = 2, padx = 1, pady = 1)
 
minus = Button(btns_frame, text = "-", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click("-")).grid(row = 2, column = 3, padx = 1, pady = 1)
 
# fourth row
 
one = Button(btns_frame, text = "1", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(1)).grid(row = 3, column = 0, padx = 1, pady = 1)
 
two = Button(btns_frame, text = "2", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(2)).grid(row = 3, column = 1, padx = 1, pady = 1)
 
three = Button(btns_frame, text = "3", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(3)).grid(row = 3, column = 2, padx = 1, pady = 1)
 
plus = Button(btns_frame, text = "+", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click("+")).grid(row = 3, column = 3, padx = 1, pady = 1)
 
# fourth row
 
zero = Button(btns_frame, text = "0", fg = "black", width = 21, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(0)).grid(row = 4, column = 0, columnspan = 2, padx = 1, pady = 1)
 
point = Button(btns_frame, text = ".", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click(".")).grid(row = 4, column = 2, padx = 1, pady = 1)
 
equals = Button(btns_frame, text = "=", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: bt_equal()).grid(row = 4, column = 3, padx = 1, pady = 1)
 
win.mainloop()

FAQ

How to learn the program step-by-step?

Learning to code nowadays has become a trend. Especially for the younger generations. Why should not be it so? But people often hastily rush to learn to program. If you want to learn efficiently then the first thing you will do is know what you are willing to do. Does this mean that what’s your purpose of learning to program? If you just want to learn it for fun then go ahead and choose the topic of your own. But if you are serious about it and want to land a job as a software engineer then you need to follow some rules of it.
1. Pick any one language and start learning about its basics. Will say start with C programming and it helps you to build your fundamental concepts more clearly.
2. Then once you learn its fundamental things, learn some advanced concepts.
3. Do some mini projects
4. Upgrade yourself too much higher programming languages like C++, Python, Java, JavaScript, Rust
5. Start preparing to learn Data Structure and Algorithms.
And keep learning and applying your knowledge to build live projects.

Conclusion

Using Tkinter and the many Tkinter widgets that we covered in our Tkinter Article, we created a simple calculator application in this tutorial. To explore additional apps created using Tkinter and put your new knowledge into practice, click Next. If you have any questions related to this topic feel free to leave a comment below the blog. we will make sure to respond to you as quickly as we can.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.