如何在Kivy中使用ScrollView?

问题描述 投票:0回答:1

我想在屏幕的特定部分创建滚动视图。视图中内容的数量可以更改-因此,我想通过for循环将小部件插入视图中。

This is how the view is going to be我在这个问题上苦苦挣扎了一段时间。我似乎无法:

  1. 将小部件插入带有for循环的视图中
  2. 将视图放置在特定位置。

`

class PlaylistWidget(ScrollView):

    def __init__(self, **kwargs):
        super(PlaylistWidget, self).__init__(**kwargs)

        self.bar_width = 50
        self.size_hint = 1, .1
        self.scroll_type = ['bars']
        self.bar_inactive_color = 5, 20, 10, .5
        self.bar_color = 5, 10, 15, .8
        self.do_scroll_x = False
        self.do_scroll_y = True

        grid = GridLayout()
        grid.height = self.size[1]
        grid.size_hint_y = None
        grid.cols = 1
        grid.row_default_height = '20dp'
        grid.row_force_default = True

        for i in range(148):
            a = Label()
            a.text = "Blah blah blah"
            a.font_size = 30
            a.size_hint_y = None
            a.size_hint_x = 1.0
            a.height = self.size[1]

            grid.add_widget(a)

        self.add_widget(grid)

`这是我做过的尝试代码....

Thx。Ofek Harel。

python scroll kivy scrollview kivy-language
1个回答
0
投票

我想您所缺少的就是必须设置添加到Label的每个GridLayout的高度,并且还必须设置GridLayout的高度。这是执行此操作的代码的修改版本:

class PlaylistWidget(ScrollView):

    def __init__(self, **kwargs):
        super(PlaylistWidget, self).__init__(**kwargs)

        self.bar_width = 50
        self.size_hint = 1, .1
        self.scroll_type = ['bars']
        self.bar_inactive_color = 5, 20, 10, .5
        self.bar_color = 5, 10, 15, .8
        self.do_scroll_x = False
        self.do_scroll_y = True

        grid = GridLayout()

        # star with zero height
        grid.height = 0

        grid.size_hint_y = None
        grid.cols = 1
        grid.row_default_height = '20dp'
        grid.row_force_default = True

        for i in range(148):
            a = Label()
            a.text = "Blah blah blah"
            a.font_size = 30
            a.size_hint_y = None
            a.size_hint_x = 1.0

            # set label height
            a.height = grid.row_default_height

            # increment grid height
            grid.height += a.height

            grid.add_widget(a)

        self.add_widget(grid)

另一种方法是使用猕猴桃语言设置ScrollView。这种方法利用了minimum_height计算出的GridLayout的优势,因此您不必计算heightGridLayout

class PlaylistWidget(ScrollView):
    def add_labels(self, *args):
        grid = self.ids.grid
        for i in range(148):
            a = Label()
            a.text = "Blah blah blah"
            a.font_size = 30
            a.size_hint_y = None
            a.size_hint_x = 1.0

            # set label height
            a.height = grid.row_default_height

            grid.add_widget(a)


kv = '''
PlaylistWidget:
    bar_width: 50
    size_hint: 1, .1
    scroll_type: ['bars']
    bar_inactive_color: 5, 20, 10, .5
    bar_color: 5, 10, 15, .8
    do_scroll_x: False
    do_scroll_y: True
    GridLayout:
        id: grid
        cols: 1
        row_default_height: '20dp'
        row_force_default: True
        size_hint_y: None
        height: self.minimum_height
'''


class TestApp(App):
    def build(self):
        pl = Builder.load_string(kv)
        Clock.schedule_once(pl.add_labels)
        return pl

TestApp().run()
© www.soinside.com 2019 - 2024. All rights reserved.