test4.py
1.9 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
from xsd_complextype import XSDComplexTypeFrame
from scrollframe import ScrollFrame
from lxml import etree
import tkinter as tk
if __name__=="__main__":
print("Testing complex types")
# load the XSD schema
xsd_schema = etree.parse("spase-2.3.1.xsd")
# store all simple type definitions
simple_types = {e.attrib["name"]:e for e in xsd_schema.iter("{*}simpleType")}
complex_types = {e.attrib["name"]:e for e in xsd_schema.iter("{*}complexType")}
# create mainwindow and populate
mainwindow = tk.Tk()
mainframe = tk.Frame(mainwindow)
scrollFrame = ScrollFrame(mainframe) # add a new scrollable frame.
# populate window with all simpleTypes found in the XSD schema
c=1
form_frame = None
for complex_type in xsd_schema.iter("{*}complexType"):
form_frame = XSDComplexTypeFrame(parent = scrollFrame.viewPort,\
element = complex_type,\
simple_types=simple_types,\
complex_types=complex_types)
form_frame.pack(side=tk.TOP, fill=tk.X, expand=True)
c-=1
if c==0:
break
scrollFrame.pack(side="top", fill="both", expand=True)
mainframe.pack(side="top", fill="both", expand=True)
# add a submit and cancel button at bottom
action_frame = tk.Frame(scrollFrame.viewPort)
def save_tree(t):
with open("mytree.xml","wb") as f:
a=etree.tostring(t, pretty_print=True)
f.write(a)
f.close()
submit_button = tk.Button(action_frame, text="Submit", command=lambda form=form_frame: save_tree(form.get_content()))
cancel_button = tk.Button(action_frame, text="Cancel", command=mainwindow.quit)
submit_button.pack(side=tk.LEFT, fill=tk.X, expand=1)
cancel_button.pack(side=tk.RIGHT, fill=tk.X, expand=1)
action_frame.pack(side=tk.TOP, fill=tk.X, expand=1)
# start event loop
mainwindow.mainloop()