python - 如何在 kivy 中使用 ScrollView 内部的按钮行为?

标签 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

最佳答案

只需创建一个类似于按钮的自定义标签:

class MyButtonLabel(ButtonBehavior, Label):
    pass

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

或者,您可以在 kv 端执行此操作,如下所示:

<MyButtonLabel@ButtonBehavior+Label>

关于python - 如何在 kivy 中使用 ScrollView 内部的按钮行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59409354/

相关文章:

python - 在 Python 2.7 中,如何覆盖单个函数的字符串表示形式?

python - 为什么我的 ZenDesk 宏正在更新,但实际上没有任何变化?

python - Android 12 设备上的 Plyers GPS 模块存在问题

python - 一个简单示例中的无效属性名称

python - 无法从使用 PyInstaller 构建的 Kivy 应用程序中获取 lexers.PythonLexer()

python - 类型错误 : __init__() got an unexpected keyword argument 'scoring'

Python 迭代行和匹配值

python - 如何从pytorch中的AlexNet中提取fc7特征作为numpy数组?

python - android中带有opencv的kivy相机应用程序显示黑屏

python - 使用 pyinstaller 生成的基于 kivy 的 Windows exe 黑屏