如何将多列放入奇异的RecycleView中? [英] How to put multiple columns into a kivy RecycleView?
问题描述
我想将(csv-)表的数据放入kivy recycleview.
I want to put the data of a (csv-)table into a kivy recycleview.
如果我为kv中的标签分配了固定的文本,我设法在一行中插入多列,但是我无法用字典列表中的数据填充标签.到目前为止,这是我用来测试概念的代码:
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 get it to fill the labels with data from a dictionary list. This is the code so far, that i use to test the concept:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.boxlayout import BoxLayout
import csv
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'}
]
class Tabelle(BoxLayout):
def __init__(self, **kwargs):
super(Tabelle, self).__init__(**kwargs)
def insert_SP(self, data):
for i in data:
self.spalte1_SP = i['SP1']
#print(self.spalte1_SP)
self.spalte2_SP = i['SP2']
self.spalte3_SP = i['SP3']
Builder.load_string('''
<Tabelle>:
orientation: 'horizontal'
spalte1_SP: 'spalte1'
spalte2_SP: 'spalte2'
spalte3_SP: 'spalte3'
Label:
id: Spalte1
text: root.spalte1_SP
Label:
id: Spalte2
text: root.spalte2_SP
Label:
id: Spalte3
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'
''')
class RV(RecycleView):
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
#self.data = []
x = Tabelle()
x.insert_SP(items)
class TestApp(App):
def build(self):
return RV()
if __name__ == '__main__':
TestApp().run()
我希望在3列中看到项中的数据,但是由于某些原因它们仍然为空.
I expect to see the data from items in 3 columns, but they stay empty for some reason.
推荐答案
它为空,因为未填充data
.
- 删除
class Tabelle()
中的所有编码
- 将
pass
添加到class Tabelle()
- 将以下内容添加到构造函数中,即
class RV()
的__init__()
- Remove all codings in
class Tabelle()
- Add
pass
intoclass Tabelle()
- Add the following into constructor,
__init__()
ofclass RV()
self.data = [{'spalte1_SP': str(x['SP1']), 'spalte2_SP': str(x['SP2']), 'spalte3_SP': str(x['SP3'])} for x in items]
Kivy RecycleView»数据
通过处理数据,本质上是一个列表 的dict,并使用这些dict生成viewclass的实例 根据需要.
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是AliasProperty,它获取并设置用于生成的数据 视图.
data is an AliasProperty that gets and sets the data used to generate the views.
示例
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'}
]
class Tabelle(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'
''')
class RV(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]
class TestApp(App):
def build(self):
return RV()
if __name__ == '__main__':
TestApp().run()
输出
这篇关于如何将多列放入奇异的RecycleView中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!