(:title Create UI using XML):
|Writing your own widgets||Widgets|
To create UI using XML, you need to use the XMLWidget. It's preferable to use a class for embedding the xml + event handler. Here is an example of XML :
xml = '''
<MTButton label="1" id='"btn1"'/>
<MTButton label="2" id='"btn2"'/>
<MTButton label="3" id='"btn3"'/>
def __init__(self, **kwargs):
xml = XMLWidget(xml=MainUI.xml)
def on_btn2_press(self, *largs):
print 'my button2 is pressed'
Things are very simple:
- You create an instance of XMLWidget and pass your xml definition
- You try to connect event from button to the class
- Last, you're adding the root of xml to our widget.
autoconnect() will search for handler automaticly in the instance passed as the first argument.
In the example, the button with the
"btn2" is found. It will search for a method named :
So you can declare method like
on_btn3_touch_down... and put action on this handler.
In XMLWidget, the value of each attribute actually use eval() to understand the content. Be careful to not pass user data, otherwise, you'll introduce a security issue !