gpt4 book ai didi

python - 如何将多列放入kivy RecycleView中?

转载 作者:太空宇宙 更新时间:2023-11-03 21:01:15 28 4
gpt4 key购买 nike

我想将(csv-)表的数据放入kivy recycleview中。

如果我为 kv 中的标签分配固定文本,我设法在一行中插入多列,但我无法让它用字典列表中的数据填充标签。这是到目前为止我用来测试这个概念的代码:

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 列中的 items 数据,但由于某种原因它们保持为空。

最佳答案

它是空的,因为数据未填充。

解决方案

  • 删除class Tabelle()中的所有编码
  • pass添加到class Tabelle()
  • 将以下内容添加到 class RV() 的构造函数 __init__()

片段

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.

示例

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()

输出

Result

关于python - 如何将多列放入kivy RecycleView中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55692120/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com