Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 64f2723

Browse files
Merge pull request #563 from lavish619/Scientific_calculator
Scientific Calculator GUI Added
2 parents f43ec98 + a9acc62 commit 64f2723

File tree

4 files changed

+296
-0
lines changed

4 files changed

+296
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
**Note:** Put Proper Paranthesis while writing the complete expression in Calculator.
7+
8+
## Modules Required
9+
Python3
10+
(No external library or module needed)
11+
12+
## How To Open
13+
Simply open the python file, Scientific_Calculator.py
14+
15+
## Screenshot
16+
![alt text](https://github.com/lavish619/Python_and_the_Web/blob/master/Scripts/Miscellaneous/Scientific_Calculator_GUI/Screenshots/Screenshot-scientific.jpg?raw=true)
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
#import Gui Package and libraries
2+
import tkinter.font
3+
from tkinter import *
4+
import math
5+
6+
7+
def clearall():
8+
9+
'''Clear function toclear the screen
10+
and reset all variables'''
11+
12+
global expression
13+
global equation
14+
global value
15+
global ans
16+
expression=''
17+
value=''
18+
ans=''
19+
equation.set(expression)
20+
21+
def sgn(a):
22+
#Signum function
23+
return 1 if a>0 else -1 if a<0 else 0
24+
25+
def clearback():
26+
27+
'''Backspace button function to delete
28+
a character from the screen'''
29+
30+
result1=""
31+
result2=""
32+
global equation
33+
global expression
34+
global value
35+
global ans
36+
37+
expression = area.get()
38+
temp1= list(expression)
39+
temp2= list(value)
40+
41+
if value=='':
42+
temp1=[]
43+
temp2=[]
44+
elif expression[-5:] in ["asin(","acos(","atan("]:
45+
for _ in range(5):temp1.pop()
46+
for _ in range(10):temp2.pop()
47+
48+
elif expression[-4:]=="log(":
49+
for _ in range(4):temp1.pop()
50+
for _ in range(11):temp2.pop()
51+
52+
elif expression[-4:] in ['sin(','cos(','tan(']:
53+
for _ in range(4): temp1.pop()
54+
for _ in range(9): temp2.pop()
55+
56+
elif expression[-4:]=='sgn(':
57+
for _ in range(4): temp1.pop()
58+
for _ in range(4): temp2.pop()
59+
60+
elif expression[-3:]=='ln(':
61+
for _ in range(3):temp1.pop()
62+
for _ in range(9): temp2.pop()
63+
64+
elif expression[-2:]=='e^':
65+
for _ in range(2):temp1.pop()
66+
for _ in range(8): temp2.pop()
67+
68+
elif expression[-1]=='^':
69+
for _ in range(1):temp1.pop()
70+
for _ in range(2): temp2.pop()
71+
72+
elif expression[-1]=="√":
73+
for _ in range(1):temp1.pop()
74+
for _ in range(10):temp2.pop()
75+
76+
elif expression[-1]=='π':
77+
for _ in range(1):temp1.pop()
78+
for _ in range(7): temp2.pop()
79+
80+
elif expression[-1]=='e':
81+
for _ in range(1):temp1.pop()
82+
for _ in range(6): temp2.pop()
83+
84+
elif expression[-1]=='%':
85+
for _ in range(1):temp1.pop()
86+
for _ in range(4): temp2.pop()
87+
88+
else:
89+
temp1.pop()
90+
temp2.pop()
91+
92+
for element in range(len(temp1)):
93+
result1+=temp1[element]
94+
expression = result1
95+
equation.set(expression)
96+
97+
for element in range(len(temp2)):
98+
result2+=temp2[element]
99+
100+
value=result2
101+
try:ans = str(eval(value))
102+
except:pass
103+
104+
def pressbtn(num):
105+
106+
'''Function to determine which button is pressed.
107+
Everytime a button a pressed , this function is
108+
called and a parameter is passed, which updates the
109+
variables and the expression to be evaluated'''
110+
111+
global expression
112+
global value
113+
global ans
114+
expression = expression + str(num)
115+
equation.set(expression)
116+
if num in ["1","2","3","4","5","6","7","8","9","0","(",")","00"]:
117+
value += num
118+
try:ans = str(eval(value))
119+
except:ans = "Invalid Expression"
120+
121+
elif num in ["+",'-','/','*','.','1/','sgn(']:
122+
value += num
123+
124+
elif num in ['asin(','acos(','atan(','sin(','cos(','tan(']:
125+
value += 'math.'+ num
126+
127+
elif num=='^':value += '**'
128+
129+
elif num=='%':
130+
value += '/100'
131+
try:ans = str(eval(value))
132+
except:ans = "Invalid Expression"
133+
elif num=='^2':
134+
value += '**2'
135+
try:ans = str(eval(value))
136+
except:ans = "Invalid Expression"
137+
elif num=='^3':
138+
value += '**3'
139+
try:ans = str(eval(value))
140+
except:ans = "Invalid Expression"
141+
142+
elif num=='√(':value += 'math.sqrt('
143+
144+
elif num=='e':
145+
value += 'math.e'
146+
try:ans = str(eval(value))
147+
except:ans = "Invalid Expression"
148+
elif num=='π':
149+
value += 'math.pi'
150+
try:ans = str(eval(value))
151+
except:ans = "Invalid Expression"
152+
elif num=='log(':value += 'math.log10('
153+
elif num=='ln(':value += 'math.log('
154+
elif num=='e^':value += 'math.e**'
155+
156+
def equal():
157+
158+
'''On pressing equal to button, this function is called.
159+
And it prints the result on the screen.'''
160+
161+
global ans
162+
global value
163+
global expression
164+
165+
if value=="":
166+
ans=""
167+
168+
equation.set(ans)
169+
ans=''
170+
value=''
171+
expression=''
172+
173+
#Configuring the layout of Calculator
174+
root=Tk()
175+
root.title("Scientific Calculator")
176+
177+
root.resizable(False,False)
178+
cal= Frame(root)
179+
cal.grid()
180+
cal.configure(bg="burlywood4")
181+
equation=StringVar()
182+
183+
area = Entry(cal, textvariable = equation,width= 60, font= ("Comic Sans MS", 15),bd=10 ,justify=LEFT,state=DISABLED,
184+
disabledbackground="white",disabledforeground="black")
185+
area.insert(0,"0")
186+
area.grid(row=0,columnspan=8)
187+
188+
#standard calculator
189+
def standard():
190+
root.geometry('361x350')
191+
area['width']=28
192+
area.grid(row=0,columnspan=4,sticky= EW)
193+
root.title("Standard Calculator")
194+
195+
#scientific calculator
196+
def scientific():
197+
root.geometry('742x350')
198+
area['width']=60
199+
area.grid(row=0,columnspan=8)
200+
root.title("Scientific Calculator")
201+
202+
# adding menubar on top
203+
menubar = Menu(cal)
204+
filemenu= Menu(menubar,tearoff=0)
205+
menubar.add_cascade(label="File", menu=filemenu)
206+
filemenu.add_command(label= "Standard", command= standard)
207+
filemenu.add_separator()
208+
filemenu.add_command(label="Scientific", command= scientific)
209+
root.config(menu=menubar)
210+
211+
value=""
212+
ans=""
213+
expression=""
214+
215+
#Fonts and Colours
216+
font= tkinter.font.Font(size=12,weight= "bold", family='Helvetica',)
217+
h=2
218+
w=7
219+
actvbgnd='white'
220+
bg1='wheat3'
221+
bg2="burlywood1"
222+
bg3="burlywood2"
223+
bg4= "tan1"
224+
fg1= "white"
225+
fg2="black"
226+
227+
numberpad = [7,8,9,4,5,6,1,2,3]
228+
i=0
229+
230+
for j in range(3):
231+
for k in range(3):
232+
Button(cal,command = lambda x = str(numberpad[i]) : pressbtn(x), text = str(numberpad[i]), bg= bg1, fg=fg2,activebackground=actvbgnd,
233+
height=h, width=w,font= font).grid(row=j+2,column=k)
234+
i+=1
235+
236+
r=5 #number of rows
237+
c=7 #number of columns
238+
239+
#creating buttons
240+
Button(cal,command = lambda: pressbtn(0), text = "0", bg= bg1, fg=fg2,activebackground=actvbgnd,
241+
height=h, width=w,font= font).grid(row=r,column= c-7)
242+
Button(cal,command = lambda: pressbtn('00'),text = "00", bg= bg1, fg=fg2,activebackground=actvbgnd,
243+
height=h, width=w,font= font).grid(row=r,column= c-6)
244+
Button(cal,command = clearback, text = "C", bg= bg2, fg=fg2,activebackground=actvbgnd,
245+
height=h, width=w,font= font).grid(row=r-4,column= c-7)
246+
Button(cal,command = clearall, text = "AC",bg= bg2, fg=fg2,activebackground=actvbgnd,
247+
height=h, width=w,font= font).grid(row=r-4,column= c-6)
248+
Button(cal,command = lambda: pressbtn('.'), text = "•", bg= bg3, fg=fg2,activebackground=actvbgnd,
249+
height=h, width=w,font= font).grid(row=r,column=c-5)
250+
Button(cal,command = lambda: pressbtn('+'), text = "+", bg= bg3, fg=fg2,activebackground=actvbgnd,
251+
height=h, width=w,font= font).grid(row=r-2,column=c-4)
252+
Button(cal,command = lambda: pressbtn('-'), text = "–", bg= bg3, fg=fg2,activebackground=actvbgnd,
253+
height=h, width=w,font= font).grid(row=r-3,column=c-4)
254+
Button(cal,command = lambda: pressbtn('/'), text = "/", bg= bg3, fg=fg2,activebackground=actvbgnd,
255+
height=h, width=w,font= font).grid(row=r-4,column=c-5)
256+
Button(cal,command = lambda: pressbtn('*'), text = "✶", bg= bg3, fg=fg2,activebackground=actvbgnd,
257+
height=h, width=w,font= font).grid(row=r-4,column=c-4)
258+
Button(cal,command = equal, text = "=", bg= bg2, fg=fg2,activebackground=actvbgnd,
259+
height=2*h,width=w,font= font,pady=10).grid(row=r-1,column=c-4,rowspan=2,)
260+
261+
list1=['(',')','%','asin','sin','log','x^2','acos','cos','ln','x^3','atan','tan','e^x','1/x','x^y','e',"π",'√x','sgn']
262+
list2=['(',')','%','asin(','sin(','log(','^2','acos(','cos(','ln(','^3','atan(','tan(','e^','1/','^','e',"π",'√(','sgn(']
263+
i=0
264+
for j in range(5):
265+
for k in range(4):
266+
Button(cal,command = lambda x= list2[i]: pressbtn(x), text = list1[i], bg=bg4, fg= fg2,activebackground=actvbgnd,
267+
height=h,width=w,font= font).grid(row=j+1,column=k+4)
268+
i+=1
269+
270+
#set the spacing between buttons
271+
msize=60
272+
cal.rowconfigure(0,minsize=50)
273+
for i in range(1,6):
274+
cal.rowconfigure(i,minsize=60)
275+
276+
msize = 90
277+
for i in range(8):
278+
cal.columnconfigure(i,minsize= msize)
279+
280+
cal.mainloop()
75.8 KB
Loading[フレーム]
37.4 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /