Widgets Basics
| Widgets | Using 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 :
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 name | Arguments | Description | Dispatched in children |
|---|---|---|---|
| on_touch_down() | touch | Fired when a touch is down | Yes |
| on_touch_move() | touch | Fired when a touch is moving | Yes |
| on_touch_up() | touch | Fired when a touch is up | Yes |
| on_update() | Fired to update the widget | Yes | |
| on_draw() | Fired to draw the widget | Yes | |
| on_resize() | w, h | Fired when the size change (except at creation) | No |
| on_move | x, y | Fired when the position change (except at creation) | No |
| on_parent_move | x, y | Fired 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 :
- The framework call on_update()
- The widget dispatch on_update() to his children
Draw the widget
Every frame, the widget is drawed in 3 steps:
- The function on_draw() is called by the framework to draw the widget
- The default on_draw() call draw() to draw the widget himself
- Then, the on_draw() is called on every children of the widget
