-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkjson.py
76 lines (57 loc) · 1.94 KB
/
tkjson.py
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
import tkinter as tk
class TkJsonParser:
def __init__(self, json, parent):
self._registry = {}
self._element = self.parse_element(json, parent)
def getItem(self, id) -> tk.Widget:
return self._registry[id]
def getElement(self) -> tk.Widget:
return self._element
def parse_element(self, json, parent) -> tk.Widget:
item = None
blockedKeys = [
"type", "children", "id", "doLayout"
]
if "_src" in json:
args = {}
if "args" in json:
args = json["args"]
json.pop("args")
new = _parseJson(readFile(json["_src"]), args)
json.pop("_src")
new.update(json)
return self.parse_element(new, parent)
item = eval(f"tk.{json['type']}(parent)")
for key in json.keys():
if key not in blockedKeys:
config = {}
config[key] = json[key]
try:
item.config(config)
except Exception as e:
print(f"Erro: {e}")
item.pack()
if "doLayout" in json:
doLayout = json["doLayout"]
func = eval(f"item.{doLayout['type']}")
doLayout.pop("type")
func(doLayout)
if "id" in json:
self._registry[json["id"]] = item
if "children" in json:
for child in json["children"]:
self.parse_element(child, item)
return item
def _parseJson(source, args={}):
true=True
false=False
return eval(source)
def readFile(path):
file = open(path, mode="r", encoding="utf-8")
content = file.read()
file.close()
return content
def parseString(source, parent, args={}):
return TkJsonParser(_parseJson(source, args), parent)
def parseFile(path, parent, args={}):
return parseString(readFile(path), parent, args)