#!/usr/bin/python import sys from xml.dom import minidom import Tkinter def attr_to_dict(attr): ''' convert (widget or grid_location) attributes into a dict ''' dict = {} for a in attr.values(): if a.name == 'image': dict[a.name] = Tkinter.PhotoImage(file=a.value) elif a.name == 'command': module = sys.modules[__name__] dict[a.name] = getattr(module, a.value) else: dict[a.name] = a.value return dict def construct_widget_dict(): ''' Go through all widget xml nodes on first level, I mean, those widgets defined inside other widget will not be checked. If the widget has a name, it will be recorded in the widget_dict.''' widgets = [e for e in xmldoc.firstChild.childNodes if e.nodeType == e.ELEMENT_NODE and e.nodeName == "widget"] for w in widgets: if "name" in w.attributes.keys(): widget_dict[w.attributes["name"].value] = w def widget(w_xml, p_win): ''' create a widget. w_xml is a xml node describing the widget. p_win is the parent widget.''' func = getattr(Tkinter, w_xml.attributes["type"].value) # convert attributes into dictionary wa_list = [wa for wa in w_xml.childNodes if wa.nodeType == wa.ELEMENT_NODE and wa.nodeName =="widget_attribute"] if wa_list: # only process the first element, and there should be only one element. attr_dict = attr_to_dict(wa_list[0].attributes) w_win = func(p_win, attr_dict) else: w_win = func(p_win) gm_list = [m for m in w_xml.childNodes if m.nodeType == m.ELEMENT_NODE and m.nodeName == "grid_management"] gl_list = [l for l in w_xml.childNodes if l.nodeType == l.ELEMENT_NODE and l.nodeName == "grid_location"] w_list = [ w for w in w_xml.childNodes if w.nodeType == w.ELEMENT_NODE and w.nodeName == "widget" ] for w in w_list: widget(w, w_win) for m in gm_list: for cf in m.childNodes: if cf.nodeType != m.ELEMENT_NODE or cf.nodeName != "configure": continue if "row" in cf.attributes.keys(): cf_func = getattr(w_win, "rowconfigure") n = cf.attributes["row"].value elif "column" in cf.attributes.keys(): cf_func = getattr(w_win, "columnconfigure") n = cf.attributes["column"].value else: continue cf_func(n, weight=cf.attributes["weight"].value) # only process the first item break for l in gl_list: ld = attr_to_dict(l.attributes) w_win.grid(ld) # only process the first item break def display(): ''' Display a series of widgets ''' root_window = Tkinter.Tk() # first display the main frame widget(widget_dict["main"], root_window) root_window.columnconfigure(0, weight=1) root_window.rowconfigure(0, weight=1) root_window.geometry('800x600+0+0') root_window.mainloop() # commands for buttons def quit(): print "quit" def previous_step(): print "previous step" def next_step(): print "next step" xmldoc = minidom.parse("../../xml/interface_t.xml") widget_dict={} construct_widget_dict() sequence_dict={} construct_sequence_dict() display()