-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
94 lines (80 loc) · 3.77 KB
/
config.py
File metadata and controls
94 lines (80 loc) · 3.77 KB
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
from mt import ensure_venv, deprecated
ensure_venv(__file__)
deprecated(__name__)
from tkinter.ttk import Button, Label, Radiobutton, Frame
from tkinter import Text, BooleanVar
from ttkthemes import ThemedStyle
from papertools import File
from typing import Callable
class GUI:
def __init__(self, root: Frame, themes: list[str], save_callback: Callable) -> None:
self.root: Frame = root
self.themes: list[str] = themes
self.style: ThemedStyle = ThemedStyle(root)
self.save_callback: Callable = save_callback
self.main_frame: Frame = Frame(self.root)
self.main_frame.pack(fill='both', expand=True)
self.cfg: dict = File('config.json').json_r()
self.entries: dict = {}
for group in self.cfg.keys():
self.add_group(group)
self.entries[group] = {}
for key, value in self.cfg[group].items():
self.add_option(group, key, value, type(value))
self.button_frame: Frame = Frame(self.root)
self.button_frame.pack(side='bottom', fill='x')
self.button1: Button = Button(
self.button_frame, text="Speichern", command=self.save)
self.button1.pack(fill='x')
def add_option(self, group: str, name: str, value: str, type: type) -> None:
frame: Frame = Frame(self.main_frame)
frame.pack(fill='x', padx=20, pady=1)
label: Label = Label(frame, text=name)
label.pack(side='left')
if type == bool:
var = BooleanVar(value=bool(value))
true_button = Radiobutton(
frame, text="True", variable=var, value=True)
false_button = Radiobutton(
frame, text="False", variable=var, value=False)
true_button.pack(side='left', padx=5)
false_button.pack(side='left', padx=5)
self.entries[group][name] = var
else:
text_field: Text = Text(frame, height=1, width=20)
text_field.insert('1.0', value)
text_field.pack(side='left', padx=5, fill='x', expand=True)
self.entries[group][name] = text_field
def add_group(self, name: str) -> None:
frame: Frame = Frame(self.main_frame)
frame.pack(fill='x', padx=5, pady=5)
label: Label = Label(frame, text=name)
label.pack(side='left')
def save(self) -> None:
for group, options in self.cfg.items():
for name in options.keys():
if isinstance(self.entries[group][name], BooleanVar):
self.cfg[group][name] = self.entries[group][name].get()
elif name == 'theme':
if self.entries[group][name].get(
'1.0', 'end-1c') in self.themes:
self.cfg[group][name] = self.entries[group][name].get(
'1.0', 'end-1c')
self.entries[group][name].config(bg=self.style.lookup(
'TFrame', 'background') or '#000')
else:
self.entries[group][name].config(bg='#800')
elif name == 'user':
if len(self.entries[group][name].get(
'1.0', 'end-1c')) <= 10:
self.cfg[group][name] = self.entries[group][name].get(
'1.0', 'end-1c')
self.entries[group][name].config(bg=self.style.lookup(
'TFrame', 'background') or '#000')
else:
self.entries[group][name].config(bg='#800')
else:
self.cfg[group][name] = self.entries[group][name].get(
'1.0', 'end-1c')
File('config.json').json_w(self.cfg)
self.save_callback(self.cfg)