Using Widgets

Widgets BasicsWidgetsWriting your own widgets

Instance a widget

The syntax to create a widget is :

instance = ClassName(key=value, key2=value2)

Some example :

scatter = MTScatter() # create a simple scatter widget
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 :

slider = MTSlider(orientation='horizontal')

You can add it to window :

getWindow().add_widget(slider)

Add to another widget

You can create a layout to add multiple widget in. For example, create a grid layout, and add some button :

layout = MTBoxLayout(orientation='horizontal')
 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 :

getWindow().add_widget(layout)

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 = MTWidget()
@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 = MTButton()
@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 :

def my_onpress_callback(*largs):
  print 'My button is pressed, arguments is', largs
button = MTButton()
button.connect('on_press', my_onpress_callback)