-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator_v1_0.py
209 lines (151 loc) · 7.58 KB
/
calculator_v1_0.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import tkinter as tk
from tkinter import font
MAX_NUM = 12
num1, num2, operator = None, None, None
is_operation = False
is_result = False
last_button = None
last_button_bg = None
last_button_fg = None
def get_result():
global num1, num2, operator, is_result
result = 0
num2 = input_.get()
if num1 is not None:
match operator:
case '%':
result = (float(num2) / 100) * float(num1)
case '÷':
result = float(num1) / float(num2)
case 'x':
result = float(num1) * float(num2)
case '+':
result = float(num1) + float(num2)
case '-':
result = float(num1) - float(num2)
input_.delete(0, 'end')
input_.insert('end', str(round(result, 3)))
is_result = True
def clear_input():
input_.delete(0, 'end')
def click_clear_button():
input_.delete(0, 'end')
input_.insert(0, '0')
def need_to_clear():
global is_operation, is_result
if input_.get().startswith('0') and len(input_.get()) == 1:
clear_input()
if is_result:
clear_input()
is_result = False
if is_operation:
clear_input()
is_operation = False
def click_num_button(num):
global last_button
if len(input_.get()) <= MAX_NUM:
if last_button is not None:
change_button_color(last_button)
need_to_clear()
input_.insert('end', num)
def click_comma_button():
if '.' not in input_.get():
input_.insert('end', '.')
def click_operation_btn(btn_text, button):
global num1, operator, is_operation
change_button_color(button)
operator = btn_text
num1 = input_.get()
is_operation = True
def click_pls_min_btn():
if '-' in input_.get():
input_.delete(0)
else:
input_.insert(0, '-')
def click_percent_btn():
global num1, operator, is_operation
num1 = input_.get()
operator = '%'
is_operation = True
def change_button_color(button):
global last_button, last_button_bg, last_button_fg
# Restore the color of the last pressed button
if last_button is not None:
last_button.config(background=last_button_bg, foreground=last_button_fg)
last_button = None
else:
last_button = button
last_button_bg = button.cget('background')
last_button_fg = button.cget('foreground')
button.config(background=last_button_fg, foreground=last_button_bg)
window = tk.Tk()
window.title('Калькулятор')
window.geometry("350x550")
window.resizable(False, False)
window.config(background='black')
font_for_buttons = font.Font(family='Arial', size=20, weight='normal', slant='roman')
font_for_buttons2 = font.Font(family='Arial', size=20, weight='bold', slant='roman')
font_for_input = font.Font(family='Arial', size=30, weight='bold', slant='roman')
for c in range(4):
window.columnconfigure(index=c, weight=1)
for r in range(7):
window.rowconfigure(index=r, weight=1)
input_ = tk.Entry(window, width=105, font=font_for_input, justify='right', background='black', foreground='white')
input_.insert(0, '0')
input_.grid(row=0, column=0, rowspan=2, columnspan=4, ipady=50, padx=7, pady=7)
button_clear = tk.Button(window, text='c', font=font_for_buttons, background='grey100', command=click_clear_button)
button_clear.grid(row=2, column=0, ipadx=20, ipady=20, padx=7, pady=7)
button_pl_mns = tk.Button(window, text='+/-', font=font_for_buttons, background='grey100', command=click_pls_min_btn)
button_pl_mns.grid(row=2, column=1, ipadx=20, ipady=20, padx=7, pady=7)
button_percents = tk.Button(window, text='%', font=font_for_buttons, background='grey100',
command=lambda: click_percent_btn())
button_percents.grid(row=2, column=2, ipadx=20, ipady=20, padx=7, pady=7)
button_div = tk.Button(window, text='÷', font=font_for_buttons, background='gold3', foreground="white",
command=lambda: click_operation_btn('÷', button_div))
button_div.grid(row=2, column=3, ipadx=20, ipady=20, padx=7, pady=7)
button_7 = tk.Button(window, text='7', font=font_for_buttons2, background='DarkGray', foreground="white",
command=lambda: click_num_button('7'))
button_7.grid(row=3, column=0, ipadx=20, ipady=20, padx=7, pady=7)
button_8 = tk.Button(window, text='8', font=font_for_buttons2, background='DarkGray', foreground="white",
command=lambda: click_num_button('8'))
button_8.grid(row=3, column=1, ipadx=20, ipady=20, padx=7, pady=7)
button_9 = tk.Button(window, text='9', font=font_for_buttons2, background='DarkGray', foreground="white",
command=lambda: click_num_button('9'))
button_9.grid(row=3, column=2, ipadx=20, ipady=20, padx=7, pady=7)
button_mult = tk.Button(window, text='x', font=font_for_buttons, background='gold3', foreground="white",
command=lambda: click_operation_btn('x', button_mult))
button_mult.grid(row=3, column=3, ipadx=20, ipady=20, padx=7, pady=7)
button_4 = tk.Button(window, text='4', font=font_for_buttons2, background='DarkGray', foreground="white",
command=lambda: click_num_button('4'))
button_4.grid(row=4, column=0, ipadx=20, ipady=20, padx=7, pady=7)
button_5 = tk.Button(window, text='5', font=font_for_buttons2, background='DarkGray', foreground="white",
command=lambda: click_num_button('5'))
button_5.grid(row=4, column=1, ipadx=20, ipady=20, padx=7, pady=7)
button_6 = tk.Button(window, text='6', font=font_for_buttons2, background='DarkGray', foreground="white",
command=lambda: click_num_button('6'))
button_6.grid(row=4, column=2, ipadx=20, ipady=20, padx=7, pady=7)
button_minus = tk.Button(window, text='-', font=font_for_buttons, background='gold3', foreground="white",
command=lambda: click_operation_btn('-', button_minus))
button_minus.grid(row=4, column=3, ipadx=20, ipady=20, padx=7, pady=7)
button_1 = tk.Button(window, text='1', font=font_for_buttons2, background='DarkGray', foreground="white",
command=lambda: click_num_button('1'))
button_1.grid(row=5, column=0, ipadx=20, ipady=20, padx=7, pady=7)
button_2 = tk.Button(window, text='2', font=font_for_buttons2, background='DarkGray', foreground="white",
command=lambda: click_num_button('2'))
button_2.grid(row=5, column=1, ipadx=20, ipady=20, padx=7, pady=7)
button_3 = tk.Button(window, text='3', font=font_for_buttons2, background='DarkGray', foreground="white",
command=lambda: click_num_button('3'))
button_3.grid(row=5, column=2, ipadx=20, ipady=20, padx=7, pady=7)
button_plus = tk.Button(window, text='+', font=font_for_buttons, background='gold3', foreground="white",
command=lambda: click_operation_btn('+', button_plus))
button_plus.grid(row=5, column=3, ipadx=20, ipady=20, padx=7, pady=7)
button_0 = tk.Button(window, text='0', font=font_for_buttons2, background='DarkGray', foreground="white",
command=lambda: click_num_button('0'))
button_0.grid(row=6, column=0, columnspan=2, ipadx=65, ipady=20, padx=7, pady=7)
button_comma = tk.Button(window, text=',', font=font_for_buttons2, background='DarkGray', foreground="white",
command=lambda: click_comma_button())
button_comma.grid(row=6, column=2, ipadx=20, ipady=20, padx=7, pady=7)
button_result = tk.Button(window, text='=', font=font_for_buttons, background='gold3', foreground="white",
command=lambda: get_result())
button_result.grid(row=6, column=3, ipadx=20, ipady=20, padx=7, pady=7)
window.mainloop()