Kivy:更改小部件尺寸后,不会激活Scrollview

时间:2019-08-14 03:39:32

标签: python python-3.x kivy

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

此代码仅会在4秒后增加小部件的大小来进行测试。
首先看起来像 A user interface with all content visible on screen 然后看起来像 A user interface with content pushed past the bottom of the screen 注意,现在我们看不到最后一个列表项。但是滚动条应该在屏幕右侧可见,表明我们可以向下滚动以查看它。但是我们不能。 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的缩放问题。

我对您的问题
有什么方法可以手动触发在scrollview中显示滚动条吗?还是有某种方法可以刷新滚动视图,以便它可以注意到其内容有多大并做出适当的响应?

1 个答案:

答案 0 :(得分:1)

下载ERROR TypeError: Cannot create property '_meta' on number '195.759995' 并进行以上我的评论中建议的更改后:

Adaptive_GridLayout

Adaptive_GridLayout: id: Row2 cols: 1 grow_rows: True size_hint: 1.0, None height: self.minimum_height 更改了ScrollView后,GrowingLabel才能工作。