Retrieve Data from Kivy Recycleview

↘锁芯ラ 提交于 2021-01-29 06:21:05

问题


I am able to make a recycle view layout of TextInput boxes. Now, further, I would like to give text input in these boxes and gather these inputs in the form of list or dict.

I have tried, self.ids.idname.data to append in an empty list but it didn't work.


回答1:


Following an example showing how to extract TextInput entries under a RecycleView and put that into a list:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ListProperty, NumericProperty, ObjectProperty
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.textinput import TextInput

APP_KV = """
#:import ScrollEffect kivy.effects.scroll.ScrollEffect
<DataView>:
    orientation: 'vertical'
    RecycleView:
        id: rv
        size_hint_y: 0.9
        viewclass: 'RecycleItem'
        key_size: 'size'
        effect_cls: ScrollEffect
        cols: 1
        RecycleBoxLayout:
            id: rvbox
            cols: rv.cols
            orientation: 'vertical'
            size_hint_y: None
            height: self.minimum_height
            default_size_hint: 1, None
    Button:
        text: 'Submit'
        size_hint_y: 0.1
        on_release: root.extract_data()

<RecycleItem>:
    on_text: self.parent.parent.data[self.index]['text'] = self.text
"""

class RecycleItem(RecycleDataViewBehavior, TextInput):
    index = NumericProperty(0)

    def refresh_view_attrs(self, rv, index, data):
        self.index = index
        return super(RecycleItem, self).refresh_view_attrs(rv, index, data)

class DataView(BoxLayout):
    DataList = ListProperty()
    TextInputNum = NumericProperty(10)
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        data = []
        for x in range(self.TextInputNum):
            data.append({'text': '', 'height': 50})
        self.ids.rv.data = data
    
    def extract_data(self):
        self.DataList.clear()
        for x in range(self.TextInputNum):
            self.DataList.append(self.ids.rv.data[x]['text'])
        print(self.DataList)

class MainApp(App):
    def build(self):
        self.root = Builder.load_string(APP_KV)
        return DataView()

if __name__ == '__main__':
    MainApp().run()


来源:https://stackoverflow.com/questions/63551028/retrieve-data-from-kivy-recycleview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!