forked from orbitbreak-zz/tkinter-calc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtkinter-calc.py
More file actions
88 lines (69 loc) · 2.01 KB
/
tkinter-calc.py
File metadata and controls
88 lines (69 loc) · 2.01 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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from tkinter import *
calc = Tk()
calc.title("CrappyCalc")
calc.config(bg="grey")
# default size of window
calc.geometry("300x500")
# avoid stretching of Window
calc.maxsize(300,500)
calc.minsize(300,500)
buttons = [
'7', '8', '9', '*', 'C',
'4', '5', '6', '/', 'Neg',
'1', '2', '3', '-', '$',
'0', '.', '=', '+', '@']
# set up GUI
row = 1
col = 0
for i in buttons:
button_style = 'raised'
action = lambda x=i: click_event(x)
Button(calc, text=i, width=1,height=5, relief=button_style, command=action,font="arial 13 bold") \
.grid(row=row, column=col, sticky='nesw',padx =1,pady=1)
col += 1
if col > 4:
col = 0
row += 1
display = Entry(calc, width=50, bg="white")
display.grid(row=0, column=0, columnspan=5,pady=5)
def click_event(key):
# = -> calculate results
if key == '=':
# safeguard against integer division
if '/' in display.get() and '.' not in display.get():
display.insert(END, ".0")
# attempt to evaluate results
try:
result = eval(display.get())
display.insert(END, " = " + str(result))
except:
display.insert(END, " Error, use only valid chars")
# C -> clear display
elif key == 'C':
display.delete(0,END)
# $ -> clear display
elif key == '$':
display.delete(0, END)
display.insert(END, "$$$$C.$R.$E.$A.$M.$$$$")
# @ -> clear display
elif key == '@':
display.delete(0, END)
display.insert(END, "wwwwwwwwwwwwwwwwebsite")
# neg -> negate term
elif key == 'neg':
if '=' in display.get():
display.delete(0, END)
try:
if display.get()[0] == '-':
display.delete(0)
else:
display.insert(0, '-')
except IndexError:
pass
# clear display and start new input
else:
if '=' in display.get():
display.delete(0, END)
display.insert(END, key)
# RUNTIME
calc.mainloop()