Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Event Handling


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.


Basic Event Handling

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.


Example: Handling Button Press Event


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()

            

In this example:

Common Widget Events:

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.


Example: Handling Touch Events

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()

            



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML 5

Python

java

C++

C

JavaScript

Campus Learning

C

C#

java