Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 5849719

Browse files
author
Lavish Bansal
committed
Scientific Calculator Added
1 parent 9d35536 commit 5849719

File tree

4 files changed

+261
-0
lines changed

4 files changed

+261
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Scientific-Calculator-GUI-Tkinter
2+
Scientific and Standard Calculator combined, made in python using Tkinter module based on GUI.
3+
4+
The Calculator is built in such a way that it first allows the user to enter the expression or the complete equation just like an actual scientific calulator in your device and then on pressing the equalto button , it shows the result.
5+
6+
Though it may look like a long code, but it is very easy to understand. I tried to shorten it as much as possible. It works very efficiently and fastly for as many functions you enter in a single expression it will evaluate it correctly.
7+
8+
Note: The only condition with it is that must contain appropriate parantheses otherwise its gonna take it as an Invalid Expression.
9+
10+
11+
![alt text](https://github.com/lavish619/Scientific-Calculator-GUI-Tkinter/blob/master/scientific%20%20calc%20image.jpg?raw=true)
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
import tkinter.font
2+
from tkinter import *
3+
import math
4+
5+
def clearall():
6+
global expression
7+
global equation
8+
global value
9+
global ans
10+
expression=''
11+
value=''
12+
ans=''
13+
equation.set(expression)
14+
15+
def sgn(a):
16+
return 1 if a>0 else -1 if a<0 else 0
17+
18+
def clearback():
19+
result1=""
20+
result2=""
21+
global equation
22+
global expression
23+
global value
24+
global ans
25+
26+
expression = area.get()
27+
temp1= list(expression)
28+
temp2= list(value)
29+
30+
if value=='':
31+
temp1=[]
32+
temp2=[]
33+
elif expression[-5:] in ["asin(","acos(","atan("]:
34+
for _ in range(5):temp1.pop()
35+
for _ in range(10):temp2.pop()
36+
37+
elif expression[-4:]=="log(":
38+
for _ in range(4):temp1.pop()
39+
for _ in range(11):temp2.pop()
40+
41+
elif expression[-4:] in ['sin(','cos(','tan(']:
42+
for _ in range(4): temp1.pop()
43+
for _ in range(9): temp2.pop()
44+
45+
elif expression[-4:]=='sgn(':
46+
for _ in range(4): temp1.pop()
47+
for _ in range(4): temp2.pop()
48+
49+
elif expression[-3:]=='ln(':
50+
for _ in range(3):temp1.pop()
51+
for _ in range(9): temp2.pop()
52+
53+
elif expression[-2:]=='e^':
54+
for _ in range(2):temp1.pop()
55+
for _ in range(8): temp2.pop()
56+
57+
elif expression[-1]=='^':
58+
for _ in range(1):temp1.pop()
59+
for _ in range(2): temp2.pop()
60+
61+
elif expression[-1]=="√":
62+
for _ in range(1):temp1.pop()
63+
for _ in range(10):temp2.pop()
64+
65+
elif expression[-1]=='π':
66+
for _ in range(1):temp1.pop()
67+
for _ in range(7): temp2.pop()
68+
69+
elif expression[-1]=='e':
70+
for _ in range(1):temp1.pop()
71+
for _ in range(6): temp2.pop()
72+
73+
elif expression[-1]=='%':
74+
for _ in range(1):temp1.pop()
75+
for _ in range(4): temp2.pop()
76+
77+
else:
78+
temp1.pop()
79+
temp2.pop()
80+
81+
for element in range(len(temp1)):
82+
result1+=temp1[element]
83+
expression = result1
84+
equation.set(expression)
85+
86+
for element in range(len(temp2)):
87+
result2+=temp2[element]
88+
89+
value=result2
90+
try:ans = str(eval(value))
91+
except:pass
92+
93+
def pressbtn(num):
94+
global expression
95+
global value
96+
global ans
97+
expression = expression + str(num)
98+
equation.set(expression)
99+
if num in ["1","2","3","4","5","6","7","8","9","0","(",")","00"]:
100+
value += num
101+
try:ans = str(eval(value))
102+
except:ans = "Invalid Expression"
103+
104+
elif num in ["+",'-','/','*','.','1/','sgn(']:
105+
value += num
106+
107+
elif num in ['asin(','acos(','atan(','sin(','cos(','tan(']:
108+
value += 'math.'+ num
109+
110+
elif num=='^':value += '**'
111+
112+
elif num=='%':
113+
value += '/100'
114+
try:ans = str(eval(value))
115+
except:ans = "Invalid Expression"
116+
elif num=='^2':
117+
value += '**2'
118+
try:ans = str(eval(value))
119+
except:ans = "Invalid Expression"
120+
elif num=='^3':
121+
value += '**3'
122+
try:ans = str(eval(value))
123+
except:ans = "Invalid Expression"
124+
125+
elif num=='√(':value += 'math.sqrt('
126+
127+
elif num=='e':
128+
value += 'math.e'
129+
try:ans = str(eval(value))
130+
except:ans = "Invalid Expression"
131+
elif num=='π':
132+
value += 'math.pi'
133+
try:ans = str(eval(value))
134+
except:ans = "Invalid Expression"
135+
elif num=='log(':value += 'math.log10('
136+
elif num=='ln(':value += 'math.log('
137+
elif num=='e^':value += 'math.e**'
138+
139+
def equal():
140+
global ans
141+
global value
142+
global expression
143+
144+
if value=="":
145+
ans=""
146+
147+
equation.set(ans)
148+
ans=''
149+
value=''
150+
expression=''
151+
152+
root=Tk()
153+
root.title("Scientific Calculator")
154+
155+
root.resizable(False,False)
156+
cal= Frame(root)
157+
cal.grid()
158+
cal.configure(bg="burlywood4")
159+
equation=StringVar()
160+
161+
area = Entry(cal, textvariable = equation,width= 60, font= ("Comic Sans MS", 15),bd=10 ,justify=LEFT,state=DISABLED,
162+
disabledbackground="white",disabledforeground="black")
163+
area.insert(0,"0")
164+
area.grid(row=0,columnspan=8)
165+
166+
def standard():
167+
root.geometry('361x350')
168+
area['width']=28
169+
area.grid(row=0,columnspan=4,sticky= EW)
170+
root.title("Standard Calculator")
171+
172+
def scientific():
173+
root.geometry('742x350')
174+
area['width']=60
175+
area.grid(row=0,columnspan=8)
176+
root.title("Scientific Calculator")
177+
178+
menubar = Menu(cal)
179+
filemenu= Menu(menubar,tearoff=0)
180+
menubar.add_cascade(label="File", menu=filemenu)
181+
filemenu.add_command(label= "Standard", command= standard)
182+
filemenu.add_separator()
183+
filemenu.add_command(label="Scientific", command= scientific)
184+
root.config(menu=menubar)
185+
186+
value=""
187+
ans=""
188+
expression=""
189+
190+
font= tkinter.font.Font(size=12,weight= "bold", family='Helvetica',)
191+
h=2
192+
w=7
193+
actvbgnd='white'
194+
bg1='wheat3'
195+
bg2="burlywood1"
196+
bg3="burlywood2"
197+
bg4= "tan1"
198+
fg1= "white"
199+
fg2="black"
200+
201+
numberpad = [7,8,9,4,5,6,1,2,3]
202+
i=0
203+
for j in range(3):
204+
for k in range(3):
205+
Button(cal,command = lambda x = str(numberpad[i]) : pressbtn(x), text = str(numberpad[i]), bg= bg1, fg=fg2,activebackground=actvbgnd,
206+
height=h, width=w,font= font).grid(row=j+2,column=k)
207+
i+=1
208+
209+
r=5
210+
c=7
211+
Button(cal,command = lambda: pressbtn(0), text = "0", bg= bg1, fg=fg2,activebackground=actvbgnd,
212+
height=h, width=w,font= font).grid(row=r,column= c-7)
213+
Button(cal,command = lambda: pressbtn('00'),text = "00", bg= bg1, fg=fg2,activebackground=actvbgnd,
214+
height=h, width=w,font= font).grid(row=r,column= c-6)
215+
Button(cal,command = clearback, text = "C", bg= bg2, fg=fg2,activebackground=actvbgnd,
216+
height=h, width=w,font= font).grid(row=r-4,column= c-7)
217+
Button(cal,command = clearall, text = "AC",bg= bg2, fg=fg2,activebackground=actvbgnd,
218+
height=h, width=w,font= font).grid(row=r-4,column= c-6)
219+
Button(cal,command = lambda: pressbtn('.'), text = "•", bg= bg3, fg=fg2,activebackground=actvbgnd,
220+
height=h, width=w,font= font).grid(row=r,column=c-5)
221+
Button(cal,command = lambda: pressbtn('+'), text = "+", bg= bg3, fg=fg2,activebackground=actvbgnd,
222+
height=h, width=w,font= font).grid(row=r-2,column=c-4)
223+
Button(cal,command = lambda: pressbtn('-'), text = "–", bg= bg3, fg=fg2,activebackground=actvbgnd,
224+
height=h, width=w,font= font).grid(row=r-3,column=c-4)
225+
Button(cal,command = lambda: pressbtn('/'), text = "/", bg= bg3, fg=fg2,activebackground=actvbgnd,
226+
height=h, width=w,font= font).grid(row=r-4,column=c-5)
227+
Button(cal,command = lambda: pressbtn('*'), text = "✶", bg= bg3, fg=fg2,activebackground=actvbgnd,
228+
height=h, width=w,font= font).grid(row=r-4,column=c-4)
229+
Button(cal,command = equal, text = "=", bg= bg2, fg=fg2,activebackground=actvbgnd,
230+
height=2*h,width=w,font= font,pady=10).grid(row=r-1,column=c-4,rowspan=2,)
231+
232+
list1=['(',')','%','asin','sin','log','x^2','acos','cos','ln','x^3','atan','tan','e^x','1/x','x^y','e',"π",'√x','sgn']
233+
list2=['(',')','%','asin(','sin(','log(','^2','acos(','cos(','ln(','^3','atan(','tan(','e^','1/','^','e',"π",'√(','sgn(']
234+
i=0
235+
for j in range(5):
236+
for k in range(4):
237+
Button(cal,command = lambda x= list2[i]: pressbtn(x), text = list1[i], bg=bg4, fg= fg2,activebackground=actvbgnd,
238+
height=h,width=w,font= font).grid(row=j+1,column=k+4)
239+
i+=1
240+
241+
msize=60
242+
cal.rowconfigure(0,minsize=50)
243+
for i in range(1,6):
244+
cal.rowconfigure(i,minsize=60)
245+
246+
msize = 90
247+
for i in range(8):
248+
cal.columnconfigure(i,minsize= msize)
249+
250+
cal.mainloop()
75.8 KB
Loading
37.4 KB
Loading

0 commit comments

Comments
 (0)