-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic calculator.py
61 lines (50 loc) · 1.7 KB
/
basic 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
import tkinter as tk
import time
print('*****|Basic Calculator|*****')
print('Program version: 1.11.2.')
print('')
print('This is a basic calculator coded in Python! You can check the advanced version of it from: https://github.com/GamerSoft24/Python/blob/Main/PySoft/advanced%20calculator.py')
print("You can either type the numbers & caracters in or click the buttons.")
time.sleep(3)
def button_click(number):
current = entry.get()
entry.delete(0, tk.END)
entry.insert(0, current + str(number))
def clear():
entry.delete(0, tk.END)
def calculate():
current = entry.get()
try:
result = eval(current)
entry.delete(0, tk.END)
entry.insert(0, str(result))
except Exception as e:
entry.delete(0, tk.END)
entry.insert(0, "Error")
def on_enter(event):
calculate()
window = tk.Tk()
window.title("Basic Calculator")
entry = tk.Entry(window, width=20, borderwidth=5)
entry.grid(row=0, column=0, columnspan=4)
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+'
]
row_val = 1
col_val = 0
for button in buttons:
if button == '=':
tk.Button(window, text=button, padx=20, pady=20, command=calculate).grid(row=row_val, column=col_val)
elif button == 'C':
tk.Button(window, text=button, padx=20, pady=20, command=clear).grid(row=row_val, column=col_val)
else:
tk.Button(window, text=button, padx=20, pady=20, command=lambda b=button: button_click(b)).grid(row=row_val, column=col_val)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
entry.bind('<Return>', on_enter)
window.mainloop()