Search code examples
pythonoopkivygrid-layoutkivy-language

Dynamically add Grid Layouts to another layout created in .kv


I want to be able to add a row to my scrolling list from .py file. I wanted to create each row as an object so it could have data like "user_id" that it held but didn't display (also open to a different approach). Also, keep in mind that the scroll layout is created in the purge screen. Please help. I really need it.

From my screens.py:

class PurgeScreen(Screen):
    def backButton(self):
        SCREEN_MANAGER.current = 'dashboard'


class ListRow(PurgeScreen):
    def __init__(self, username, userid, profile_pic):
        self.username = username
        self.userid = userid
        self.profile = profile_pic

    def create_row(self):
        layout = GridLayout(row=1, row_force_default=True, row_default_height=40)
        layout.add_widget(Label(text=self.username))
        # Im going to add more stuff in the future
        self.ids.widget_list.add_widget(layout)

From my purge.kv:

<PurgeScreen>:
    name: 'purge'
    FloatLayout:
        size_hint: None, None
        ScrollView:
            size_hint: (None, None)
            do_scroll_x: False
            bar_width: 4
            bar_inactive_color: (.7, .7, .7, .4)
            size: (root.width * .75, root.height * .65)
            x: (root.width * .5) - (self.width/2)
            y: root.height * 0.2
            GridLayout:
                id: widget_list
                cols: 1
                size_hint_y: 2
                height: self.minimum_height

                Button:
                    text: "1"

                Button:
                    text: "2"

                Button:
                    text: "3"

                Button:
                    text: "4"

Solution

  • The code seems fine but

    1. When will the method create_row be called ?
    2. Do you have a widget (Button or something) which will trigger this method to add widgets to the GridLayout ?