[Python] Kivy Button with on_touch_up defined calls the connected method twice

Discussão em 'Python' iniciado por Stack, Julho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I defined a Kivy Button and associated a method with on_touch_up. However, when I click the button, the method is called twice. This issue stops occurring if I also define on_touch_down and return True when touch.button == "left".

    Both on_touch_up and on_touch_down are filtered to handle only the left mouse button.

    What I did seems to solve the problem, but I'm not sure if it's the correct or recommended approach.

    I would like to know:


    • Is this the right way to handle this situation?


    • Is there a better or more idiomatic approach in Kivy?

    Here is the code:

    class MyButton(Button):
    def on_touch_up(self, touch):
    if self.collide_point(*touch.pos):
    if touch.button == 'left':
    self.left_released()
    return True
    return super().on_touch_up(touch)

    def on_touch_down(self, touch):
    # I tried this without knowing if it's the right way
    if self.collide_point(*touch.pos):
    if touch.button == 'left':
    return True
    return super().on_touch_down(touch)

    Continue reading...

Compartilhe esta Página