|
| 1 | +from tkinter import * |
| 2 | + |
| 3 | + |
| 4 | +def btn(numbers): |
| 5 | + global operator |
| 6 | + operator += str(numbers) |
| 7 | + txt_input.set(operator) |
| 8 | + |
| 9 | + |
| 10 | +def Clear(): |
| 11 | + global operator |
| 12 | + operator = '' |
| 13 | + txt_input.set('') |
| 14 | + Display.insert(0,'Start Calculating...') |
| 15 | + |
| 16 | +def Del(): |
| 17 | + global operator |
| 18 | + operator = operator[:-1] |
| 19 | + txt_input.set(operator) |
| 20 | + |
| 21 | + |
| 22 | +def Equal(): |
| 23 | + global operator |
| 24 | + sumup = float(eval(operator)) |
| 25 | + txt_input.set(sumup) |
| 26 | + operator ='' |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +root = Tk() |
| 31 | +root.title('Calculator') |
| 32 | + |
| 33 | + |
| 34 | +operator = "" |
| 35 | +txt_input = StringVar(value='Start Calculating...') |
| 36 | + |
| 37 | +# ======================================Screen================================== |
| 38 | +Display = Entry(root, font=('arial', 28, 'bold'), fg='white', bg='green', |
| 39 | + justify='right', bd=40,textvariable = txt_input) |
| 40 | + |
| 41 | +Display.grid(columnspan=4) |
| 42 | + |
| 43 | +# ======================================Row1==================================== |
| 44 | +btn7 = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 45 | + text='7',command = lambda :btn(7)).grid(row=1, column=0) |
| 46 | + |
| 47 | +btn8 = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 48 | + text='8',command = lambda :btn(8)).grid(row=1, column=1) |
| 49 | + |
| 50 | +btn9 = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 51 | + text='9', command=lambda: btn(9)).grid(row=1, column=2) |
| 52 | + |
| 53 | +btndel = Button(root, padx=22, pady=14, bd=6, fg='black', font=('arial', 26,), |
| 54 | + text='<<', bg='red', command=Del).grid(row=1, column=3) |
| 55 | + |
| 56 | +# ======================================Row2==================================== |
| 57 | +btn4 = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 58 | + text='4',command = lambda :btn(4)).grid(row=2, column=0) |
| 59 | + |
| 60 | +btn5 = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 61 | + text='5',command = lambda :btn(5)).grid(row=2, column=1) |
| 62 | + |
| 63 | +btn6 = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 64 | + text='6',command = lambda :btn(6)).grid(row=2, column=2) |
| 65 | + |
| 66 | +btnplus = Button(root, padx=31, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 67 | + text='+', bg='orange',command = lambda :btn('+')).grid(row=2, column=3) |
| 68 | + |
| 69 | + |
| 70 | +# ======================================Row3==================================== |
| 71 | +btn1 = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 72 | + text='1',command = lambda :btn(1)).grid(row=3, column=0) |
| 73 | + |
| 74 | +btn2 = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 75 | + text='2',command = lambda :btn(2)).grid(row=3, column=1) |
| 76 | + |
| 77 | +btn3 = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 78 | + text='3',command = lambda :btn(3)).grid(row=3, column=2) |
| 79 | + |
| 80 | +btnMinus = Button(root, padx=36, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 81 | + text='-', bg='orange',command = lambda:btn('-')).grid(row=3, column=3) |
| 82 | + |
| 83 | +# ======================================Row4==================================== |
| 84 | +btn0 = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 85 | + text='0',command = lambda :btn(0)).grid(row=4, column=0) |
| 86 | + |
| 87 | +btndot = Button(root, padx=33, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 88 | + text='.', bg='orange',command = lambda:btn('.')).grid(row=4, column=1) |
| 89 | + |
| 90 | +btndiv = Button(root, padx=34, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 91 | + text='/', bg='orange',command = lambda:btn('/')).grid(row=4, column=2) |
| 92 | + |
| 93 | +btnmul = Button(root, padx=32, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 94 | + text='x', bg='orange',command = lambda:btn('*')).grid(row=4, column=3) |
| 95 | + |
| 96 | + |
| 97 | +# ======================================Row5==================================== |
| 98 | +btnEqu = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 99 | + text='=', bg='green',command = Equal).grid(row=5, column=0) |
| 100 | + |
| 101 | +btnopenBrac = Button(root, padx=33, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 102 | + text='(', bg='blue',command = lambda:btn('(')).grid(row=5, column=1) |
| 103 | + |
| 104 | +btncloseBrac = Button(root, padx=34, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 105 | + text=')', bg='blue',command = lambda:btn(')')).grid(row=5, column=2) |
| 106 | + |
| 107 | +btnC = Button(root, padx=28, pady=14, bd=6, fg='black', font=('arial', 26, 'bold'), |
| 108 | + text='C', bg='green',command = Clear).grid(row=5, column=3) |
| 109 | + |
| 110 | + |
| 111 | +root.mainloop() |
0 commit comments