by
354 16
6
8
5,047
15
Top 1% !
Popular
Famous
Pearl of Wisdom
Nice
Tagged
Specified
OpenSource
Popularity: 1588th place

Published on:

Languagepython
LicenseMIT_X11
Target Frameworkkivy

Touch down event binding in Kivy framework

This code shows how to handle a touch event in the python Kivy framework.
This snippet will generate a simple GUI, with 2 standard buttons and a custom button, where to bind the press event.
Any Kivy property has a default on_<property_name> event. 
This event is called when the value of the property is changed. In this case the on_touch_down(self, touch) method wraps such event.

Source: http://kivy.org/#home
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/36385/Touch-down-event-binding-in-Kivy-framewo?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.button import Button from kivy.uix.boxlayout import BoxLayout from kivy.properties import ListProperty class RootWidget(BoxLayout): def __init__(self, **kwargs): super(RootWidget, self).__init__(**kwargs) self.add_widget(Button(text='btn 1')) cb = CustomBtn() cb.bind(pressed=self.btn_pressed) self.add_widget(cb) self.add_widget(Button(text='btn 2')) def btn_pressed(self, instance, pos): print ('pos: printed from root widget: {pos}'.format(pos=pos)) class CustomBtn(Widget): pressed = ListProperty([0, 0]) def on_touch_down(self, touch): if self.collide_point(*touch.pos): self.pressed = touch.pos # we consumed the touch. return False here to propagate # the touch further to the children. return True return super(CustomBtn, self).on_touch_down(touch) def on_pressed(self, instance, pos): print ('pressed at {pos}'.format(pos=pos)) class TestApp(App): def build(self): return RootWidget() if __name__ == '__main__': TestApp().run()
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus