-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
151 lines (128 loc) · 5.26 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from tkinter import *
root = Tk()
root.title('Calculator')
# initial windows size and its location
root.geometry('330x450+1500+200')
# make windows size fixed
root.resizable(0, 0)
# set background color
root.configure(bg='black')
# the real equation which will affect answer directly
equation = ''
# it decide the text show on the calculator
display = '0'
# create a set of operators to help me check is a char variable a operator
operators = {'*', '/', '+', '-'}
# finish is a bool variable which help me display correctly after pressing '='
finish = False
# this is a function which helps me debug
def checkStatus():
print('display: ', display)
print('equation: ', equation)
# status in the beginning
checkStatus()
# behavior of button number
def show(value):
global finish
global equation
global display
if display == '0':
display = value
equation += value
elif equation[-1] in operators:
display = value
equation += value
else:
if finish:
finish = False
display = value
equation = value
else:
display += value
equation += value
label_result.config(text=display)
checkStatus()
# behavior of button operator
def operator(value):
global finish
global equation
if finish:
finish = False
if len(equation) >= 1 and equation[-1] in operators:
equation[-1] = equation[:-1]+value
else:
equation += value
checkStatus()
# behavior of button 'c'
def backspace():
global display
if len(display):
display = display[:-1]
label_result.config(text=display)
checkStatus()
# behavior of button 'AC'
def clear():
global equation
global display
equation = ''
display = '0'
label_result.config(text=display)
checkStatus()
# behavior of button '='
def calculate():
global finish
global equation
if equation:
try:
equation = str(eval(equation))
except:
equation = 'error'
label_result.config(text=equation)
if equation == 'error':
equation = ''
finish = True
checkStatus()
# the display screen
label_result = Label(root, width=18, height=2, text='0', fg='white',
bg='black', font=('arial', 30, 'bold'), anchor='se')
label_result.pack()
# layout
Button(root, text='AC', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='black', bg='silver', command=lambda: clear()).place(x=10, y=100)
Button(root, text='C', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='black', bg='silver', command=lambda: backspace()).place(x=90, y=100)
Button(root, text='%', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='black', bg='silver', command=lambda: operator('*0.01')).place(x=170, y=100)
Button(root, text='/', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='orange', command=lambda: operator('/')).place(x=250, y=100)
Button(root, text='7', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='#444444', command=lambda: show('7')).place(x=10, y=170)
Button(root, text='8', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='#444444', command=lambda: show('8')).place(x=90, y=170)
Button(root, text='9', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='#444444', command=lambda: show('9')).place(x=170, y=170)
Button(root, text='X', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='orange', command=lambda: operator('*')).place(x=250, y=170)
Button(root, text='4', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='#444444', command=lambda: show('4')).place(x=10, y=240)
Button(root, text='5', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='#444444', command=lambda: show('5')).place(x=90, y=240)
Button(root, text='6', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='#444444', command=lambda: show('6')).place(x=170, y=240)
Button(root, text='-', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='orange', command=lambda: show('-')).place(x=250, y=240)
Button(root, text='1', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='#444444', command=lambda: show('1')).place(x=10, y=310)
Button(root, text='2', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='#444444', command=lambda: show('2')).place(x=90, y=310)
Button(root, text='3', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='#444444', command=lambda: show('3')).place(x=170, y=310)
Button(root, text='+', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='orange', command=lambda: operator('+')).place(x=250, y=310)
Button(root, text='0', width=5, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='#444444', command=lambda: show('0')).place(x=10, y=380)
Button(root, text='.', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='#444444', command=lambda: show('.')).place(x=170, y=380)
Button(root, text='=', width=2, height=1, font=('arial', 30, 'bold'),
bd=1, fg='white', bg='orange', command=lambda: calculate()).place(x=250, y=380)
root.mainloop()