-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_calculator.py
116 lines (92 loc) · 4.53 KB
/
simple_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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
## Create a GUI-based simple calculator,
# which can perform basic arithmetic operations.
from tkinter import *
root = Tk()
OPERATORS = ('÷','x','+','-', '%')
# Expand the window
root.geometry('350x500+150+300')
root.title('Calculator')
Grid.rowconfigure(root,0,weight=1)
Grid.columnconfigure(root,0,weight=1)
content = Frame(root)
content.grid(sticky='NSEW')
content.columnconfigure(0, weight=1)
content.columnconfigure(1, weight=1)
content.columnconfigure(2, weight=1)
content.columnconfigure(3, weight=1)
content.rowconfigure(0, weight=1)
content.rowconfigure(1, weight=1)
content.rowconfigure(2, weight=1)
content.rowconfigure(3, weight=1)
content.rowconfigure(4, weight=1)
content.rowconfigure(5, weight=1)
# Create the main screen
display = Label(content, text='Display', bg='#363636', fg='white', relief='raised', font=('Ariel', 32), anchor=E, padx=12)
display.grid(columnspan=4, sticky='NSEW')
# Functionality
# Have the buttons display text when clicked.
# StringVar automatically updates the display when its contents change.
stringvar = StringVar()
stringvar.set("0")
display.configure(textvariable=stringvar)
def clicked(digit):
text = stringvar.get()
if text.endswith(OPERATORS) and digit in OPERATORS:
text = text[:-1] + digit
elif text == '0' and digit.isdigit():
text = digit
elif digit in OPERATORS and text != '0':
stringvar.set(str(evaluate(text)) + digit)
return
elif digit == 'C' or (text.endswith(OPERATORS) and digit in OPERATORS) or digit in OPERATORS and text == '0':
stringvar.set('0')
return
else:
text += digit
stringvar.set(text)
def equals(stringvar=stringvar):
text = stringvar.get()
text = text.rstrip(''.join(OPERATORS))
if text != '':
stringvar.set(str(evaluate(text)))
return
def evaluate(text):
text = text.replace('x', '*')
text = text.replace('÷', '/')
result = eval(text)
if str(result).endswith('.0'):
result = int(str(result)[:-2])
result = round(result, 2)
return result
def plus_minus(stringvar=stringvar):
text = stringvar.get()
if text.startswith('-'):
text = text[1:]
else:
text = str(evaluate(text))
text = '-' + text
stringvar.set(text)
return
# Operation buttons
Button(content, text='=', command=equals, bg='Orange', activebackground='Orange', font=('Ariel', 16, 'bold')).grid(column=3, row=5, sticky='NSEW')
Button(content, text='÷', command=lambda: clicked('÷'), font=('Ariel', 16, 'bold')).grid(column=3, row=1, sticky='NSEW')
Button(content, text='x', command=lambda: clicked('x'), font=('Ariel', 16, 'bold')).grid(column=3, row=2, sticky='NSEW')
Button(content, text='-', command=lambda: clicked('-'), font=('Ariel', 16, 'bold')).grid(column=3, row=3, sticky='NSEW')
Button(content, text='+', command=lambda: clicked('+'), font=('Ariel', 16, 'bold')).grid(column=3, row=4, sticky='NSEW')
# Integer buttons
Button(content, text='7', command=lambda: clicked('7'), font=('Ariel', 16)).grid(column=0, row=2, sticky='NSEW')
Button(content, text='8', command=lambda: clicked('8'), font=('Ariel', 16)).grid(column=1, row=2, sticky='NSEW')
Button(content, text='9', command=lambda: clicked('9'), font=('Ariel', 16)).grid(column=2, row=2, sticky='NSEW')
Button(content, text='4', command=lambda: clicked('4'), font=('Ariel', 16)).grid(column=0, row=3, sticky='NSEW')
Button(content, text='5', command=lambda: clicked('5'), font=('Ariel', 16)).grid(column=1, row=3, sticky='NSEW')
Button(content, text='6', command=lambda: clicked('6'), font=('Ariel', 16)).grid(column=2, row=3, sticky='NSEW')
Button(content, text='1', command=lambda: clicked('1'), font=('Ariel', 16)).grid(column=0, row=4, sticky='NSEW')
Button(content, text='2', command=lambda: clicked('2'), font=('Ariel', 16)).grid(column=1, row=4, sticky='NSEW')
Button(content, text='3', command=lambda: clicked('3'), font=('Ariel', 16)).grid(column=2, row=4, sticky='NSEW')
Button(content, text='0', command=lambda: clicked('0'), font=('Ariel', 16)).grid(column=0, row=5, sticky='NSEW', columnspan=2)
# Misc buttons
Button(content, text='.', command=lambda: clicked('.'), font=('Ariel', 16, 'bold')).grid(column=2, row=5, sticky='NSEW')
Button(content, text='C', command=lambda: clicked('C'), font=('Ariel', 16, 'bold')).grid(column=0, row=1, sticky='NSEW')
Button(content, text='+/-', command=plus_minus, font=('Ariel', 16, 'bold')).grid(column=1, row=1, sticky='NSEW')
Button(content, text='%', command=lambda: clicked('%'), font=('Ariel', 16, 'bold')).grid(column=2, row=1, sticky='NSEW')
root.mainloop()