2012-04-18 19:12:50 +00:00
|
|
|
Events
|
2012-05-28 16:47:54 +00:00
|
|
|
------
|
2012-07-16 15:22:19 +00:00
|
|
|
|
2013-06-07 22:42:12 +00:00
|
|
|
Kivy is mostly `event-based <http://en.wikipedia.org/wiki/Event-driven_programming>`_, meaning the flow of the program is determined
|
2012-07-16 15:22:19 +00:00
|
|
|
by events.
|
|
|
|
|
2012-08-16 16:23:52 +00:00
|
|
|
**Clock events**
|
2012-07-16 15:22:19 +00:00
|
|
|
|
|
|
|
.. image:: ../images/gs-events-clock.png
|
|
|
|
:class: gs-eleft
|
|
|
|
|
|
|
|
The :doc:`/api-kivy.clock` allows you to schedule a function call in the
|
2014-03-11 08:32:16 +00:00
|
|
|
future as a one-time event with :meth:`~kivy.clock.ClockBase.schedule_once`,
|
2012-07-16 15:22:19 +00:00
|
|
|
or as a repetitive event with :meth:`~kivy.clock.ClockBase.schedule_interval`.
|
|
|
|
|
|
|
|
You can also create Triggered events with
|
2013-05-01 20:26:11 +00:00
|
|
|
:meth:`~kivy.clock.ClockBase.create_trigger`. Triggers have the advantage of
|
|
|
|
being called only once per frame, even if you have scheduled multiple triggers
|
|
|
|
for the same callback.
|
2012-07-16 15:22:19 +00:00
|
|
|
|
2012-08-16 16:23:52 +00:00
|
|
|
**Input events**
|
2012-07-16 15:22:19 +00:00
|
|
|
|
|
|
|
.. image:: ../images/gs-events-input.png
|
|
|
|
:class: gs-eleft
|
|
|
|
|
2013-05-01 20:26:11 +00:00
|
|
|
All the mouse click, touch and scroll wheel events are part of the
|
2012-07-16 15:22:19 +00:00
|
|
|
:class:`~kivy.input.motionevent.MotionEvent`, extended by
|
2013-05-01 20:26:11 +00:00
|
|
|
:doc:`/api-kivy.input.postproc` and dispatched through the `on_motion` event in
|
|
|
|
the :class:`~kivy.core.window.Window` class. This event then generates the
|
2012-07-16 15:22:19 +00:00
|
|
|
:meth:`~kivy.uix.widget.Widget.on_touch_down`,
|
2013-05-01 20:26:11 +00:00
|
|
|
:meth:`~kivy.uix.widget.Widget.on_touch_move` and
|
|
|
|
:meth:`~kivy.uix.widget.Widget.on_touch_up` events in the
|
|
|
|
:class:`~kivy.uix.widget.Widget`.
|
2012-07-16 15:22:19 +00:00
|
|
|
|
2013-05-01 20:30:44 +00:00
|
|
|
For an in-depth explanation, have a look at :doc:`/api-kivy.input`.
|
2012-07-16 15:22:19 +00:00
|
|
|
|
2012-08-16 16:23:52 +00:00
|
|
|
**Class events**
|
2012-07-16 15:22:19 +00:00
|
|
|
|
|
|
|
.. image:: ../images/gs-events-class.png
|
|
|
|
:class: gs-eleft
|
|
|
|
|
|
|
|
Our base class :class:`~kivy.event.EventDispatcher`, used by
|
2013-05-01 20:26:11 +00:00
|
|
|
:class:`~kivy.uix.widget.Widget`, uses the power of our
|
|
|
|
:doc:`/api-kivy.properties` for dispatching changes. This means when a widget
|
|
|
|
changes its position or size, the corresponding event is automatically fired.
|
2012-07-16 15:22:19 +00:00
|
|
|
|
2013-05-01 20:26:11 +00:00
|
|
|
In addition, you have the ability to create your own events using
|
2012-07-16 15:22:19 +00:00
|
|
|
:meth:`~kivy.event.EventDispatcher.register_event_type`, as the
|
2013-05-01 20:26:11 +00:00
|
|
|
`on_press` and `on_release` events in the :class:`~kivy.uix.button.Button`
|
2013-06-07 22:42:12 +00:00
|
|
|
widget demonstrate.
|
2012-07-16 15:22:19 +00:00
|
|
|
|
|
|
|
Another thing to note is that if you override an event, you become responsible
|
|
|
|
for implementing all its behaviour previously handled by the base class. The
|
2012-07-22 15:08:33 +00:00
|
|
|
easiest way to do this is to call `super()`::
|
2012-04-18 19:12:50 +00:00
|
|
|
|
|
|
|
def on_touch_down(self, touch):
|
2012-07-16 15:22:19 +00:00
|
|
|
if super(OurClassName, self).on_touch_down(touch):
|
2012-04-18 19:12:50 +00:00
|
|
|
return True
|
|
|
|
if not self.collide_point(touch.x, touch.y):
|
|
|
|
return False
|
2013-05-31 09:12:21 +00:00
|
|
|
print('you touched me!')
|
2012-07-16 15:22:19 +00:00
|
|
|
return True
|
2012-04-18 19:12:50 +00:00
|
|
|
|
2012-07-16 15:22:19 +00:00
|
|
|
Get more familiar with events by reading the :doc:`/guide/events` documentation.
|
2012-03-16 14:42:35 +00:00
|
|
|
|