1
|
|
|
"""Отрисовка окна настроек.""" |
2
|
|
|
import os |
3
|
|
|
import yaml |
4
|
|
|
from tkinter import Toplevel, LabelFrame, PhotoImage, Label, messagebox |
5
|
|
|
from tkinter.ttk import Combobox, Button |
6
|
|
|
from f_getconfig import getconfig |
7
|
|
|
|
8
|
|
|
POPUP_WIDTH: int = 180 |
9
|
|
|
POPUP_HEIGHT: int = 90 |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def check_langs(): |
13
|
|
|
"""Проверяет какие локализации доступны.""" |
14
|
|
|
langs: list = os.listdir('l10n') |
15
|
|
|
return langs |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def apply(lang, popup): |
19
|
|
|
"""Применяет изменения и вносит их в конфиг.""" |
20
|
|
|
conffile = open('conf/main.yml', 'r') |
21
|
|
|
config = yaml.full_load(conffile) |
22
|
|
|
conffile.close() |
23
|
|
|
config['settings']['locale'] = lang |
24
|
|
|
with open(r'conf/main.yml', 'w') as file: |
25
|
|
|
yaml.dump(config, file) |
26
|
|
|
messagebox.showinfo(_('Information'), |
|
|
|
|
27
|
|
|
_('Settings will be applied after programm restart.')) |
28
|
|
|
popup.destroy() |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def current_lang(): |
32
|
|
|
"""Определяет индекс текущего языка для выпадающего списка.""" |
33
|
|
|
return check_langs().index(getconfig()['settings']['locale']) |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def popup_settings(): |
37
|
|
|
"""Открывает окно 'Настройки'.""" |
38
|
|
|
popup = Toplevel() |
39
|
|
|
launchicon = PhotoImage(file = 'data/imgs/20ok.png') |
40
|
|
|
center_x_pos = int(popup.winfo_screenwidth() / 2) - POPUP_WIDTH |
41
|
|
|
center_y_pos = int(popup.winfo_screenheight() / 2) - POPUP_HEIGHT |
42
|
|
|
|
43
|
|
|
popup.geometry(f'{POPUP_WIDTH}x{POPUP_HEIGHT}+' |
44
|
|
|
f'{center_x_pos}+{center_y_pos}') |
45
|
|
|
popup.title(_('Settings')) |
|
|
|
|
46
|
|
|
frame_settings = LabelFrame(popup, text = _('Settings')) |
47
|
|
|
frame_settings.grid(sticky = 'NWSE', column = 0, row = 0, |
48
|
|
|
ipadx = 5, padx = 5, pady = 5) |
49
|
|
|
|
50
|
|
|
lang_label = Label(frame_settings, text = _('Localisation')) |
51
|
|
|
lang_label.grid(column = 0, row = 0, ipadx = 5) |
52
|
|
|
|
53
|
|
|
lang_vars = Combobox(frame_settings, state = 'readonly', |
54
|
|
|
values = check_langs(), width = 4) |
55
|
|
|
lang_vars.current(current_lang()) |
56
|
|
|
lang_vars.grid(column = 1, row = 0) |
57
|
|
|
|
58
|
|
|
apply_button = Button(popup, text = _('Apply'), |
59
|
|
|
width = 20, compound = 'left', |
60
|
|
|
image = launchicon, |
61
|
|
|
command = lambda: apply(lang_vars.get(), popup)) |
62
|
|
|
apply_button.grid(column = 0, row = 1) |
63
|
|
|
|
64
|
|
|
popup.grab_set() |
65
|
|
|
popup.focus_set() |
66
|
|
|
popup.wait_window() |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
if __name__ == '__main__': |
70
|
|
|
print(current_lang()) |
71
|
|
|
|