带有boxlayout的kivy滚动视图

2022-01-15 00:00:00 python kivy

问题描述

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

I'm at the moment getting familiar with kivy. I think it has great potential, but I do find the relationship between "normal python" and the kv-language a bit confusing which makes it hard to understand where to do items. At the moment it appears to me that the behaviour (the things that happens behind the scenes) is not one to one when using python vs kv-l, in general I think that makes the bar quite high for usability/productivity.

我和其他人一起使用了速成课程",这是获得对 kivy 第一印象的良好开端.无论如何,在学习的过程中,我只是想看看我是否可以制作一个可滚动的 box-view - 结果我做不到.

I've amongst others used the "crash course" by inclement which is a great start to get a first impression of kivy. Anyway, in the process of learning I just wanted to see if I could make a box-view scrollable - it turned out that I could not.

需要什么才能使这段代码正常工作,即将标签扩展到它们的纹理大小",同时拥有一个可以调整的 ScrollView?

What's needed to make this code work, i.e. expand the labels to their "texture-size", and at the same time having a ScrollView that adjusts to that?

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

If the BoxLayout has a size_hint_y: None, the labels are not expanded to the text, but the scrollview can be seen in action when making the window really small.

如果 BoxLayout 有 size_hint_y: 1,则标签会展开,但显然 boxlayout 的高度根本没有改变,即滚动视图窗口似乎与 size_hint_y: None 相同

If the BoxLayout has a size_hint_y: 1, the labels are expanded, but apparantly the height of the boxlayout does not change at all, i.e. the scrollview window seems to be the same as with size_hint_y: None

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

If I just put in a height which is large, the scrollview covers this, but I would expect that it's possible to get a dynamic height of the boxlayout coupled to it's content.

我玩过高度、size_hints 等,但我没有找到有效的组合,有时会收到警告说由于内部重绘循环需​​要重新编写代码?

I've played around with heights, size_hints, etc. and I have not found a combination that works and sometimes get warnings that the code needs to be remade due to internal redrawing loops?

我错过/不理解什么?

代码如下.

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.

The BoxLayout is designed to make its children fill itself. A better layout for the dynamic resizing you want is the GridLayout, which has a minimum_height you can bind to for automatic resizing.

<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'
""")

相关文章