-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_calculator.py
143 lines (108 loc) · 3.94 KB
/
simple_calculator.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from tkinter import *
window = Tk()
expression = ""
window.geometry("330x280")
window.resizable(False, False)
window.configure(background="purple")
<<<<<<< HEAD
# photo = PhotoImage(name="cal.png")
# window.iconphoto(False, photo)
=======
photo = PhotoImage(name='cal.png')
window.iconphoto(False, photo)
>>>>>>> 854e369d28720e8fd0ff21d84b67bb1c614cf300
window.title("Calculator")
equation = StringVar()
expression_field = Entry(window, textvariable=equation)
expression_field.grid(columnspan=8, ipadx=80, ipady=20)
equation.set('Enter Values')
def listener(event, window="expression_field"):
if "0123456789+-*/".find(event.char) != -1:
press(str(event.char))
if event.char == '\r':
equalpress()
def press(num):
global expression
expression = expression + str(num)
equation.set(expression)
def equalpress():
try:
global expression
total = str(eval(expression))
equation.set(total)
expression = ""
except:
equation.set(" Enter a Value!!")
expression = ""
def clear():
global expression
expression = ""
equation.set("")
def listener(event):
if "0123456789+-*/".find(event.char) != -1:
press(event.char)
if event.char == '\r':
equalpress()
if event.char == 'del':
clear()
button1 = Button(text="1", bg="purple", width=7,
height=2, command=lambda: press(1))
button2 = Button(text="2", bg="purple", width=7,
height=2, command=lambda: press(2))
button3 = Button(text="3", bg="purple", width=7,
height=2, command=lambda: press(3))
button4 = Button(text="4", bg="purple", width=7,
height=2, command=lambda: press(4))
button5 = Button(text="5", bg="purple", width=7,
height=2, command=lambda: press(5))
button6 = Button(text="6", bg="purple", width=7,
height=2, command=lambda: press(6))
button7 = Button(text="7", bg="purple", width=7,
height=2, command=lambda: press(7))
button8 = Button(text="8", bg="purple", width=7,
height=2, command=lambda: press(8))
button9 = Button(text="9", bg="purple", width=7,
height=2, command=lambda: press(9))
button0 = Button(text="0", bg="purple", width=7,
height=2, command=lambda: press(0))
button_dot = Button(text=".", bg="purple", width=7,
height=2, command=lambda: press('.'))
button_plus = Button(text="+", bg="purple", width=7,
height=2, command=lambda: press("+"))
button_minus = Button(text="-", bg="purple", width=7,
height=2, command=lambda: press("-"))
button_mul = Button(text="*", bg="purple", width=7,
height=2, command=lambda: press("*"))
button_div = Button(text="/", bg="purple", width=7,
height=2, command=lambda: press("/"))
button_clear = Button(text="C", bg="purple", width=7,
height=2, command=lambda: clear())
button_clrscrn = Button(text="CE", bg="purple", width=7,
height=2, command=lambda: clear())
button_equal = Button(text="=", bg="purple", width=7,
height=2, command=lambda: equalpress())
window.bind("<KeyPress>", listener)
button1.grid(row=2, column=0)
button2.grid(row=2, column=1)
button3.grid(row=2, column=2)
button4.grid(row=3, column=0)
button5.grid(row=3, column=1)
button6.grid(row=3, column=2)
button7.grid(row=4, column=0)
button8.grid(row=4, column=1)
button9.grid(row=4, column=2)
button0.grid(row=5, column=1)
button_plus.grid(row=2, column=4)
button_minus.grid(row=3, column=4)
button_mul.grid(row=4, column=4)
button_div.grid(row=5, column=4)
button_clrscrn.grid(row=6, column=0)
button_clear.grid(row=6, column=2)
button_dot.grid(row=5, column=0)
button_equal.grid(row=5, column=2)
<<<<<<< HEAD
window.bind("<KeyPress>", listener)
window.mainloop()
=======
window.mainloop()
>>>>>>> 854e369d28720e8fd0ff21d84b67bb1c614cf300