-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.py
More file actions
89 lines (65 loc) · 3.6 KB
/
Calculator.py
File metadata and controls
89 lines (65 loc) · 3.6 KB
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
86
87
88
89
from tkinter import *
exprsn = ""
class Calculator:
def click(str1):
global exprsn
exprsn = exprsn + str(str1)
eqn.set(exprsn)
def delete():
global exprsn
exprsn = ""
eqn.set(exprsn)
def equal():
if(True):
global exprsn
total = str(eval(exprsn))
eqn.set(total)
exprsn = ""
else:
eqn.set('error')
exprsn = ""
if __name__ == '__main__':
obj1=Calculator
r = Tk()
r.title("Calculator")
eqn = StringVar()
expression_field = Entry(r, textvariable=eqn)
expression_field.grid(columnspan=4, ipadx=70)
button1 = Button(r, text=' 1 ', fg='white', bg='black',command=lambda: obj1.click(1), height=2, width=7)
button1.grid(row=2, column=0)
button2 = Button(r, text=' 2 ', fg='white', bg='black',command=lambda: obj1.click(2), height=2, width=7)
button2.grid(row=2, column=1)
button3 = Button(r, text=' 3 ', fg='white', bg='black',command=lambda: obj1.click(3), height=2, width=7)
button3.grid(row=2, column=2)
button4 = Button(r, text=' 4 ', fg='white', bg='black',command= lambda: obj1.click(4), height=2, width=7)
button4.grid(row=3, column=0)
button5 = Button(r, text=' 5 ', fg='white', bg='black',command=lambda: obj1.click(5), height=2 ,width=7)
button5.grid(row=3, column=1)
button6 = Button(r, text=' 6 ', fg='white', bg='black',command=lambda: obj1.click(6), height=2, width=7)
button6.grid(row=3, column=2)
button7 = Button(r, text=' 7 ', fg='white', bg='black',command=lambda: obj1.click(7), height=2, width=7)
button7.grid(row=4, column=0)
button8 = Button(r, text=' 8 ', fg='white', bg='black',command=lambda: obj1.click(8), height=2, width=7)
button8.grid(row=4, column=1)
button9 = Button(r, text=' 9 ', fg='white', bg='black',command=lambda: obj1.click(9), height=2, width=7)
button9.grid(row=4, column=2)
button0 = Button(r, text=' 0 ', fg='white', bg='black',command=lambda: obj1.click(0), height=2, width=7)
button0.grid(row=5, column=0)
addition = Button(r, text=' + ', fg='white', bg='black',command=lambda: obj1.click("+"), height=2, width=7)
addition.grid(row=2, column=3)
subtraction = Button(r, text=' - ', fg='white', bg='black',
command=lambda: obj1.click("-"), height=2, width=7)
subtraction.grid(row=3, column=3)
multiplication = Button(r, text=' x ', fg='white', bg='black',
command=lambda: obj1.click("*"), height=2, width=7)
multiplication.grid(row=4, column=3)
division = Button(r, text=' / ', fg='white', bg='black',
command=lambda: obj1.click("/"), height=2, width=7)
division.grid(row=5, column=3)
equal_pre = Button(r, text=' = ', fg='white', bg='black',command=lambda: obj1.equal(), height=2, width=7)
equal_pre.grid(row=5, column=2)
clearall = Button(r, text='Clear', fg='white', bg='black',command=lambda: obj1.delete(), height=2, width=7)
clearall.grid(row=5, column='1')
Decimal = Button(r, text='.', fg='white', bg='black',command=lambda: obj1.click('.'), height=2, width=7)
Decimal.grid(row=6, column=0)
r.mainloop()