我的design.kv文件中有以下代码段:
<Track>:
on_release:
root.print_data(self.text)
RecycleView:
viewclass: 'Track'
RecycleGridLayout:
cols: 1
default_size_hint: 1, None
orientation: 'vertical'
但是它返回一个错误:
在上面的代码段以及我的python代码中都定义了“ Track”类。
我尝试将viewclass设置为'Button',并且可以,但是它只是返回了一个按钮,这不是预期的行为。
我在这里出了什么问题? 谢谢:)
我的python和kivy文件的整个代码都在这里:https://github.com/Jezrianne/ANTS
以防万一错误不是由上面的片段引起的:)
答案 0 :(得分:0)
以下示例说明了将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()
没有属性,orientation:
用于GridLayout。请从您的kv文件中删除它。
您需要实现以下内容:
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. '''
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()
#: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