How to Create a Calculator using Python

In this short guide, you’ll see how to create a simple calculator using Python.

The Complete Code

Here is the complete code to create a simple calculator using Python.

import tkinter as tk

root = tk.Tk()

canvas1 = tk.Canvas(root, width=400, height=400)
canvas1.pack()

entry1 = tk.Entry(root)
canvas1.create_window(230, 100, window=entry1)

entry2 = tk.Entry(root)
canvas1.create_window(230, 140, window=entry2)

label0 = tk.Label(root, text="Calculator")
label0.config(font=("arial", 14))
canvas1.create_window(200, 40, window=label0)

label1 = tk.Label(root, text="Type Value 1:")
label1.config(font=("arial", 10))
canvas1.create_window(100, 100, window=label1)

label2 = tk.Label(root, text="Type Value 2:")
label2.config(font=("arial", 10))
canvas1.create_window(100, 140, window=label2)

result_frame = tk.Frame(root, width=250, height=50, bd=1, relief="solid")
canvas1.create_window(200, 240, window=result_frame)

label3 = tk.Label(result_frame, text="Result:", font=("Arial", 10))
label3.pack(side="top")

result_label = tk.Label(
result_frame, text="", font=("Arial", 10), width=20, height=1, bd=1, relief="sunken"
)
result_label.pack(side="bottom")


def add():
v1 = entry1.get()
v2 = entry2.get()
result = float(v1) + float(v2)
result_label.config(text=f"{result}")


button_add = tk.Button(
text="+", command=add, bg="green", fg="white", font=("arial", 9, "bold"), width=5
)
canvas1.create_window(90, 190, window=button_add)


def sub():
v1 = entry1.get()
v2 = entry2.get()
result = float(v1) - float(v2)
result_label.config(text=f"{result}")


button_sub = tk.Button(
text="–", command=sub, bg="green", fg="white", font=("arial", 9, "bold"), width=5
)
canvas1.create_window(140, 190, window=button_sub)


def mul():
v1 = entry1.get()
v2 = entry2.get()
result = float(v1) * float(v2)
result_label.config(text=f"{result}")


button_mul = tk.Button(
text="x", command=mul, bg="green", fg="white", font=("arial", 9, "bold"), width=5
)
canvas1.create_window(190, 190, window=button_mul)


def div():
v1 = entry1.get()
v2 = entry2.get()
result = float(v1) / float(v2)
result_label.config(text=f"{result}")


button_div = tk.Button(
text="/", command=div, bg="green", fg="white", font=("arial", 9, "bold"), width=5
)
canvas1.create_window(240, 190, window=button_div)

root.mainloop()

How to Build the Calculator in Python

Import the tkinter package and create the Canvas

To start, import the tkinter package, which is used to create a Graphical User Interface (GUI) in Python.

Also add the canvas, which is your GUI display in which you can place items, such as buttons, entry boxes, etc.

import tkinter as tk

root = tk.Tk()

canvas1 = tk.Canvas(root, width=400, height=400)
canvas1.pack()

Create the entry boxes

Next, create the entry boxes to collect the data from the user:

entry1 = tk.Entry(root)
canvas1.create_window(230, 100, window=entry1)

entry2 = tk.Entry(root)
canvas1.create_window(230, 140, window=entry2)

Add the labels

There are 4 labels that are currently displayed on the GUI:

  • Calculator
  • Type Value 1:
  • Type Value 2:
  • Result:

You may edit the text, format and/or location where the labels will be displayed.

label0 = tk.Label(root, text="Calculator")
label0.config(font=("arial", 14))
canvas1.create_window(200, 40, window=label0)

label1 = tk.Label(root, text="Type Value 1:")
label1.config(font=("arial", 10))
canvas1.create_window(100, 100, window=label1)

label2 = tk.Label(root, text="Type Value 2:")
label2.config(font=("arial", 10))
canvas1.create_window(100, 140, window=label2)

result_frame = tk.Frame(root, width=250, height=50, bd=1, relief="solid")
canvas1.create_window(200, 240, window=result_frame)

label3 = tk.Label(result_frame, text="Result:", font=("Arial", 10))
label3.pack(side="top")

result_label = tk.Label(
result_frame, text="", font=("Arial", 10), width=20, height=1, bd=1, relief="sunken"
)
result_label.pack(side="bottom")

Create the functions and buttons

There are 4 functions in the code:

  • add() – to add the values
  • sub() – to subtract the values
  • mul() – to multiply the values
  • div() – to divide the values

For each of those 4 functions, there is an associated button that can be used to trigger the function. For example, the “button_add” would trigger the add function to add the values that the user typed in the entry boxes.

def add():
v1 = entry1.get()
v2 = entry2.get()
result = float(v1) + float(v2)
result_label.config(text=f"{result}")


button_add = tk.Button(
text="+", command=add, bg="green", fg="white", font=("arial", 9, "bold"), width=5
)
canvas1.create_window(90, 190, window=button_add)


def sub():
v1 = entry1.get()
v2 = entry2.get()
result = float(v1) - float(v2)
result_label.config(text=f"{result}")


button_sub = tk.Button(
text="–", command=sub, bg="green", fg="white", font=("arial", 9, "bold"), width=5
)
canvas1.create_window(140, 190, window=button_sub)


def mul():
v1 = entry1.get()
v2 = entry2.get()
result = float(v1) * float(v2)
result_label.config(text=f"{result}")


button_mul = tk.Button(
text="x", command=mul, bg="green", fg="white", font=("arial", 9, "bold"), width=5
)
canvas1.create_window(190, 190, window=button_mul)


def div():
v1 = entry1.get()
v2 = entry2.get()
result = float(v1) / float(v2)
result_label.config(text=f"{result}")


button_div = tk.Button(
text="/", command=div, bg="green", fg="white", font=("arial", 9, "bold"), width=5
)
canvas1.create_window(240, 190, window=button_div)

root.mainloop()

Don’t forget to add the root.mainloop() at the end.

Leave a Comment