Search code examples
pythonpython-3.xkivykivy-language

Unknown class error even if class is defined Kivy/recycle view


I have this snippet from my design.kv file:

<Track>:
    on_release:
        root.print_data(self.text)

RecycleView:
    viewclass: 'Track'
    RecycleGridLayout:
        cols: 1
        default_size_hint: 1, None
        orientation: 'vertical'

However it returns an error:

Error message in kivy log

The class 'Track was defined as seen in the snippet above as well as in my python code.

I tried setting the viewclass to 'Button' and it worked but it just returned a button which is not the intended behavior.

What am I getting wrong here? Thanks :)

The whole code of my python and kivy files are right here: https://github.com/Jezrianne/ANTS

Just in case the error does not originate from the snippet above :)


Solution

  • Root Widget - Screen

    The following example illustrates using Screen widget as the root widget and combined with RecycleView widget.

    main.py

    from kivy.app import App
    from kivy.uix.recycleview.views import RecycleDataViewBehavior
    from kivy.uix.recyclegridlayout import RecycleGridLayout
    from kivy.uix.behaviors import FocusBehavior
    from kivy.uix.recycleview.layout import LayoutSelectionBehavior
    from kivy.uix.button import Button
    from kivy.uix.screenmanager import Screen
    from kivy.lang import Builder
    
    
    Builder.load_string('''
    #:kivy 1.11.0
    
    <Track>:
        on_release:
            root.print_data(self.text)
    
    <RootWidget>:
        RecycleView:
            id: rv
            viewclass: 'Track'
            SelectableRecycleGridLayout:
                cols: 1
                default_size: None, dp(56)
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'
                multiselect: True
                touch_multiselect: True
    ''')
    
    
    class Track(RecycleDataViewBehavior, Button):
    
        def print_data(self, text):
            print("\nprint_data: text=", text)
    
    
    class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
                                      RecycleGridLayout):
        ''' Adds selection and focus behaviour to the view. '''
    
    
    class RootWidget(Screen):
        def __init__(self, **kwargs):
            super(RootWidget, self).__init__(**kwargs)
            self.ids.rv.data = [{'text': str(x)} for x in range(100)]
    
    
    class TestApp(App):
        def build(self):
            return RootWidget()
    
    
    if __name__ == '__main__':
        TestApp().run()
    

    Root Widget - RecycleView

    There is no attribute, orientation: for GridLayout. Please remove it from your kv file.

    You need to implement the following:

    Snippet

    class Track(RecycleDataViewBehavior, Button):
    
        def print_data(self, text):
            print("\nprint_data: text=", text)
    
    
    class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
                                      RecycleGridLayout):
        ''' Adds selection and focus behaviour to the view. '''
    

    Example

    main.py

    from kivy.app import App
    from kivy.uix.recycleview import RecycleView
    from kivy.uix.recycleview.views import RecycleDataViewBehavior
    from kivy.uix.recyclegridlayout import RecycleGridLayout
    from kivy.uix.behaviors import FocusBehavior
    from kivy.uix.recycleview.layout import LayoutSelectionBehavior
    from kivy.uix.button import Button
    
    
    class Track(RecycleDataViewBehavior, Button):
    
        def print_data(self, text):
            print("\nprint_data: text=", text)
    
    
    class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
                                      RecycleGridLayout):
        ''' Adds selection and focus behaviour to the view. '''
    
    
    class RV(RecycleView):
        def __init__(self, **kwargs):
            super(RV, self).__init__(**kwargs)
            self.data = [{'text': str(x)} for x in range(100)]
    
    
    class TestApp(App):
        def build(self):
            return RV()
    
    if __name__ == '__main__':
        TestApp().run()
    

    test.kv

    #:kivy 1.11.0
    
    <Track>:
        on_release:
            root.print_data(self.text)
    
    <RV>:
        viewclass: 'Track'
        SelectableRecycleGridLayout:
            cols: 1
            default_size: None, dp(56)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'
            multiselect: True
            touch_multiselect: True
    

    Output

    Img01