如果我有一个可滚动的标签(在滚动视图内),当文本更新且太长时如何让它自动滚动到底部?

问题描述 投票:0回答:0

使用 python 和 kivy,我在 scrollView 中有一个可以垂直滚动的标签。滚动效果很好。内容是动态更新的。我想要以下行为:每次更新内容时,

  • 如果文本适合可见区域,则文本的开头与该区域的顶部对齐。
  • 如果文本太长放不下可见区域,python更新文本时,文本底部会自动对齐可见区域底部

我试过像下面的代码一样

self.ids['scroll_view'].scroll_y = 0
但它没有考虑任何约束。它总是滚动到文本的末尾。

在 .py 文件中:

class MyEngine(BoxLayout) :

    display = StringProperty("")

    def update_display(self) :

        #some stuff

        self.display = "some text"   # this string can be long or short

        # here (I suppose) I should measure something to scroll at the bottom iff the text is too long.
        # The above line doesn't care about the text size, it always scrolls at the bottom :

        self.ids['scroll_view'].scroll_y = 0

在 .kv 文件中:

<MyEngine>

    BoxLayout:
        orientation: 'vertical'
        size : root.width, root.height
        
        ScrollView:
            id:scroll_view
            size_hint: 1, 0.5
            Label:
                size_hint_y: None
                text_size: self.width, None
                height: self.texture_size[1]
                font_size:'24sp'
                halign: 'left'
                text:root.display

        GridLayout:
            size_hint:1, 0.5
            ...etc....
  

感谢您的任何建议。

python scroll kivy label
© www.soinside.com 2019 - 2024. All rights reserved.