Korte Alma
Korte Alma

Reputation: 168

How to add multiple widget at the same time to a scrollview?

I'm new at kivy, so I'm working on a test app. I want to create a screen with ScrollView and I want to add multiple things to a 'line' in the ScrollView, a text (description) and an image. I have tried this way:

class PresentUploadedData(Screen):
    container = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(PresentUploadedData, self).__init__(**kwargs)
        Clock.schedule_once(self.setup_scrollview, 1)

    def setup_scrollview(self, dt):
        self.container.bind(minimum_height=self.container.setter('height'))
        self.add_text_inputs()

    def add_text_inputs(self):
        for x in range(30):
            self.container.add_widget(Label(text="Label {}".format(x), size_hint_y=None, height=40, color= [128,0,0,1]))
            self.container.add_widget(Image(source='test.jpg', size_hint=(None, None,), width=50, height=50))

with this .kv file:

<PresentUploadedData>:
    name: "presentupload"

    message: send
    container: container

    canvas:
        Color:
            rgba: 1,1,1,1
        Rectangle:
            size: self.size
            pos: self.pos

    GridLayout:
        rows: 3
        cols: 1
        spacing: 5
        padding: 5
        font_name: "Calibri"
        background_color: 1,1,1, 1

        ScrollView:
            background_color: 1,1,1, 1
            size_hint: (1, .9)
            bar_width: 10
            bar_color: 128,0,0,0.7
            bar_inactive_color: 128,0,0,1 
            effect_cls: "ScrollEffect"
            scroll_type: ['bars', 'content']
            color: 128,0,0,1

            StackLayout:
                id: container
                size_hint_y: None
                color: 128,0,0,1

            GridLayout:
                cols: 2

        BoxLayout:
            spacing: 5
            size_hint: .7, .1

            Button:
                text: "< BACK"
                id: send
                color: 0, 0, 0, 1
                background_color: .88,.88,.88, 1
                size_hint: .2, 1
                on_release:
                    app.root.current = "main"
                    root.manager.transition.direction = "right"

But I've got ScrollView accept only one widget Exception. Why I got this, and how can I resolve this?

Upvotes: 2

Views: 837

Answers (1)

Korte Alma
Korte Alma

Reputation: 168

There is an error in .kvfile: GridLayout: cols: 2 This is useless.

The problem can be solved with one more for inside the for loop:

row = [Label(text="Label {}".format(x), size_hint_y=None, height=50, color= [128,0,0,1]), Image(source='test.jpg', size_hint=(None, None,), width=50, height=50)]
            for r in row:
                self.container.add_widget(r)

But this also didn't put the things near each other, just let them be there.

Upvotes: 1

Related Questions