Search code examples
pythonlistlistviewkivykivy-language

Kivy: how to make a RecycleGridLayout with 2 cols:


I'm new to Python and Kivy, and I need to display a RecycleGridLayout with 2 cols where the first column gives me the position of the second column, for now it shows the list that I created in a single column

here's my .py file

class Introduccion(Screen):
    numbers = ObjectProperty()
    number_list = ObjectProperty([])
    def Add_ToList(self):
        self.number_list.data.append({'text': str(self.numbers.text)})

and my .kv file

BoxLayout:
    pos_hint:{'center_y': 0.3, 'center_x':0.4}
    size_hint: None, None
    size: 100, 250
    pos: 200, 100
    RecycleView:
        id: number_list_view
        viewclass: 'Label'
        RecycleGridLayout:
            cols: 1
            default_size: None, dp(26)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'

my other question is how do I add a button to clear the list, i tried with a button with the function self.number_list.data.clear() but only delete it with a row, if you have more than one, an error appears


Solution

  • You can use your former arrangement with RecycleBoxLayout to display a single column that looks like two columns. For example, a custom viewclass:

    class MyViewClass(BoxLayout):
        position = StringProperty('')
        text = StringProperty('')
    

    with a kv rule like:

    <MyViewClass>:
        size_hint_y: None
        height: self.minimum_height
        Label:
            text: root.position
            size_hint_y: None
            height: self.texture_size[1]
        Label:
            text: root.text
            size_hint_y: None
            height: self.texture_size[1]
    

    will display two adjacent Labels (like two columns).

    Then, in your RecycleBoxLayout, set the viewclass as:

        RecycleView:
            id: number_list_view
            viewclass: 'MyViewClass'
            RecycleBoxLayout:
            .
            .
            .
    

    Then, when you add data to the RecycleView, each dictionary in the data list would have two entries. For example:

    self.number_list.data.append({'position': 'this is position', 'text': text})
    

    As for clearing the entire RecycleView, you can just reset the entire data list, something like this:

            on_press: root.number_list.data = []