-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.py
More file actions
47 lines (39 loc) · 1.29 KB
/
Copy pathCalculator.py
File metadata and controls
47 lines (39 loc) · 1.29 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
from tkinter import Tk, Entry, StringVar, Frame, Button
def click(event):
text = event.widget.cget("text")
if text == "=":
try:
value = eval(screen.get())
screen_value.set(value)
except Exception:
screen_value.set("Error")
elif text == "C":
screen_value.set("")
else:
screen_value.set(screen_value.get() + text)
# Main window
cal = Tk()
cal.geometry("400x560")
cal.title("Super Calculator")
# Screen
screen_value = StringVar()
screen_value.set("")
screen = Entry(cal, font="lucida 45 bold", bg="lightblue", textvariable=screen_value)
screen.pack(pady=10, fill="x", padx=10)
# Button Frame
button_frame = Frame(cal)
button_frame.pack()
# Buttons
button_texts = [
("C", 1, 0), ("*", 1, 1), ("=", 1, 2), ("/", 1, 3),
("7", 2, 0), ("8", 2, 1), ("9", 2, 2), ("+", 2, 3),
("4", 3, 0), ("5", 3, 1), ("6", 3, 2), ("-", 3, 3),
("1", 4, 0), ("2", 4, 1), ("3", 4, 2), ("0", 4, 3)
]
for (text, row, col) in button_texts:
button = Button(button_frame, text=text, font="lucida 25 bold", padx=20, pady=20, bg="grey")
button.grid(row=row, column=col, padx=5, pady=5)
button.bind('<Button-1>', click)
# Tkinter event loop
cal.mainloop()
# Super Calculator by Shashank