Widgets Basics

WidgetsUsing widgets

Introduction

A widget can be a Button, Slider, Label, Scatter... It can be what you want. A widget is the generic term to handle :

  • state of a object
  • updating the object
  • drawing the object
  • handling input

All our widgets are construct over our MTWidget. The MTWidget is handling :

  • position information (x, y)
  • size information (width, height)
  • css style (bg-color, draw-background...)
  • children tree (add_widget(), remove_widget())
  • event system (on_draw(), on_touch_down()...)

If you want to write your widget, you must be "MTWidget" compatible.

Position and Size

The position is available through theses properties :

  • x - return the X position
  • y - return the Y position
  • pos - return the (x, y) position

By default, position is (0, 0)

The size is available through theses properties :

  • width - return the widget width
  • height - return the widget height
  • size - return (width, height) size information

By default, size is (100, 100)

You can pass pos or size information when creating the widget like :

m1 = MTWidget(pos=(540, 230)) # create a widget at 540,230 position
m2 = MTWidget(size=(500, 500)) # create a widget with 500x500 size
m3 = MTWidget(pos=(100, 0), size=(50, 50)) # create a widget at (100,0) pos with (50,50) size

Events

You have multiple event available in the MTWidget :

Event nameArgumentsDescriptionDispatched in children
on_touch_down()touchFired when a touch is downYes
on_touch_move()touchFired when a touch is movingYes
on_touch_up()touchFired when a touch is upYes
on_update() Fired to update the widgetYes
on_draw() Fired to draw the widgetYes
on_resize()w, hFired when the size change (except at creation)No
on_movex, yFired when the position change (except at creation)No
on_parent_movex, yFired when the parent position change (except at creation)Yes

To understand how event really work, and how you can create your own event, check Events page.

In MTWidget, some events are dispatched into his children. This is not the case of all widgets, because some widget are handling event differently.

Update the widget

You can update the widget status when on_update() call. Every frame :

  1. The framework call on_update()
  2. The widget dispatch on_update() to his children

Draw the widget

Every frame, the widget is drawed in 3 steps:

  1. The function on_draw() is called by the framework to draw the widget
  2. The default on_draw() call draw() to draw the widget himself
  3. Then, the on_draw() is called on every children of the widget