【问题标题】:Kivy: Scrollview not activating upon changing widget sizeKivy:滚动视图在更改小部件大小时未激活
【发布时间】:2019-08-14 03:39:32
【问题描述】:

我正在尝试在我的用户界面中使用滚动条。我的用户界面将能够从用户输入的文本中延伸出来。 但它的伸展方式不会导致 Scrollview “激活”。

此代码只是在 4 秒后增加小部件的大小来测试它。
首先它看起来像 然后看起来像 请注意,我们现在看不到最后一个列表项。但是滚动条应该在屏幕右侧可见,表明我们可以向下滚动查看它。但我们不能。 Scrollview 不知道内容已超出屏幕底部。

守则

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from lib.modules.adaptive_grid_layout import Adaptive_GridLayout

#This should have not enough content to scroll at first,
#but the size change should push some content past the border

Builder.load_string('''
<GrowingLabel>:
    padding: 10, 5
    size_hint_y: None
    text_size: self.width, None
    group: 'test'
    canvas.before:
        Color:
            rgba: .7, .7, .7, 1
        Rectangle:
            pos: self.pos
            size: self.size

<Controller>:
    layout_content: layout_content
    BoxLayout:
        id: bl
        orientation: 'vertical'
        padding: 10, 10
        row_default_height: '48dp'
        row_force_default: True
        spacing: 10, 10
        ScrollView:
            size: self.size
            GridLayout:
                id: layout_content
                size_hint_y: None
                cols: 1
                spacing: 0, 0
                padding: 0, 0
                Adaptive_GridLayout:
                    id: Row2
                    cols: 1
                    grow_rows: True
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    GrowingLabel:
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dkdsjahf lkasjkat"
                    Label:
                        height: 20
                        text: "Lorem ipsdodo dod dodo do dodt"
                    Label:
                        height: 20
                        text: "Lorem ipsdkjwww  ww woij ksdsdf sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Lorem ipsum dolor sit amet"
                    Label:
                        height: 20
                        text: "Last List item"



''')

class GrowingLabel(Label):
    def __init__(self, **kwargs):
        super(GrowingLabel, self).__init__(**kwargs)
        #self.size_hint_y = None
        self.height = 20
        Clock.schedule_once(lambda dt: self.changeHeight(120), timeout=4)

    def changeHeight(self, p_val):
        self.height = p_val

class Controller(FloatLayout):
    layout_content=ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Controller, self).__init__(**kwargs)
        self.layout_content.bind(minimum_height=self.layout_content.setter('height'))

class Nested2App(App):
    def build(self):
        return Controller()

if __name__ == '__main__':
    Nested2App().run()

注意:我正在使用一个名为 Adaptive_GridLayout 的自定义布局来处理缩放问题,您可以找到 here

我的问题
有没有办法手动触发滚动视图中滚动条的显示?或者有什么方法可以刷新滚动视图,以便它注意到它的内容有多大并做出适当的响应?

【问题讨论】:

  • 不熟悉Adaptive_GridLayout,但请尝试将size_hint: 1.0, Noneheight: self.minimum_height 添加到您的Adaptive_GridLayout 规则中。
  • 如果你想尝试一下,我在帖子中添加了指向 Adaptive_GridLayout 的链接。原 Adaptive_GridLayout 可以在here找到。

标签: python python-3.x kivy


【解决方案1】:

下载Adaptive_GridLayout 并在我上面的评论中进行更改后:

            Adaptive_GridLayout:
                id: Row2
                cols: 1
                grow_rows: True
                size_hint: 1.0, None
                height: self.minimum_height

ScrollViewGrowingLabel 更改其height 后工作。

【讨论】:

  • 这当然有效。看起来我将不得不大幅简化或替换Adaptive_GridLayout。我以为我必须更改 scrollview 实例的某些属性。
  • 我很好奇。声明size_hint: 1.0, None有什么意义?在Adaptive_GridLayout 中,size_hint_y 设置为None。但是为什么将size_hint_x 设置为 1.0 很重要?
  • 没有意义。 size_hint_x 默认为 1.0。所以size_hint_y: None 就足够了。
  • 此答案在具有多个列的 GridLayout 中不起作用。但它至少适用于我在这里给出的测试用例。 Kivy 很神秘。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-20
相关资源
最近更新 更多