-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.py
63 lines (48 loc) · 1.63 KB
/
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
#Task 2 Given by Codsoft
# Python program to create a simple calculator
import tkinter as tk
def on_click(event):
current_text = entry.get()
text = event.widget.cget("text")
if text == "=":
try:
result = eval(current_text)
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")
elif text == "C":
entry.delete(0, tk.END)
else:
entry.insert(tk.END, text)
def create_button(text, row, col, col_span=1):
button = tk.Button(root, text=text, font=("Helvetica", 18), bd=5)
button.grid(row=row, column=col, columnspan=col_span, padx=5, pady=5)
button.bind("<Button-1>", on_click) # Bind the button click event to the on_click() function
return button
root = tk.Tk()
root.title("Calculator")
root.geometry("300x400")
heading_label = tk.Label(root, text="Calculator", font=("Helvetica", 16, "bold"),highlightcolor=("red"))
heading_label.grid(row=0, column=0, columnspan=4, pady=20)
entry = tk.Entry(root, font=("Helvetica", 24), bd=5, justify=tk.RIGHT)
entry.grid(row=1, column=0, columnspan=4, padx=10, pady=10)
create_button("7", 2, 0)
create_button("8", 2, 1)
create_button("9", 2, 2)
create_button("/", 2, 3)
create_button("4", 3, 0)
create_button("5", 3, 1)
create_button("6", 3, 2)
create_button("*", 3, 3)
create_button("1", 4, 0)
create_button("2", 4, 1)
create_button("3", 4, 2)
create_button("-", 4, 3)
create_button("0", 5, 0)
create_button(".", 5, 1)
create_button("=", 5, 2)
create_button("+", 5, 3)
create_button("C", 6, 0, 3)
root.mainloop()