Kivy-可编辑的回收视图



我正在尝试使用recclecleview窗口小部件创建一个可编辑的表,该表使用文本输入窗口小部件作为Kivy中的单个元素。通过查看网络上的示例和一些帖子,我能够编写一个从用户输入的表。

现在,我正在尝试获取用户编辑哪一行。我可以找到文本输入的on_text的事件,但这并不能提供有关行号的信息。另外,我尝试查看可用于回收利用的事件,但从中获得了太多帮助。

你们中的任何人都可以指导我走正确的道路。我是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()

我不知道是否建议以下内容,但是,在回收视图数据中添加一个框时,我将其传递给回收视图实例,以及创建框时i将其添加到回收视图列表中,以便您可以从每个框中控制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()

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

在用户单击TextInput时,将以下过度骑行功能添加到行类中,帮助我获得行索引。

class Row(BoxLayout, RecycleDataViewBehavior):
index = None
def refresh_view_attrs(self, rv, index, data):
    ''' Catch and handle the view changes '''
    self.index = index
    return super(Row, self).refresh_view_attrs(
        rv, index, data)
def on_touch_down(self, touch):
    if super(Row, self).on_touch_down(touch):
        return True
    if self.collide_point(*touch.pos):
        global rowIndex
        rowIndex = self.index

再次感谢您帮助我提出的建议。在其他人面临相同问题的情况下发布我的解决方案。

最新更新