What is a calculator?
I know it's a silly question, but let's professionally understand this. A calculator is an electronic device that is used to calculate figures. As time passes, media phones, smartphones, and many other tech appliances take the place of calculators.
As a developer, ever wonders? How to make a calculator? It's a simple project, but an interesting one. If you are a beginner then you must try it.
Let's make a calculator using Python. Take a cup of coffee and begin the hack.
If you are looking for a video tutorial then it's here:
Setup and Installation
I prefer to do a Python-based project in a virtual environment and I suggest you do the same. It's a good habit to build projects in a virtual environment. So, let's start with a virtual environment. You can skip these steps too.
Choose a directory in your system and open your terminal or command prompt window in the same directory and run the command:
virtualenv -p python3.8 calculator
cd calculator
source bin/activate
If you are a Linux user then the above three commands are enough for making a virtual environment and activating it. But if you are a Windows user then the commands are a little bit different.
virtualenv calculator
calculator\scripts\activate
In both cases, you must have a virtual environment installed in your system.
After activating the virtual environment, let's install the required Python libraries.
pip install tk
To use the “tk” library, you must have Tkinter installed in your system.
Tkinter is a Python package that mainly deals with the GUI part of a Python project.
To install Tkinter in Linux, you can use the command as shown below.
sudo apt install python3.8-tk
If you are a windows user then, you can install Tkinter at the time of Python installation. You can install it manually too.
Code of Calculator using Python
Make a Python file inside the virtual environment.
calculator.py
from tkinter import *root = Tk()
root.geometry("500x500")
root.resizable(0,0)
root.title('Python Calculator')
Label(root,text = 'YOUTUBE - BUG NINZA', font ='arial 20 bold').pack(side=BOTTOM)
- Import everything from Tkinter
- Create a basic window
- Define the size of the window
- Prevent the window from resizing
- Create a title
- Create a footer (optional)
expression=""
input_text = StringVar()
#clear
def btn_clear():
global expression
expression=""
input_text.set("")
#click
def btn_click(item):
global expression
expression = expression + str(item)
input_text.set(expression)
#calculate
def btn_equal():
global expression
result = str(eval(expression))
input_text.set(result)
expression=""
- StringVar() is used to get the instance of an input field.
- Button clear function, this function is used to clear the input field.
- Button click function, this function continuously updates the input field whenever you enter a number.
- Button equal function, this function is used to calculate the expression present in the input field.
#input frame
input_frame = Frame(root, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2)
input_frame.pack(side=TOP)
#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)
Design the input frame. After that, create an input field inside this ‘Frame'.
Now, Design a button frame and a row-wise button.
#button frame
btns_frame = Frame(root, width=312, height=272.5, bg="grey")
btns_frame.pack()
# first row
clear = Button(btns_frame, text = "CLEAR", fg = "black", width = 32, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_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: btn_equal()).grid(row = 4, column = 3, padx = 1, pady = 1)
root.mainloop()
We are done with the code part here.
Now run the program inside the virtual environment using the command:
python calculator.py
After the compilation, you will see a virtual calculator on-screen. Perform some operations there. I did some, and here is the result.
In case you lost somewhere. The Github code access of the project is here.
Well, That's it.
If this article sounds informative then make sure to share it with your geek community.