-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathelement2json.py
More file actions
executable file
·112 lines (101 loc) · 3.39 KB
/
Copy pathelement2json.py
File metadata and controls
executable file
·112 lines (101 loc) · 3.39 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
"""
make elements.json and elements_test.py
"""
import json
import os
SCRIPT_DIR = os.path.dirname(__file__)
TEST_FILENAME = os.path.join(SCRIPT_DIR, "elements_test.py")
FILE_ELEMENTS = os.path.join(SCRIPT_DIR, "docs", "scripts", "elements.json")
COLS_PER_ROW = 4
def read_file() -> tuple[str, dict[str, list[str]]]:
"""Read elements.json"""
with open(FILE_ELEMENTS, "r", encoding="utf-8") as fp:
elements = json.load(fp)
args = {}
for e in elements:
args[e] = []
if e in [
"Button",
"Text",
"Input",
"Frame",
"Checkbox",
"Label",
"InputText",
"Multiline",
]:
args[e].append(f"'{e}'")
if e in ["Column", "Frame"]:
args[e].append("layout=[[eg.Button('OK')]]")
if e in ["Menu"]:
args[e].append(
"menu_definition=[['File', ['Open', 'Save', 'Exit']], ['Edit', ['Copy', 'Paste']]]"
)
if e in ["Table"]:
args[e].append('values=[["a","b","c"],["4","5","6"],["7","8","9"]]')
args[e].append("headings=['aaa', 'bbb', 'ccc']")
if e in ["Image"]:
args[e].append("filename=IMAGE_FILE")
args[e].append("size=(100,100)")
if e in ["Canvas"]:
args[e].append("size=(100,100)")
if e in ["Graph"]:
args[e].append("size=(100,100)")
if e in ["Combo"]:
args[e].append("values=['combo1', 'combo2', 'combo3']")
args[e].append("default_value='combo1'")
if e in ["Listbox", "ListBrowse"]:
args[e].append("values=['item1', 'item2', 'item3']")
if e in ["Tab"]:
args[e].append("title='tab'")
args[e].append("layout=[[eg.Button('OK')]]")
if e in ["TabGroup"]:
args[e].append("layout=[[ eg.Tab('tab1', layout=[[eg.Button('OK')]]) ]]")
return elements, args
def make_code():
"""Make elements_test.py"""
elements, init_args = read_file()
src = """
\"\"\"
Elements Test: auto generated by element2json.py
\"\"\"
# Test all elements of tkeasygui
import os
import TkEasyGUI as eg
# check Image file
IMAGE_FILE = "a.jpg" if os.path.exists("a.jpg") else "tests/a.jpg"
# layout with all elements
layout = [
"""
src += " [\n"
for i, e in enumerate(elements):
if e == "ImageResizeType" or e == "FrameScrollable":
continue
args = init_args.get(e, {})
args_s = ",".join(args)
if ("Browse" in e) or ("FileSaveAs" == e):
src += (
f" eg.Frame('{e}', layout=[[eg.Input(''), eg.{e}({args_s})]]),\n"
)
else:
src += f" eg.Frame('{e}', layout=[[eg.{e}({args_s})]]),\n"
if i % COLS_PER_ROW == (COLS_PER_ROW - 1):
src += " ],\n"
src += " [\n"
src += " ],\n"
src += "]\n"
src += """
window = eg.Window(f"all element v.{eg.__version__}", layout=layout, size=(800, 600), font=("Arial", 12), resizable=True, show_scrollbar=True)
for event, values in window.event_iter():
print("[EVENT]", event, values)
if event == "OK":
break
"""
print(f"==={TEST_FILENAME}===")
print(src)
with open("elements_test.py", "w", encoding="utf-8") as f:
f.write(src)
# os.system("python elements_test.py")
if __name__ == "__main__":
make_code()