2

I'm trying to add a button on a widget and bind that button to a function. At the same time I have an on_touch_down event running.

However when I press button it doesn't call the function; instead it calls the on_touch_down event.

How can I fix this?

import kivy
kivy.require('1.0.8')

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button

class a(Widget):

    def on_touch_down(self, touch):
        print("touch x is ", touch.x)
        print("touch y is ", touch.y)

    def update(self,dt):
        print("updated")

class mainApp(App):

    def build(self):
        print("Hi, I am build function")
        parent = a()

        self.Startbtn = Button(text='Start')
        parent.add_widget(self.Startbtn)
        self.Startbtn.bind(on_release=self.Loop1)

         #Clock.schedule_interval(parent.update, 10.0/1000 )
        return parent

    def Loop1(self,dt):
        print("Hi, this is Loop function")
        self.v=1


if __name__ == '__main__':
    mainApp().run()

1 Answer 1

7

Try this:

class a(Widget):
    def on_touch_down(self, touch):
        print "touch x is ",touch.x
        print "touch y is ",touch.y
        # add this line:
        super(a, self).on_touch_down(touch) 

on_touch... methods execute first in touch event chain, overwriting it, you break this chain, so just add super() to continue. Hope it helped.

EDIT: Turns out, instead of super(), you can just return True to continue down the event tree

3
  • Thanks , Can you Explain what this line did . How did it break the chain ? Mar 25, 2014 at 9:31
  • 2
    Touches propagate down the widget tree, first being passed to the root widget and then having each widget pass the touch to its children. You overwrote this behaviour by redefining on_touch_down which is where this touch passing would normally occur.
    – inclement
    Mar 25, 2014 at 11:06
  • More you can read here: Architectural Overview
    – 1bit0fMe
    Mar 25, 2014 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.