-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalc.py
65 lines (60 loc) · 1.92 KB
/
calc.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
64
65
import math
import tkinter as tk
def startCalc():
def clk(key):
global memory
if key == '=':
if('/' in entry.get() and '.' not in entry.get()):
entry.insert(tk.END, ".0")
str1 = "-+0123456789."
if(entry.get()[0] not in str1):
entry.insert(tk.END, "first char not in " + str1)
try:
result = eval(entry.get())
entry.insert(tk.END, " = " + str(result))
except:
entry.insert(tk.END, "--> Error!")
elif(key == 'C'):
entry.delete(0, tk.END)
elif(key == '->M'):
memory = entry.get()
if('=' in memory):
ix = memory.find('=')
memory = memory[ix+2:]
root.title('M=' + memory)
elif(key == 'M->'):
entry.insert(tk.END, memory)
elif(key == 'neg'):
if('=' in entry.get()):
entry.delete(0, tk.END)
try:
if(entry.get()[0] == '-'):
entry.delete(0)
else:
entry.insert(0, '-')
except IndexError:
pass
else:
if('=' in entry.get()):
entry.delete(0, tk.END)
entry.insert(tk.END, key)
root = tk.Tk()
root.title("Calculator")
buttons = [
'7', '8', '9', '*', 'C',
'4', '5', '6', '/', 'M->',
'1', '2', '3', '-', '->M',
'0', '.', '=', '+', 'neg']
r = 1
c = 0
for b in buttons:
cmd = lambda x = b: clk(x)
tk.Button(root, text = b, width = 5, relief = "ridge", command = cmd).grid(row = r, column = c)
c += 1
if c > 4:
c = 0
r += 1
entry = tk.Entry(root, width = 33, bg = "white")
entry.grid(row = 0, column = 0, columnspan = 5)
root.iconbitmap('icon.ico')
root.mainloop()