Completed
Push — master ( 931044...cb1112 )
by Andreas
22s queued 11s
created

w_settings.popup_settings()   B

Complexity

Conditions 4

Size

Total Lines 47
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 47
rs 8.9919
c 0
b 0
f 0
cc 4
nop 0
1
"""Отрисовка окна настроек."""
2
import os
3
import yaml
4
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
    popup.destroy()
31
32
33
def current_lang():
34
    """Определяет индекс текущего языка для выпадающего списка."""
35
    return check_langs().index(getconfig()['settings']['locale'])
36
37
38
def popup_settings():
39
    """Открывает окно 'Настройки'."""
40
    popup = Toplevel()
41
    log_var = BooleanVar()
42
    launchicon = PhotoImage(file = 'data/imgs/20ok.png')
43
    center_x_pos = int(popup.winfo_screenwidth() / 2) - POPUP_WIDTH
44
    center_y_pos = int(popup.winfo_screenheight() / 2) - POPUP_HEIGHT
45
46
    popup.geometry(f'{POPUP_WIDTH}x{POPUP_HEIGHT}+'
47
                   f'{center_x_pos}+{center_y_pos}')
48
    popup.title(_('Settings'))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
49
    popup.resizable(False, False)
50
    frame_settings = LabelFrame(popup, text = _('Settings'))
51
    frame_settings.grid(sticky = 'NWSE', column = 0, row = 0,
52
                        ipadx = 5, padx = 5, pady = 5)
53
54
    lang_label = Label(frame_settings, text = _('Localisation'))
55
    lang_label.grid(column = 0, row = 0, ipadx = 5)
56
57
    logs_label = Label(frame_settings, text = _('Logging'))
58
    logs_label.grid(column = 0, row = 1, ipadx = 5)
59
60
    lang_vars = Combobox(frame_settings, state = 'readonly',
61
                         values = check_langs(), width = 4)
62
    lang_vars.current(current_lang())
63
    lang_vars.grid(column = 1, row = 0)
64
    log_settings = Checkbutton(frame_settings,
65
                               variable = log_var, onvalue = True,
66
                               offvalue = False)
67
68
    log_settings.grid(column = 1, row = 1)
69
70
    if getconfig()['settings']['logging'] is True:
71
        log_var.set(True)
72
    elif getconfig()['settings']['logging'] is False:
73
        log_var.set(False)
74
75
    apply_button = Button(popup, text = _('Apply'),
76
                          width = 20, compound = 'left',
77
                          image = launchicon,
78
                          command = lambda: apply(lang_vars.get(),
79
                                                  popup, log_var.get()))
80
    apply_button.grid(column = 0, row = 1)
81
82
    popup.grab_set()
83
    popup.focus_set()
84
    popup.wait_window()
85
86
87
if __name__ == '__main__':
88
    print(current_lang())
89