【问题标题】:Increase kivy scrollview window size with increase widget entries通过增加小部件条目来增加 kivy 滚动视图窗口大小
【发布时间】:2015-05-07 21:23:12
【问题描述】:

我有以下代码可以生成行条目的滚动视图。

import ...

class DigestApp(App):
    def build(self):
        s = ScrollView()
        b = BoxLayout(orientation='vertical', size_hint=[1, 5])
        header = BoxLayout(orientation='horizontal')
        header1 = Label(text='#', size_hint=[.35, 1])
        header2 = Label(text='header', size_hint=[6, 1])
        checkAll = BoxLayout(orientation='horizontal')
        header3 = Label(text='Check All')
        header4 = CheckBox()
        checkAll.add_widget(header3)
        checkAll.add_widget(header4)
        header.add_widget(header1)
        header.add_widget(header2)
        header.add_widget(checkAll)
        b.add_widget(header)
        for i in range(100):
            b1 = BoxLayout(orientation='horizontal')
            n = Label(text=str(i+1), size_hint=[.35, 1])
            l = Button(text='> test header 01 02 03 04 050000000000000000000000000000000000', size_hint=[6, 1])
            c = CheckBox()
            b1.add_widget(n)
            b1.add_widget(l)
            b1.add_widget(c)
            b.add_widget(b1)
        s.add_widget(b)
        return(s)

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

产生以下输出(滚动查看所有 100 行):

这看起来很棒,只有 100 行(对于 i in range(100)),但是当我尝试添加 1000 行时,我得到以下结果。

需要注意的是,滚动功能是工作的,但显示显然是不可取的。

似乎即使我的滚动视图在 100 行中按预期工作,它也不能正确扩展到更大的数字。这是必要的,因为我可能需要一个有数万行的窗口。

我忽略了什么参数来解释这种缩放?

【问题讨论】:

    标签: python scrollview kivy


    【解决方案1】:

    要让 ScrollView 正确滚动 BoxLayout 的内容,bsize_hint_y 应该是 None,并且它的高度应该随着内容的添加而更新。例如,将b 定义为:

    b = BoxLayout(orientation='vertical', size_hint_y=None, height=0)
    

    在循环内部,根据要添加的内容的高度更新它的高度:

    for i in range(100):
        b1 = BoxLayout(orientation='horizontal', size_hint_y=None, height=40)
        (...)
        # Update b height
        b.height += b1.height
    
    s.add_widget(b)
    return(s)
    

    使用特定高度 40 定义 b1 是可选的,但它可以让您控制每列的​​大小。您可以保留默认值或更改为最适合您的高度。

    顺便说一句,我发现这种事情在 kv 语言中更容易控制,你可以简单地写:

    <b@BoxLayout:>
        size_hint_y: None
        height: sum(x.height for x in self.children)
        (...)
    

    b 的高度会随着其内容的高度自动更新。

    【讨论】:

    • 谢谢,这很有帮助。
    • 有没有办法调整滚动条的大小?它很小,很难点击。
    • 是的,有一个 bar_width 属性和其他一些可用于自定义的属性。 kivy.org/docs/api-kivy.uix.scrollview.html
    猜你喜欢
    • 1970-01-01
    • 2013-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多