Using Widgets
| Widgets Basics | Widgets | Writing your own widgets |
In this document
Instance a widget
The syntax to create a widget is :
Some example :
slider = MTSlider(min=100, max=500) # create a slider to select a value (100-500)
slider = MTSlider(orientation='horizontal') # create an horizontal slider
button = MTButton(label='Plop', font_size=24) # create a big button
Next, you must connect the widget somewhere on the tree
Connect widget together
Add to root window
Let's take the slider :
You can add it to window :
Add to another widget
You can create a layout to add multiple widget in. For example, create a grid layout, and add some button :
button1 = MTButton(label='BTN1')
button2 = MTButton(label='BTN2')
layout.add_widget(button1)
layout.add_widget(button2)
This will create a button box with 2 button aligned on horizontal axis.
Then, if your layout is not yet added to a widget, you can still add to window :
Using events
As we've see in Widgets Basics, widget have some event. You can attach on this event to make something. We have multiple syntax to attach on an event.
@widget.event
@widget.event
def on_draw():
print 'on_draw() called from my widget'
The on_draw function is attached to widget.event, and will be called everytime the on_draw() event will be fired (every frame.)
For example, you can attach to the on_press event of a button :
@button.event
def on_press(*largs):
print 'My button is pressed, arguments is', largs
@button.event
def on_release(*largs):
print 'My button is released, arguments is', largs
connect()
The connect() function is a little bit magic. You can use it in several manner.
Let's try with a simple button :
print 'My button is pressed, arguments is', largs
button = MTButton()
button.connect('on_press', my_onpress_callback)
