-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.py
More file actions
157 lines (125 loc) · 3.99 KB
/
frontend.py
File metadata and controls
157 lines (125 loc) · 3.99 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
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
import tkinter as tk
import backend as be
root = tk.Tk()
active_entry = None
active_var = None
# Actions =============
def close():
"""
Close the program
"""
root.destroy()
def show_notification(notify):
"""
Shows notification if can't connect to exchange rates server
or
if input data are wrong
"""
if notify == 'ErrorURL':
var_notify.set("Can't connect to 'openexchangerates.org'")
else:
var_notify.set('Wrong values!')
def darken_entries(event):
"""
Darken unfocused entries
"""
global active_entry
active_entry = entries_dol[0].focus_get()
def darken(entry):
return '#d4d4d4' if entry != active_entry else '#fff'
for entry in entries_dol:
entry.config(bg=darken(entry))
for entry in entries_rub:
entry.config(bg=darken(entry))
def clean_entries(event):
"""
Cleaning unfocused cells
"""
global active_entry, active_var
for i in range(len(entries_dol)):
# run through dollars
entry = entries_dol[i]
var = vars_dol[i]
if entry != active_entry:
var.set('')
else:
active_var = var
# run through rubles
entry = entries_rub[i]
var = vars_rub[i]
if entry != active_entry:
var.set('')
else:
active_var = var
def convert():
"""
Sending the data to and retrieving back from backend
"""
# deleting notifications
var_notify.set('')
# deleting commas if necessary
value = active_var.get()
if ',' in value:
lst = value.split(',')
value = ''.join(lst)
# absorb values
values_dol = list(map(lambda var: value if var == active_var else '', vars_dol))
values_rub = list(map(lambda var: value if var == active_var else '', vars_rub))
# send values
values_to_set = be.convert_salary(values_dol, values_rub)
# if error returned
if isinstance(values_to_set, str):
show_notification(values_to_set)
return
# fill the entries with commas
for i in range(len(values_to_set[0])):
vars_dol[i].set(f'{values_to_set[0][i]:,}')
vars_rub[i].set(f'{values_to_set[1][i]:,}')
# =====================
# Labels ==============
titles = 'Per', 'hour:', 'day:', 'week:', 'month:', 'year:'
column = 0
for i in range(6):
lbl = tk.Label(root, text=titles[column], font=('Times', 20))
lbl.grid(row=0, column=column)
column += 1
lbl_dol = tk.Label(root, text='$', font=('Times', 20))
lbl_dol.grid(row=1, column=0)
lbl_rub = tk.Label(root, text='₽', font=('Times', 20))
lbl_rub.grid(row=2, column=0)
var_notify = tk.StringVar()
lbl_notify = tk.Label(root, textvariable=var_notify)
lbl_notify.grid(row=4, column=0, columnspan=2)
# =====================
# Entries =============
vars_dol = []
for i in range(5):
vars_dol.append(tk.StringVar())
entries_dol = []
for i in range(5):
entries_dol.append(tk.Entry(root, width=10, textvariable=vars_dol[i], font=('Times', 20)))
entries_dol[i].grid(row=1, column=i+1)
vars_rub = []
for i in range(5):
vars_rub.append(tk.StringVar())
entries_rub = []
for i in range(5):
entries_rub.append(tk.Entry(root, width=10, textvariable=vars_rub[i], font=('Times', 20)))
entries_rub[i].grid(row=2, column=i+1)
# binding entries
for entry in entries_dol:
entry.bind('<Key>', clean_entries)
entry.bind('<Return>', lambda event: convert())
entry.bind('<FocusIn>', darken_entries)
for entry in entries_rub:
entry.bind('<Key>', clean_entries)
entry.bind('<Return>', lambda event: convert())
entry.bind('<FocusIn>', darken_entries)
# =====================
# Buttons =============
btn_convert = tk.Button(root, text='Convert', font=('Times', 20), width=30, bg='#ababab', command=convert)
btn_convert.grid(row=3, column=2, columnspan=3)
btn_close = tk.Button(root, text='Close', font=('Times', 15), bg='#ababab', command=close)
btn_close.grid(row=4, column=5)
# =====================
root.mainloop()