标签按钮在纹理外是可点击的

Label-Button is clickable outside of the texture

我创建了多个标签按钮。我给每一个都赋予了“on_touch_up”属性,功能各不相同。但是当我单击一个标签时,所有函数都会被调用。在我看来,可点击区域比实际标签的纹理要大。 我试图将这些标签的大小设置为不同的参数,如“texture_size”或“font_size”,但没有成功。

感谢您的帮助


<YearLabel@ButtonBehavior+Label>

<YearScreen>:
    canvas.before:
        Color:
            rgba: 1,1,1,0.25
        Rectangle:
            pos: self.pos
            size: self.size


    canvas.after:
        Color:
            rgba : 0,0,0,1
        Rectangle:
            pos: 160,1565
            size: 419,60





    BoxLayout:
        id: years_layout
        orientation: "vertical"
        pos: 0,-2825
        spacing: 380

        YearLabel:
            text: "2020"
            font_size: "150sp"
            color: 1,1,1,1
            on_touch_up:
                root.to_2020()
        YearLabel:
            text: "2021"
            font_size: "150sp"
            color: 0,0,0,1
            on_touch_up:
                root.to_2021()
        YearLabel:
            text: "2022"
            font_size: "150sp"
            color: 0,0,0,1
            on_touch_up:
                root.to_2022()
        YearLabel:
            text: "2023"
            font_size: "150sp"
            color: 0,0,0,1
            on_touch_up:
                root.to_2023()
        YearLabel:
            text: "2024"
            font_size: "150sp"
            color: 0,0,0,1
            on_touch_up:
                root.to_2024()
        YearLabel:
            text: "2025"
            font_size: "150sp"
            color: 0,0,0,1
            on_touch_up:
                root.to_2025()
        YearLabel:
            text: "2026"
            font_size: "150sp"
            color: 0,0,0,1
            on_touch_up:
                root.to_2026()
        YearLabel:
            text: "2027"
            font_size: "150sp"
            color: 0,0,0,1
            on_touch_up:
                root.to_2027()
        YearLabel:
            text: "2028"
            font_size: "150sp"
            color: 0,0,0,1
            on_touch_up:
                root.to_2028()
        YearLabel:
            text: "2029"
            font_size: "150sp"
            color: 0,0,0,1
            on_touch_up:
                root.to_2029()
        YearLabel:
            text: "2030"
            font_size: "150sp"
            color: 0,0,0,1
            on_touch_up:
                root.to_2030()


在 Kivy 中,触摸事件会传播到所有 children。您需要使用 collide_point 将其限制为相应的小部件。下面是 on_touch_up 函数正确定义的示例:

            on_touch_up:
                self.collide_point(*args[1].pos) and root.to_2020()

同样,您可以为其余标签定义。