【问题标题】:Label-Button is clickable outside of the texture标签按钮在纹理之外是可点击的
【发布时间】:2020-09-24 12:39:28
【问题描述】:

我创建了多个标签按钮。我给了每一个“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()


【问题讨论】:

    标签: python button kivy label


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的回复,但这对我不起作用
    • 抱歉,请查看更新后的答案。它应该与 and 结合使用,以便 root.to_2020() 函数嵌套在 collide_point 方法下。
    • 我以为我解决了问题,但我没有。您的解决方案有效。谢谢
    猜你喜欢
    • 2011-04-27
    • 2016-10-09
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多