-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry3.py
More file actions
39 lines (39 loc) · 1.21 KB
/
Copy pathtry3.py
File metadata and controls
39 lines (39 loc) · 1.21 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
import tkinter as tk
def on_click(button_value):
entry.insert(tk.END, button_value)
def clear():
entry.delete(0, tk.END)
def calculate():
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
except Exception:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")
root = tk.Tk()
root.title("Calculator")
root.geometry("300x400")
root.config(bg="blue")
entry = tk.Entry(root, bg="pink",fg="blue",font=("Monaco", 20), justify="right")
entry.pack(fill="both", padx=10, pady=10)
buttons = [
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
]
frame = tk.Frame(root)
frame.config(bg="purple")
frame.pack()
row, col = 0, 0
for button in buttons:
action = lambda b=button: on_click(b) if b != "=" else calculate()
tk.Button(frame, bg="pink",fg="purple",text=button, width=5, height=2, font=("Monaco", 14,"bold"),
command=action).grid(row=row, column=col, padx=5, pady=5)
col += 1
if col > 3:
col = 0
row += 1
tk.Button(root, text="Clear", width=20, height=2, font=("Arial", 14), command=clear).pack(pady=5)
root.mainloop()