Kivy - 可编辑的RecycleView

时间:2017-12-04 09:29:30

标签: python kivy textinput

我正在尝试使用TextInput小部件作为Kivy中的单个元素,使用RecycleView小部件创建可编辑的表。通过查看Web上的示例和一些帖子,我能够编写一个从用户那里获取输入的表。

现在我正在尝试让用户编辑哪一行。我可以找到文本输入的on_text的事件,但是没有提供有关行号的信息。此外,我尝试查看可用于RecycleView的事件,但无法从中获得太多帮助。

你们中的任何人都可以引导我走上正确的道路。我对kivy很新。我附上了一个我正在研究的简单例子。

from kivy.app import App
from kivy.uix.recycleview import RecycleView
from kivy.lang import Builder


Builder.load_string('''
<Row@BoxLayout>:
    canvas.before:
        Color:
            rgba: 0.5, 0.5, 0.5, 1
        Rectangle:
            size: self.size
            pos: self.pos
    itemText: ''
    TextInput:
        id:CellText
        text:root.itemText


<RV>:
    id: rv
    viewclass: 'Row'
    scroll_type: ['bars', 'content']
    scroll_wheel_distance: dp(114)
    bar_width: dp(10)
    RecycleGridLayout:
        cols:3
        default_size: None, dp(30) 
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        spacing: dp(1)
''')


class RV(RecycleView):

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'itemText':'1'}, {'itemText':'John'}, {'itemText':'K'}, {'itemText':'2'}, {'itemText':'David'}, {'itemText':'P'}]


class rvTestApp(App):

    def build(self):
        return RV()

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

2 个答案:

答案 0 :(得分:0)

我不知道以下是否可以推荐,但是,当我在循环视图数据中添加一个框时,我将它传递给循环视图实例,当创建框时,我将其添加到列表中循环视图,这样你就可以控制每个框的rv,你可以控制rv中的每个框:

from kivy.app import App
from kivy.uix.recycleview import RecycleView
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import ObjectProperty, ListProperty
from kivy.clock import Clock

Builder.load_string('''
<Row>:
    canvas.before:
        Color:
            rgba: 0.5, 0.5, 0.5, 1
        Rectangle:
            size: self.size
            pos: self.pos
    itemText: ''
    TextInput:
        id:CellText
        text:root.itemText


<RV>:
    id: rv
    viewclass: 'Row'
    scroll_type: ['bars', 'content']
    scroll_wheel_distance: dp(114)
    bar_width: dp(10)
    RecycleGridLayout:
        cols:3
        default_size: None, dp(30) 
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        spacing: dp(1)
''')


class RV(RecycleView):
    list_items = ListProperty([])

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'itemText': '1', 'paren': self, 'index':0}, {'itemText': 'John', 'paren': self, 'index':1},
                 {'itemText': 'K', 'paren': self, 'index':2}, {'itemText': '2', 'paren': self, 'index':3},
                 {'itemText': 'David', 'paren': self, 'index':4}, {'itemText': 'P', 'paren': self, 'index':5}]

    def which_edit(self, *args):
        '''This will print the index of the box which is currently edited'''
        print args[0].parent.index 


class Row(BoxLayout):
    paren = ObjectProperty() #the instance of the rv

    def __init__(self, **kwargs):
        super(Row, self).__init__(**kwargs)
        Clock.schedule_once(self.update)

    def update(self, *args):
        self.paren.list_items.append(self)
        self.ids.CellText.bind(text=self.paren.which_edit)


class TestApp(App):

    def build(self):
        return RV()

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

答案 1 :(得分:0)

SP SP的答案有办法解决这个问题,但我找到了另一种方法(希望这是一个更好的方法)来获取行索引。

将以下覆盖函数添加到行类中有助于我在用户点击文本输入时获取行索引。

select id, lastname, monthly_sales_target
  from ex
 order by lastname, monthly_sales_target;

再次感谢您帮助我提出建议。如果其他任何人遇到同样的问题,请发布我的解决方案。