[Python] Avoid having my custom button bound to multiple instances of that button when pressed

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    My example seems to pass self in the button object (based on printing my_id to debug and it matching the my_id for each button, but when I click on "Option A" all 3 buttons animate. When I click on "Option B", B and C animate.

    Why is the on_touch_down event trigging other buttons of the same RoundedButton class on the same page? (these are in a screen manager, not shown in code).

    <RoundedButton@Button>:
    style: "elevated"
    background_color: (0, 0, 0, 0)
    background_normal: ""
    background_down: ""

    canvas.before:
    Color:
    rgba: (0.72, 0.68, 0.92, 1) if self.state == 'normal' else (0.33, 0.48, 0.92, 1)
    RoundedRectangle:
    size: self.size
    pos: self.pos
    radius: [20]
    Color:
    rgba: (0.72, 0.68, 0.92, 1)
    Line:
    rounded_rectangle: (self.pos[0], self.pos[1], self.size[0], self.size[1], 20)
    width: 1 if self.state == 'normal' else 5


    ...


    MDBoxLayout:
    orientation: 'horizontal'

    RoundedButton:
    my_id: 'optionA'
    text: 'Option A'
    size_hint: 0.15, 0.35
    on_touch_down: app.animate_darken(self)

    RoundedButton:
    my_id: 'optionB'
    text: 'Option B'
    size_hint: 0.15, 0.35
    on_touch_down: app.animate_darken(self)

    RoundedButton:
    my_id: 'optionC'
    text: 'Option C'
    size_hint: 0.15, 0.35
    on_touch_down: app.animate_darken(self)


    And elsewhere, that animate_darken function:

    def animate_darken(self, widget, fade_length=0.5, *args):
    print(widget.my_id)
    BG_color = widget.background_color
    A = Animation(
    background_color=(0,0,0,1),
    duration=fade_length #seconds
    )
    A += Animation(x=0, y=0, duration=0.3)
    A += Animation(background_color=BG_color) # reset it
    A.start(widget)


    Q: How do I bind each RoundeButton only to that button, and animate it without affecting other buttons on the screen?

    Continue reading...

Compartilhe esta Página