Completed
Push — master ( 91d748...e1a893 )
by Andreas
19s queued 12s
created

w_settings.current_lang()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 0
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'),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
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'))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
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