Welcome folks today in this folks we will be building a advanced scientific calculator
in python using tkinter. All the full source code of the application will be shown below.
Get Started
In order to get started you need to install the following libraries shown below
pip install tkinter
pip install math
After installing these libraries make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
from tkinter import Tk from tkinter import StringVar,Entry,Button from math import pi,e,sin,cos,tan,log,log10,ceil,degrees,radians,exp,asin,acos,floor class calculator: def __init__(self): window=Tk() window.title('Scientific Calculator') window.configure(background="white") self.string=StringVar() entry=Entry(window,textvariable=self.string) entry.grid(row=0,column=0,columnspan=6) entry.configure(background="white") entry.focus() values=["7","8","9","/","%","clear","AC", "4","5","6","*","(",")","**", "1","2","3","-","=",",","0",".","min","+","sin","asin","cos","acos","tan()", "pow","log10","max","abs","floor","pi","e","log","ceil","degrees","radians"] text=1 i=0 row=1 col=0 for txt in values: padx=10 pady=10 if(i==7): row=2 col=0 if(i==14): row=3 col=0 if(i==19): row=4 col=0 if(i==26): row=5 col=0 if(i==33): row=6 col=0 if(txt=='='): btn=Button(window,height=2,width=4,padx=70,pady=pady,text=txt,command=lambda txt=txt:self.equals()) btn.grid(row=row,column=col,columnspan=3,padx=2,pady=2) btn.configure(background="yellow") elif(txt=='clear'): btn=Button(window,height=2,width=4,padx=padx,pady=pady, text=txt ,command=lambda txt=txt:self.delete()) btn.grid(row=row,column=col,padx=1,pady=1) btn.configure(background="grey") elif(txt=='AC'): btn=Button(window,height=2,width=4,padx=padx,pady=pady,text=txt,command=lambda txt=txt:self.clearall()) btn.grid(row=row,column=col,padx=1,pady=1) btn.configure(background="red") else: btn=Button(window,height=2,width=4,padx=padx,pady=pady,text=txt ,command=lambda txt=txt:self.addChar(txt)) btn.grid(row=row,column=col,padx=1,pady=1) btn.configure(background="cyan") col=col+1 i=i+1 window.mainloop() def clearall(self): self.string.set("") def equals(self): result="" try: result=eval(self.string.get()) self.string.set(result) except: result="INVALID INPUT" self.string.set(result) def addChar(self,char): self.string.set(self.string.get()+(str(char))) def delete(self): self.string.set(self.string.get()[0:-1]) calculator() |
So now if you execute this python
script app.py by typing the below command shown below
python app.py
DOWNLOAD SOURCE CODE