-
Notifications
You must be signed in to change notification settings - Fork 0
/
UFOS_start.pyw
178 lines (159 loc) · 6.53 KB
/
UFOS_start.pyw
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
# Version: 2.0
# Modified: 17.03.2018
# Author: Sergey Talash
import os
from tkinter import *
from tkinter import ttk
import procedures
class Gui:
def __init__(self, pars, row, col, ignore):
self.pars = pars
self.row = row
self.tree_dot = 0
self.column = col
self.max_row = 15
self.padding_x = 5
self.padding_y = 1
self.ent = ttk.Entry(root, width=1)
self.font = 'Arial 9'
self.new_pars = {}
self.selected_keys = []
if ignore:
self.gui_ignore = ['calibration', 'calibration2', 'channel_names', 'device', 'version']
else:
self.gui_ignore = []
self.default_pars = procedures.Settings.get_defaults(os.getcwd())
def make_admin_entry(self):
self.ent.grid(column=1, row=0, sticky='e', padx=self.padding_x, pady=self.padding_y)
self.ent.bind('<Return>', start_with_all_settings)
def make_labels(self, par, selected_key=None):
if selected_key:
if "description" in par.keys():
self.selected_keys = [selected_key, "description"]
for t in par.keys():
if t == "description":
continue
t2 = t
if t in self.gui_ignore:
continue
if t in self.pars.keys():
self.font = 'Arial 9 bold'
if self.row > self.max_row:
self.row = 0
self.column += 2
else:
self.font = 'Arial 9'
if self.tree_dot == 0:
t2 = t
if self.tree_dot == 1:
t2 = global_separator + t
elif self.tree_dot > 1:
t2 = self.tree_dot * ' ' + t
lab = ttk.Label(root, text=t2, font=self.font)
lab.grid(column=self.column, row=self.row, sticky='w', padx=self.padding_x, pady=self.padding_y)
self.selected_keys.append(t)
try:
par[t].keys()
self.row += 1
self.tree_dot += 1
self.make_labels(par[t], t)
self.tree_dot -= 1
except AttributeError:
self.column += 1
if type(par[t]) == list:
t2 = '; '.join([str(i) for i in par[t]])
else:
t2 = par[t]
ent = ttk.Entry(root)
ent.insert(0, str(t2))
ent.grid(column=self.column, row=self.row, sticky='w', padx=self.padding_x, pady=self.padding_y)
self.column -= 1
self.row += 1
description = self.pars
for i in self.selected_keys:
if i in description.keys():
description = description[i]
if not isinstance(description, dict):
try:
procedures.HoverInfo(lab, description)
except:
# Are there any missing descriptions in settings?
pass
if self.selected_keys[-1] != "description":
self.selected_keys.pop()
def retype(self, default_value, new_value):
if isinstance(default_value, (int, float)):
return type(default_value)(new_value)
if isinstance(default_value, list):
new_value = [i.strip() for i in new_value.split(';')]
return [self.retype(need_t, new_v) for need_t, new_v in zip(default_value, new_value)]
else:
return str(new_value)
def save_params(self, *event):
tmp_queue = []
old_fs = 0
for i in Widget.winfo_children(root):
if self.ent is i:
continue
if type(i) == ttk.Label:
t = str(Widget.cget(i, 'text'))
fs = t.count(global_separator)
if tmp_queue:
for k in range(old_fs - fs + 1):
tmp_queue.pop()
tmp_queue.append(t.strip())
position = ''.join(["['{}']".format(item) for item in tmp_queue])
# Inserting all keys and values from default settings
exec("self.new_pars{0} = self.default_pars{0}".format(position))
old_fs = fs
elif type(i) == ttk.Entry:
new = i.get()
# print([new])
position = ''.join(["['{}']".format(item) for item in tmp_queue])
# Inserting values from GUI
exec("self.new_pars{0} = self.retype(self.default_pars{0}, new)".format(position))
for key in self.new_pars.keys():
if isinstance(self.pars[key], str):
self.pars[key] = self.new_pars[key]
elif isinstance(self.pars[key], dict):
self.pars[key] = self.new_pars[key]
self.new_pars = self.pars
try:
procedures.Settings.set(os.getcwd(), self.new_pars, common_pars['device']['id'])
print('UFOS {}: New settings were written successfully.'.format(common_pars['device']['id']))
except Exception as err:
print('ERROR! New settings are incorrect! {}'.format(err))
def make_buttons(self):
but_save = ttk.Button(root, text='Сохранить', command=self.save_params)
# but_save.grid(column=self.column, row=self.max_row * 2, sticky='we', padx=self.padding_x, pady=self.padding_y)
but_save.grid(column=1, row=0, sticky='w', padx=self.padding_x, pady=self.padding_y)
def start_with_all_settings(*event):
global GUI
global show_settings_for_station
if GUI.ent.get() == '9':
show_settings_for_station = False
root.geometry('+200+10')
else:
show_settings_for_station = True
root.geometry('+200+10')
for i in Widget.winfo_children(root):
i.destroy()
GUI = Gui(params, 0, 0, show_settings_for_station)
GUI.make_labels(params)
GUI.make_admin_entry()
GUI.make_buttons()
"""============== GUI Structure =============="""
# Отображать только настройки для наблюдателей станции
show_settings_for_station = True
global_separator = ' '
root = Tk()
root.title('УФОС Настройка')
root.geometry('+200+10')
root.resizable(True, True)
common_pars = procedures.Settings.get_common(os.getcwd())
params = procedures.Settings.get_device(os.getcwd(), common_pars['device']['id'])
GUI = Gui(params, 0, 0, show_settings_for_station)
GUI.make_labels(params)
GUI.make_admin_entry()
GUI.make_buttons()
root.mainloop()