如何在kivy中的scrollview内部使用按钮行为?

时间:2019-12-19 11:58:56

标签: python kivy

我试图弄清楚如何使标签具有按钮的属性,而标签本身是可滚动的。 我尝试了几种不同的方法,但没有设法使其正常工作,这是我当前的代码,我的最终目标是将每个数字都作为一个单独的“可点击”实体,但是现在如果我能弄清楚如何使整个标签具有按钮的属性,就足够了。

我的代码:

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty
from kivy.clock import Clock
from kivy.properties import ListProperty
from kivy.properties import StringProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import ButtonBehavior
from kivy.uix.label import Label

class Testime(Widget):

    pass

class loendtest(App):
    tulemus = NumericProperty()
    loend = ListProperty()
    loend2 = StringProperty()
    loend3 = StringProperty()

    def build(self):

        return Testime()
if __name__ == "__main__":
    loendtest().run()
<Testime>:
    GridLayout:
        cols:3
        size: root.size
        Button:
            text: "add a result"
            on_press:
                app.tulemus += 1
                app.loend.append(app.tulemus)
                print(app.tulemus)
                print(app.loend)
                app.loend2 = " " + str(app.loend).strip('[]').replace(',', '\n')
                print(app.loend2.split())
                app.loend3 = " " + str(app.loend2.split()).strip('[]').replace(',', '\n')
                print(app.loend3)
        ScrollView:
            Label:
                size_hint_y: 2
                font_size: 75
                text_size: None, self.height
                valign: "top"

                text:
                    app.loend3

1 个答案:

答案 0 :(得分:2)

只需创建一个像Label一样的自定义Button

class MyButtonLabel(ButtonBehavior, Label):
    pass

...并用它替换您的Label ...

OR ,您可以在kv端进行操作,如下所示:

<MyButtonLabel@ButtonBehavior+Label>