Skip to content Skip to sidebar Skip to footer

How To Put Multiple Columns Into A Kivy Recycleview?

I want to put the data of a (csv-)table into a kivy recycleview. I managed to insert multiple columns with one row, if i assign a fixed text to the Labels in the kv, but i can't g

Solution 1:

It is empty because data was not populated.

Solution

  • Remove all codings in class Tabelle()
  • Add pass into class Tabelle()
  • Add the following into constructor, __init__() of class RV()

Snippets

self.data = [{'spalte1_SP': str(x['SP1']), 'spalte2_SP': str(x['SP2']), 'spalte3_SP': str(x['SP3'])} for x in items]

Kivy RecycleView » data

The view is generatad by processing the data, essentially a list of dicts, and uses these dicts to generate instances of the viewclass as required.

data

The data used by the current view adapter. This is a list of dicts whose keys map to the corresponding property names of the viewclass.

data is an AliasProperty that gets and sets the data used to generate the views.

Example

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.boxlayout import BoxLayout

items = [{'SP1': 'Artikelnummer', 'SP2': 'Name', 'SP3': 'Groesse'},
         {'SP1': '510001', 'SP2': 'Big Pump', 'SP3': '1.50 L'},
         {'SP1': '523001', 'SP2': 'Leonie Still', 'SP3': '1.50 L'},
         {'SP1': '641301', 'SP2': 'Cola Mix', 'SP3': '1.50 L'}
         ]


classTabelle(BoxLayout):
    pass


Builder.load_string('''
<Tabelle>:
    orientation: 'horizontal'
    spalte1_SP: 'spalte1'
    spalte2_SP: 'spalte2'
    spalte3_SP: 'spalte3'
    Label:
        id: SP1
        text: root.spalte1_SP
    Label:
        id: SP2
        text: root.spalte2_SP
    Label:
        id: SP3
        text: root.spalte3_SP

<RV>:
    viewclass: 'Tabelle'
    RecycleBoxLayout:
        default_size: None, dp(20)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
''')


classRV(RecycleView):
    def__init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'spalte1_SP': str(x['SP1']), 'spalte2_SP': str(x['SP2']), 'spalte3_SP': str(x['SP3'])} for x in items]


classTestApp(App):
    defbuild(self):
        return RV()


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

Output

Result

Solution 2:

Based on the previous solution, here's a code whose interpretation is much easier. Additionally, one of the column is a checkbox.

Result: enter image description here

Python file:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout

# data
items = [{'number': '510001', 'name': 'Big Pump', 'size': '1.50 L', 'in_stock': True},
         {'number': '523001', 'name': 'Leonie Still', 'size': '1.60 L', 'in_stock': False},
         {'number': '641301', 'name': 'Apple Mix', 'size': '1.30 L', 'in_stock': True},
         {'number': '681301', 'name': 'Orange Mix', 'size': '1.40 L', 'in_stock': True}
        ]


classMultiFieldLine(BoxLayout):
    # class layout defined in kv filepassclassAppGUI(GridLayout):
    def__init__(self, **kwargs):
        super().__init__(**kwargs)
        self.rv.data = [{'label_1': str(x['number']), 'label_2': str(x['name']), 'label_3': str(x['size']), 'checkbox_1': x['in_stock']} for x in items]


classExploreRecycleViewMultipleFieldApp(App):
    defbuild(self):
        return AppGUI()


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

KV file:

<MultiFieldLine>:orientation:'horizontal'label_1:''label_2:''label_3:''checkbox_1:FalseLabel:text:root.label_1Label:text:root.label_2Label:text:root.label_3CheckBox:active:root.checkbox_1<AppGUI>:# inherit from GridLayoutrv:rv_idcols:1rows:2GridLayout:# col titlescols:4rows:1size_hint_y:0.04Label:text:'Number'Label:text:'Name'Label:text:'Size'Labeltext:'In stock'GridLayout:# datacols:1rows:1size_hint_y:0.96RecycleView:id:rv_idviewclass:'MultiFieldLine'RecycleBoxLayout:default_size:None,dp(20)default_size_hint:1,Nonesize_hint_y:Noneheight:self.minimum_heightorientation:'vertical'

The version below adds row selection as well as a larger label width for the name column and a method in the gui class called each time a line is selected.:

Result: enter image description here

Python file:

from kivy.app import App
from kivy.properties import BooleanProperty
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout

# datafrom kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior

items = [{'number': '510001', 'name': 'Big Pump', 'size': '1.50 L', 'in_stock': True},
         {'number': '523001', 'name': 'Leonie Still very, very,very long, long name', 'size': '1.60 L', 'in_stock': False},
         {'number': '641301', 'name': 'Apple Mix', 'size': '1.30 L', 'in_stock': True},
         {'number': '681301', 'name': 'Orange Mix', 'size': '1.40 L', 'in_stock': True}
        ]


classMultiFieldLine(RecycleDataViewBehavior, BoxLayout):
    ''' class layout defined in kv file '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)
    
    defrefresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.rv = rv
        self.appGUI = rv.appGUI
        self.index = index
        
        returnsuper(MultiFieldLine, self).refresh_view_attrs(
            rv, index, data)
    
    defon_touch_down(self, touch):
        ''' Add selection on touch down '''ifsuper(MultiFieldLine, self).on_touch_down(touch):
            returnTrueif self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)
    
    defapply_selection(self, rv, index, is_selected):
        # instance variable used in .kv file to change the selected item# color !
        self.selected = is_selected
  
        if is_selected:
            self.appGUI.outputSelectedData(rv.data[index])


classSelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                 RecycleBoxLayout):
    ''' Adds selection and focus behaviour to the view. '''# required to authorise unselecting a selected item
    touch_deselect_last = BooleanProperty(True)


classAppGUI(GridLayout):
    def__init__(self, **kwargs):
        super().__init__(**kwargs)
        self.rv.data = [{'label_1': str(x['number']), 'label_2': str(x['name']), 'label_3': str(x['size']), 'checkbox_1': x['in_stock']} for x in items]

    defoutputSelectedData(self, data):
    # method called when a line is selectedprint(data)

classExploreRecycleViewMultipleFieldBoxLayoutApp(App):
    defbuild(self):
        return AppGUI()


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

KV file:

<MultiFieldLine>:# inherit from RecycleDataViewBehavior and BoxLayoutorientation:'horizontal'label_1:''label_2:''label_3:''checkbox_1:False# add selection backgroundcanvas.before:Color:rgba:(1,0,0,1)ifself.selectedelse(.0,0.9,.1,.3)Rectangle:pos:self.possize:self.sizeColor:rgba:(0,0.9,.1,.3)Label:size_hint_x:0.1text:root.label_1Label:size_hint_x:0.7text:root.label_2Label:size_hint_x:0.1text:root.label_3CheckBox:size_hint_x:0.1active:root.checkbox_1<AppGUI>:# inherit from GridLayoutrv:rv_idcols:1rows:2GridLayout:# col titlescols:4rows:1size_hint_y:0.04Label:size_hint_x:0.1text:'Number'Label:size_hint_x:0.7text:'Name'Label:size_hint_x:0.1text:'Size'Labelsize_hint_x:0.1text:'In stock'GridLayout:# datacols:1rows:1size_hint_y:0.96RecycleView:id:rv_idappGUI:rootviewclass:'MultiFieldLine'SelectableRecycleBoxLayout:default_size:None,dp(20)default_size_hint:1,Nonesize_hint_y:Noneheight:self.minimum_heightorientation:'vertical'

Post a Comment for "How To Put Multiple Columns Into A Kivy Recycleview?"