Events in Kivy are actions or occurrences recognized by the software that may be handled by event listeners. Common events include user interactions like touches, clicks, key presses, and more.
To handle events in Kivy, you typically bind an event to a callback function. A callback function is a function that is called when an event occurs.
from kivy.app import App
from kivy.uix.button import Button
class MyApp(App):
def build(self):
button = Button(text='Click Me')
button.bind(on_press=self.on_button_press)
return button
def on_button_press(self, instance):
print('Button pressed!')
if __name__ == '__main__':
MyApp().run()
on_press : Fired when a button is pressed.
on_release :Fired when a button is released.
on_touch_down :Fired when a touch/click is initiated.
on_touch_up :Fired when a touch/click is ended.
on_touch_move :Fired when a touch/click is moved.
from kivy.app import App
from kivy.uix.label import Label
class TouchApp(App):
def build(self):
label = Label(text='Touch me!')
label.bind(on_touch_down=self.on_touch_down)
return label
def on_touch_down(self, instance, touch):
if instance.collide_point(*touch.pos):
print('Label touched at', touch.pos)
return super().on_touch_down(instance, touch)
if __name__ == '__main__':
TouchApp().run()