即使已定义类Kivy / recycle视图,也会出现未知类错误

问题描述 投票:-1回答:1

我的design.kv文件中有这个片段:

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

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

但是它返回一个错误:

Error message in kivy log

类的Track被定义为在上面的代码段和我的python代码中看到的。

我尝试将viewclass设置为'Button'并且它工作但是它只返回了一个不是预期行为的按钮。

我在这里弄错了什么?谢谢 :)

我的python和kivy文件的整个代码就在这里:https://github.com/Jezrianne/ANTS

以防万一错误不是来自上面的代码段:)

python python-3.x kivy kivy-language
1个回答
0
投票

Root Widget - Screen

以下示例说明了使用Screen小部件作为根小部件并与RecycleView小部件结合使用。

卖弄.朋友

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

GridLayout没有属性,orientation:。请从您的kv文件中删除它。

您需要实现以下内容:

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

卖弄.朋友

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

热门问题
推荐问题
最新问题