【问题标题】:Kivy RecycleView. Appending data from another classKivy RecycleView。附加来自另一个类的数据
【发布时间】:2019-09-01 02:35:08
【问题描述】:

我正在尝试从Newline 附加RV 的数据。我看到refresh_view_attrs 有效,但屏幕上没有显示任何内容。我该怎么做?

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.gridlayout import GridLayout
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.clock import Clock


Builder.load_string('''
<SelectableLabel>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size

    pos: self.pos
    size: self.size
    Label:
        id: id_label

<RV>:
    viewclass: 'SelectableLabel'

    SelectableRecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        multiselect: True
        touch_multiselect: True
''')

class SelectableRecycleBoxLayout(LayoutSelectionBehavior,
                                 RecycleBoxLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableLabel(RecycleDataViewBehavior, GridLayout):
    ''' Add selection support to the Label '''

    def refresh_view_attrs(self, rv, index, data):
        print('SelectableLabel', data)
        self.ids['id_label'].text = data['label']
        return super(SelectableLabel, self).refresh_view_attrs(
            rv, index, data)

class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = []

rv = RV()

class Newline(GridLayout):
    def __init__(self, **kwargs):
        Clock.schedule_once(self.add_line, 3)

    def add_line(self, dt):
        d = {'label': 'NEW LINE'}
        rv.data.append(d)

Newline()

class TestApp(App):
    def build(self):
        return RV()

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

【问题讨论】:

    标签: kivy


    【解决方案1】:
    rv = RV()
    
    class Newline(GridLayout):
        def __init__(self, **kwargs):
            Clock.schedule_once(self.add_line, 3)
    
        def add_line(self, dt):
            d = {'label': 'NEW LINE'}
            rv.data.append(d)
    
    Newline()
    
    class TestApp(App):
        def build(self):
            return RV()
    

    您的根小部件是RV(),而您将数据附加到不同的 RV()

    您想将数据附加到您实际显示的 RV:App.get_running_app().root.data.append(d) 可以工作。

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      相关资源
      最近更新 更多