-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.py
50 lines (37 loc) · 1.09 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
import tkinter as tk
def evaluate():
try:
user_input = entry.get()
output = eval(user_input)
entry.delete(0, tk.END)
entry.insert(tk.END, str(output))
except:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")
def clear_entry():
entry.delete(0, tk.END)
# Create the main window
window = tk.Tk()
window.title("Simple Calculator")
# Create an entry widget
entry = tk.Entry(window, width=20)
entry.grid(row=0, column=0, columnspan=4)
# Create button widgets
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
row_val = 1
col_val = 0
for button in buttons:
tk.Button(window, text=button, width=5, height=2, command=lambda b=button: entry.insert(tk.END,
b) if b != "=" and b != "C" else evaluate() if b == "=" else clear_entry()).grid(
row=row_val, column=col_val)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
# Run the event loop
window.mainloop()