# 7 : Calculator using Python Tkinter
Calculator using Python Tkinter
This is an amazing python Tkinter project based on object-oriented programming that I have made. In this project, I have created a calculator using the Tkinter library and this is able to do basic mathematical operations like adding, subtracting, etc.
The main logic behind the Programme = I can simply add the number or operations in a number string, on which user press and on clicking the '=' button the result of this string is printed using eval built in the function of python. Eval function is used to operate strings like numbers.
If you want to know a brief about the logic then a brief explanation is given at the beginning of code so please download python file from the link below or see the code below
Download links
- Click here to download the python code file
- Click here to download this software and install in your system
Note = No any coding software is required to run this software simply download this from the second link from above and install and run
Code for the software
********************************************************************************
# **************************************************************************************************************
# Logic = the function that is called when the any button is pressed is written in such a way that it can apped the
# Numbers and operations which user press in a string like '34+56/78' and when the = is pressed we use eval function
# which is used to apply operations of mathematics in a string.
# use of number funtion written below = The number function is the main function because this is the all mechanics all
# other functions are just for designing so you can understand them easily. Number fuction can take a text as an
# argument.
# Parts of Number function
# whenever the user press any button then text of button like 1,2,3 etc. will be added in string containing
# numbers and operation like '4+5+6/1-1' and if user press following of any then following parts will run.
# Part1 = This part will handle if text is = or answer. Then this will apply eval function on the string and
# return output we use try statements to error handling
# part2 = This part contain 'clear' command and this will clear all the string of numbers so that entry will
# be deleted
# part3 = This part contain 'del' command and this will delete only the last element of string ls so that only
# one number is deleted
# **************************************************************************************************************
from tkinter import *
class main(Tk): # Main class
def __init__(self):
super().__init__()
self.geometry('500x500')
self.minsize(500,500)
self.maxsize(900,500)
self.title('Basic calculator for Mathematics')
self.a = Label(self,text='Basic calculator for Mathematics',font='MVBoli 22 bold',bg='orange')
self.a.pack(pady=10)
self.input_data = StringVar()
self.input_data.set("")
self.entry = Entry(self, textvariable=self.input_data,font='lucida 23 bold',width=80,bd=2
,relief=SUNKEN)
self.entry.pack(pady=10,padx=10,ipadx=10)
def frame_and_buttons(self,height,width):
'''Function for making frames '''
self.frame_name = Frame(self,height=height,width=width,bd =1,relief=GROOVE)
self.frame_name.pack(pady=10,padx =10)
def number(self,number):
'''A function that will present result and perform operations. The breif is dicussed at the stat of code'''
global ls
if number=='=': # Part 1 discuss at above
if ls.isnumeric():
# if there is no any operations in string like = 78774838 then we have to return it same
self.input_data.set(ls)
else:
# Error handling
try:
result = eval(ls)
print(result)
ls = str(result)
self.input_data.set(result)
except:
ls=""
self.input_data.set('Error! Invalid input...')
elif number=='Clear': # Part 2 Discuss above
self.input_data.set('')
ls = ''
elif number=='del': # Part 3 Discuss above
lis_for_delete = []
for i in range(len(ls)-1): # Adding all elements len(ls)-1 except last so last is deleted
lis_for_delete.append(ls[i])
deleted = ''.join(lis_for_delete)
ls = deleted
self.input_data.set(ls)
else: # adding numbers to string
ls+=number
self.input_data.set(ls)
def mae(self,text,text2,text3,text4):
'''Function for making four buttons at one time and take the text of button as an argumnet and pass the text
to the number function'''
self.b = Button(self.frame_name,text=text,width=4,activebackground='green',bd=2,
relief=SUNKEN,bg='yellow' ,font= 'Arial 19 bold',command=lambda : self.number(text))
self.b.pack(anchor= 'w',side=LEFT,padx =10,fill=BOTH)
self.b = Button(self.frame_name,text=text2,width=4,activebackground='green',bd=2,
relief=SUNKEN,bg='yellow' ,font= 'Arial 19 bold',command=lambda : self.number(text2))
self.b.pack(anchor= 'w',side=LEFT,padx =10,fill=BOTH)
self.b = Button(self.frame_name,text=text3,width=4,activebackground='green',bd=2,
relief=SUNKEN,bg='yellow' ,font= 'Arial 19 bold',command=lambda : self.number(text3))
self.b.pack(anchor= 'w',side=LEFT,padx =10,fill=BOTH)
self.b = Button(self.frame_name,text=text4,width=4,activebackground='green',bd=2,
relief=SUNKEN,bg='yellow' ,font= 'Arial 19 bold',command=lambda : self.number(text4))
self.b.pack(anchor= 'w',side=LEFT,padx =10,fill=BOTH)
if __name__ == '__main__':
# Main progamme starts here
ls = ''
root = main()
# First Frame
root.frame_and_buttons(300,300)
root.mae('1','2','3','(')
# second Frame
root.frame_and_buttons(300,300)
root.mae('4','5','6',')')
# third Frame
root.frame_and_buttons(300,300)
root.mae('7','8','9','del')
# Fourth Frame
root.frame_and_buttons(300,300)
root.mae('0','+','-','Clear')
# Fifth Frame
root.frame_and_buttons(300,300)
root.mae('%','/','*','=')
root.mainloop()
********************************************************************************
Comments
Post a Comment