Support our Blind Hand Charity by donating at 9818291723. Your contribution has the potential to bring about significant positive change for humanity.

Search This Blog

Thursday, December 14, 2023

CALCULATOR

from tkinter import *

win=Tk()

win.title("CALCULATOR")

win.geometry('280x400')

l1=Label(win,text='                 0',font=('Bookman Old Style',30),width=10,bg='white')

l1.place(x=10,y=10)

l=[]

p=[]

n=''

t=0

d=0

s=0

c=0

w=''

def clr():

    global n,w

    l1.config(text='                  ')

    n=''

    w=''


def one():

    global c,d

    d=1

    if c==0:

        num()

    else:

        dot()


def two():

    global c,d

    d=2

    if c==0:

        num()

    else:

        dot()


def three():

    global d,c

    d=3

    if c==0:

        num()

    else:

        dot()


def four():

    global d,c

    d=4

    if c==0:

        num()

    else:

        dot()


def five():

    global d,c

    d=5

    if c==0:

        num()

    else:

        dot()


def six():

    global d,c

    d=6

    if c==0:

        num()

    else:

        dot()


def seven():

    global d,c

    d=7

    if c==0:

        num()

    else:

        dot()


def eight():

    global d,c

    d=8

    if c==0:

        num()

    else:

        dot()


def nine():

    global d,c

    d=9

    if c==0:

        num()

    else:

        dot()


def zero():

    global d,c

    d=0

    if c==0:

        num()

    else:

        dot()

def tick():

    dot()

def num():

    global n,d,w

    for i in range(0,10):

        if i==d:

            n=n+str(i)

            w=w+str(i)

    l1.config(text=w)


def dot():

    global d,n,w,c

    c+=1

    for i in range(0,10):

        if i==d:

            n=str(float(n)+(i)*10**(-c))

            w=w[0:len(w)-len(n)]

            w=w+str(float(n))

    l1.config(text=w)


def mem():

    global s,p

    s-=1

    l1.config(text=str(p[s]))

def ext():

    win.destroy()


def add():

    global l,n,c,w

    c=0

    l.append(n)

    n=''

    l.append('+')

    w+='+'

    l1.config(text=w)


def sub():

    global l,n,c,w

    c=0

    l.append(n)

    n=''

    l.append('-')

    w+='-'

    l1.config(text=w)


def mul():

    global l,n,c,w

    c=0

    l.append(n)

    n=''

    l.append('*')

    w+='x'

    l1.config(text=w)


def div():

    global l,n,c,w

    c=0

    l.append(n)

    n=''

    l.append('/')

    w+='/'

    l1.config(text=w)


def pers():

    global l,n,c,w

    c=0

    l.append(n)

    n=''

    l.append('*')

    l.append('0.01')

    w+='%'

    l1.config(text=w)


def ans():

    global l,n,s,c,t,w

    l.append(n)

    c=0

    s+=1

    cdB=l.count('/')

    cmB=l.count('*')

    caB=l.count('+')

    csB=l.count('-')

    i=-1

    while l.count('/')!=0:

        i=l.index('/')

        t=float(l[i-1])/float(l[i+1])

        l[i]=t

        l.pop(i-1)

        l.pop(i)

    while l.count('*')!=0:

        i=l.index('*')

        t=float(l[i-1])*float(l[i+1])

        l[i]=t

        l.pop(i-1)

        l.pop(i)

    while l.count('+')!=0:

        i=l.index('+')

        t=float(l[i-1])+float(l[i+1])

        l[i]=t

        l.pop(i-1)

        l.pop(i)

    while l.count('-')!=0:

        i=l.index('-')

        t=float(l[i-1])-float(l[i+1])

        l[i]=t

        l.pop(i-1)

        l.pop(i)

    p.append(l[0])

    l=[]

    l1.config(text=str(p[-1]))

    n=''

    w=''

b1=Button(win,text='OF',font=('Bookman Old Style',18),bg='White',command=ext)

b1.place(x=10,y=75)

b2=Button(win,text='C',font=('Bookman Old Style',18),bg='White',command=clr)

b2.place(x=81,y=75)

b3=Button(win,text='MR',font=('Bookman Old Style',18),bg='White',command=mem)

b3.place(x=140,y=75)

b4=Button(win,text='/',font=('Bookman Old Style',18),bg='White',command=div)

b4.place(x=220,y=75)

b5=Button(win,text='1',font=('Bookman Old Style',18),bg='White',command=one)

b5.place(x=10,y=140)

b6=Button(win,text='2',font=('Bookman Old Style',18),bg='White',command=two)

b6.place(x=77,y=140)

b7=Button(win,text='3',font=('Bookman Old Style',18),bg='White',command=three)

b7.place(x=150,y=140)

b8=Button(win,text='X',font=('Bookman Old Style',18),bg='White',command=mul)

b8.place(x=220,y=140)

b9=Button(win,text='4',font=('Bookman Old Style',18),bg='White',command=four)

b9.place(x=10,y=205)

b10=Button(win,text='5',font=('Bookman Old Style',18),bg='White',command=five)

b10.place(x=77,y=205)

b11=Button(win,text='6',font=('Bookman Old Style',18),bg='White',command=six)

b11.place(x=150,y=205)

b12=Button(win,text='-',font=('Bookman Old Style',18),bg='White',command=sub)

b12.place(x=220,y=205)

b13=Button(win,text='7',font=('Bookman Old Style',18),bg='White',command=seven)

b13.place(x=10,y=270)

b14=Button(win,text='8',font=('Bookman Old Style',18),bg='White',command=eight)

b14.place(x=77,y=270)

b15=Button(win,text='9',font=('Bookman Old Style',18),bg='White',command=nine)

b15.place(x=150,y=270)

b16=Button(win,text='+',font=('Bookman Old Style',18),bg='White',command=add)

b16.place(x=220,y=270)

b17=Button(win,text='.',font=('Bookman Old Style',18),bg='White',command=tick)

b17.place(x=15,y=335)

b18=Button(win,text='0',font=('Bookman Old Style',18),bg='White',command=zero)

b18.place(x=77,y=335)

b19=Button(win,text='%',font=('Bookman Old Style',18),bg='White',command=pers)

b19.place(x=140,y=335)

b20=Button(win,text='=',font=('Bookman Old Style',18),bg='White',command=ans)

b20.place(x=220,y=335)

win.mainloop()

OUTPUT

CLICK HERE TO VIEW THE OUPUT VIDEO