└── calculator.py /calculator.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | from tkinter import font 3 | 4 | base = Tk() 5 | base.title("Calculator") 6 | base.geometry('392x500+30+40') 7 | base.resizable(0,0) 8 | base.configure(background='white') 9 | 10 | #Display label 11 | label = Label(base,height=7, width=41,anchor= 'se') 12 | label.place(x=30,y=30) 13 | company_label =Label(base,text='Masio', bd=5, relief='groove', height=3, width=19, bg='white',fg='red') 14 | company_label.place(x=30,y=172) 15 | 16 | #glabal variable 17 | num ='' 18 | 19 | # Functions 20 | def display(number): 21 | 22 | global num 23 | num = num+str(number) 24 | label['text'] = num 25 | 26 | 27 | def equals(): 28 | try: 29 | global num 30 | result= eval(num) 31 | label['text'] = result 32 | except: 33 | label['text'] = 'Error' 34 | def clear(): 35 | global num 36 | num = '' 37 | label['text']=num 38 | def backspace(): 39 | global num 40 | num = num[:-1] 41 | label['text']=num 42 | 43 | #adding buttons 44 | 45 | times15 = font.Font(family='times', size=15, weight='bold') 46 | btn1= Button(text=1, bd=1, height=2, width=5, font=times15,bg='#A9A9A9',command=lambda: display(1)) 47 | btn2 = Button(text=2, bd=1, height=2, width=5,font=times15,bg='#A9A9A9',command=lambda: display(2)) 48 | btn3 = Button(text=3, bd=1, height=2, width=5,font=times15,bg='#A9A9A9',command=lambda: display(3)) 49 | btn4= Button(text=4, bd=1, height=2, width=5, font=times15,bg='#A9A9A9',command=lambda: display(4)) 50 | btn5 = Button(text=5, bd=1, height=2, width=5,font=times15,bg='#A9A9A9',command=lambda: display(5)) 51 | btn6 = Button(text=6, bd=1, height=2, width=5,font=times15,bg='#A9A9A9',command=lambda: display(6)) 52 | btn7= Button(text=7, bd=1, height=2, width=5, font=times15,bg='#A9A9A9',command=lambda: display(7)) 53 | btn8 = Button(text=8, bd=1, height=2, width=5,font=times15, bg='#A9A9A9',command=lambda: display(8)) 54 | btn9 = Button(text=9, bd=1, height=2, width=5,font=times15,bg='#A9A9A9',command=lambda: display(9)) 55 | btn0= Button(text=0, bd=1, height=2, width=5, font=times15,bg='#A9A9A9',command=lambda: display(0)) 56 | btndot = Button(text='.', bd=1, height=2, width=5,font=times15,bg='#A9A9A9',command=lambda: display('.')) 57 | btnequal = Button(text='=', bd=1, height=2, width=5,font=times15, bg='blue', fg='white',command=equals) 58 | btnClear = Button(text='AC', bd=1, height=2, width=5,font=times15,bg='#D3D3D3',command=clear) 59 | 60 | btnback = Button(text='c', bd=1, height=2, width=5,font=times15,command=backspace) 61 | btndiv = Button(text='/', bd=1, height=2, width=5,font=times15,command=lambda: display('/')) 62 | btnmult = Button(text='x', bd=1, height=2, width=5,font=times15,command=lambda: display('*')) 63 | btnsub = Button(text='-', bd=1, height=2, width=5,font=times15,command=lambda: display('-')) 64 | btnsum = Button(text='+', bd=1, height=2, width=5,font=times15,bg='orange',command=lambda: display('+')) 65 | #button alignment 66 | btn1.place(x=30,y=358) 67 | btn2.place(x=115,y=358) 68 | btn3.place(x=200,y=358) 69 | btn4.place(x=30,y=296) 70 | btn5.place(x=115,y=296) 71 | btn6.place(x=200,y=296) 72 | btn7.place(x=30,y=234) 73 | btn8.place(x=115,y=234) 74 | btn9.place(x=200,y=234) 75 | btn0.place(x=30,y=420) 76 | btndot.place(x=115,y=420) 77 | btnequal.place(x=200,y=420) 78 | btnClear.place(x=200,y=172) 79 | btnback.place(x=285,y=172) 80 | btndiv.place(x=285,y=358) 81 | btnmult.place(x=285,y=296) 82 | btnsub.place(x=285,y=234) 83 | btnsum.place(x=285,y=420) 84 | 85 | base.mainloop() 86 | --------------------------------------------------------------------------------