Overview:
- Kivy dispatches any touch event on the app to the root widget class.
- A touch event could be any of the following
- Down event
- Up event
- Move event
- In Kivy, the root widget passes the event to all of its children while the children can pass the event to further down to their children.
- This way, it is not only the widget within whose physical boundaries the touch event occurred receive the event. Other widgets also receive the touch event.
- When a touch event occurs it is first received by one of the on_touch_xxx() handlers of the root widget.
- Once received an event can be digested by the Kivy widget meaning it need not be propagated to the children or it can be propagated to the children.
Example:
from kivy.uix.button import Button from kivy.app import App
# Customized button class AppButton(Button): # Handle touch - an up event def on_touch_down(self, touch): if self.collide_point(*touch.pos): self.text = "Touch down" # digest the event return False
# Handle touch - a down event def on_touch_up(self, touch): if self.collide_point(*touch.pos): self.text = "Touch me...." # touch is on the button...do not want to send this event any further return False
# App class class TouchDemo(App):
def build(self): appButton = AppButton(text = "Touch me...") return appButton
if __name__ == '__main__': TouchDemo().run() |