繁体   English   中英

与boxlayout的kivy scrollview

[英]kivy scrollview with boxlayout

我现在正熟悉kivy。 我认为它有很大的潜力,但我确实发现“普通python”和kv语言之间的关系有点令人困惑,这使得很难理解在哪里做项目。 目前在我看来,当使用python vs kv-l时,行为(幕后发生的事情)并不是一对一的,总的来说,我认为这对于可用性/生产力来说非常高。

我和其他人一起使用了“崩溃课程”,这是一个很好的开始,以获得kivy的第一印象。 无论如何,在学习的过程中,我只是想看看我是否可以制作一个可滚动的盒子视图 - 事实证明我不能。

使这个代码工作所需要的是什么,即将标签扩展到“纹理大小”,同时让ScrollView调整到那个?

如果BoxLayout具有size_hint_y:None,则标签不会扩展到文本,但是在使窗口非常小时可以看到滚动视图。

如果BoxLayout的size_hint_y为1,则标签会被展开,但显然boxlayout的高度根本不会改变,即scrollview窗口看起来与size_hint_y相同:None

如果我只是放入一个很大的高度,滚动视图会覆盖这个,但我希望可以获得与其内容耦合的boxlayout的动态高度。

我玩过高度,size_hints等等,我还没有找到一个有效的组合,有时会因为内部重绘循环而得到代码需要重新编写的警告?

我错过了什么/不理解?

代码如下。

from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView

Builder.load_string("""

<ScrollableLabel>:
    BoxLayout:
        orientation: 'vertical'
        # size_hint_y: 1
        size_hint_y: None
        height: 400 #self.size[1]
        canvas:
            Color:
                rgba: (1, 0, 0, .5) # DarkOliveGreen
            Rectangle:
                size: self.size
                pos: self.pos
        Label:
            id: bust
            text: 'a string that is long ' * 10
            font_size: 50
            text_size: self.width, None
            size_hint_y: None
            height: self.texture_size[1]
            canvas:
                Color:
                    rgba: (0, 1, 0, .5) # DarkOliveGreen
                Rectangle:
                    size: self.size
                    pos: self.pos
        Label:
            text: '2 strings that are long ' * 10
            text_size: self.width, None
            size_hint_y: None
            height: self.texture_size[1]
        Button:
            text: 'just testing'



""")

class ScrollableLabel(ScrollView):
    pass

runTouchApp(ScrollableLabel())

BoxLayout旨在让孩子们自己填充。 您想要的动态调整大小的更好布局是GridLayout,它具有您可以绑定的minimum_height以自动调整大小。

<ScrollableLabel>:
    GridLayout:
        cols: 1
        size_hint_y: None
        height: self.minimum_height
        canvas:
            Color:
                rgba: (1, 0, 0, .5) # DarkOliveGreen
            Rectangle:
                size: self.size
                pos: self.pos
        Label:
            id: bust
            text: 'a string that is long ' * 10
            font_size: 50
            text_size: self.width, None
            size_hint_y: None
            height: self.texture_size[1]
            canvas:
                Color:
                    rgba: (0, 1, 0, .5) # DarkOliveGreen
                Rectangle:
                    size: self.size
                    pos: self.pos
        Label:
            text: '2 strings that are long ' * 10
            text_size: self.width, None
            size_hint_y: None
            height: self.texture_size[1]
        Button:
            text: 'just testing'
""")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM