-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator2.py
48 lines (34 loc) · 1.5 KB
/
calculator2.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
from Tkinter import *
from functools import partial
def button_pressed(value):
expression_field_value.set(expression_field_value.get() + str(value))
def equal_pressed():
try:
result = eval(expression_field_value.get())
expression_field_value.set(result)
except ZeroDivisionError:
expression_field_value.set("Division by zero error")
def clear_pressed():
expression_field_value.set("")
if __name__ == "__main__":
window = Tk()
window.title("My Little Calculator")
expression_field_value = StringVar()
expression_field = Entry(window, width=30, textvariable=expression_field_value)
expression_field.grid(row=0, column=0, columnspan=4)
button_rows = [
["1", "2", "3", "*"],
["4", "5", "6", "-"],
["7", "8", "9", "+"],
["0", "/"]
]
for row, buttons in enumerate(button_rows):
for col, button_value in enumerate(buttons):
when_pressed = partial(button_pressed, button_value)
button1 = Button(window, text=button_value, height=3, width=3, borderwidth=1, command=when_pressed)
button1.grid(row=row + 1, column=col if button_value != "/" else 3, sticky="ew")
equal_button = Button(window, text="=", height=3, width=3, borderwidth=1, command=equal_pressed)
equal_button.grid(row=4, column=1, sticky="ew")
clear_button = Button(window, text="C", height=3, width=3, borderwidth=1, command=clear_pressed)
clear_button.grid(row=4, column=2, sticky="ew")
window.mainloop()