Passed
Push — dev ( cad626...3dc5ca )
by Andreas
52s
created

w_settings.popup_settings()   B

Complexity

Conditions 4

Size

Total Lines 47
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 47
ccs 0
cts 12
cp 0
rs 8.9919
c 0
b 0
f 0
cc 4
nop 0
crap 20
1
"""Отрисовка окна настроек."""
2 1
import os
3 1
import yaml
4 1
from tkinter import BooleanVar
5
from tkinter import Toplevel, LabelFrame, PhotoImage, Label, messagebox
6
from tkinter.ttk import Combobox, Button, Checkbutton
7
from f_getconfig import getconfig
8
9
POPUP_WIDTH: int = 160
10
POPUP_HEIGHT: int = 110
11
12
13
def check_langs():
14
    """Проверяет какие локализации доступны."""
15
    langs: list = os.listdir('l10n')
16
    return langs
17
18
19
def apply(lang, popup, log_var):
20
    """Применяет изменения и вносит их в конфиг."""
21
    conffile = open('conf/main.yml', 'r')
22
    config = yaml.full_load(conffile)
23
    conffile.close()
24
    config['settings']['locale'] = lang
25
    config['settings']['logging'] = log_var
26
    with open(r'conf/main.yml', 'w') as file:
27
        yaml.dump(config, file)
28
    messagebox.showinfo(_('Information'),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
29
                        _('Settings will be applied after programm restart.'))
30
    print(log_var)
31
    popup.destroy()
32
33
34
def current_lang():
35
    """Определяет индекс текущего языка для выпадающего списка."""
36
    return check_langs().index(getconfig()['settings']['locale'])
37
38
39
def popup_settings():
40
    """Открывает окно 'Настройки'."""
41
    popup = Toplevel()
42
    log_var = BooleanVar()
43
    launchicon = PhotoImage(file = 'data/imgs/20ok.png')
44
    center_x_pos = int(popup.winfo_screenwidth() / 2) - POPUP_WIDTH
45
    center_y_pos = int(popup.winfo_screenheight() / 2) - POPUP_HEIGHT
46
47
    popup.geometry(f'{POPUP_WIDTH}x{POPUP_HEIGHT}+'
48
                   f'{center_x_pos}+{center_y_pos}')
49
    popup.title(_('Settings'))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
50
    popup.resizable(False, False)
51
    frame_settings = LabelFrame(popup, text = _('Settings'))
52
    frame_settings.grid(sticky = 'NWSE', column = 0, row = 0,
53
                        ipadx = 5, padx = 5, pady = 5)
54
55
    lang_label = Label(frame_settings, text = _('Localisation'))
56
    lang_label.grid(column = 0, row = 0, ipadx = 5)
57
58
    logs_label = Label(frame_settings, text = _('Logging'))
59
    logs_label.grid(column = 0, row = 1, ipadx = 5)
60
61
    lang_vars = Combobox(frame_settings, state = 'readonly',
62
                         values = check_langs(), width = 4)
63
    lang_vars.current(current_lang())
64
    lang_vars.grid(column = 1, row = 0)
65
    log_settings = Checkbutton(frame_settings,
66
                               variable = log_var, onvalue = True,
67
                               offvalue = False)
68
69
    log_settings.grid(column = 1, row = 1)
70
71
    if getconfig()['settings']['logging'] is True:
72
        log_var.set(True)
73
    elif getconfig()['settings']['logging'] is False:
74
        log_var.set(False)
75
76
    apply_button = Button(popup, text = _('Apply'),
77
                          width = 20, compound = 'left',
78
                          image = launchicon,
79
                          command = lambda: apply(lang_vars.get(),
80
                                                  popup, log_var.get()))
81
    apply_button.grid(column = 0, row = 1)
82
83
    popup.grab_set()
84
    popup.focus_set()
85
    popup.wait_window()
86
87
88
if __name__ == '__main__':
89
    print(current_lang())
90