从Kivy Recycleview检索数据 [英] Retrieve Data from Kivy Recycleview

查看:109
本文介绍了从Kivy Recycleview检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够对TextInput框进行回收视图布局.现在,进一步,我想在这些框中输入文本,并以列表或字典的形式收集这些输入.

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.

我已经尝试过self.ids.idname.data将其追加到一个空列表中,但是没有用.

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

推荐答案

下面的示例演示如何提取RecycleView下的TextInput条目并将其放入列表中:

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

这篇关于从Kivy Recycleview检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆