【问题标题】:Kivy recycleview duplicating data itemsKivy recycleview 重复数据项
【发布时间】:2020-08-08 09:20:10
【问题描述】:

我在 kivy 中有一个回收视图,我用一个包含文本和图像的列表写给它,文本很好,但是一旦我写了一个图像,它就会在我向下滚动时复制视图中的图像,请参阅下面的代码,我将如何阻止这种情况发生,这是我的 kv 文件我有一个包含两组数据的 boxlayout,还有一张问题的图片example

kv file:

 <SelectableReportTextbox@BoxLayout>:


    text: ''
    imagesource: ''
    color: ''


    Button:
            size_hint:  .8, 1
            text_size : self.text_size
            size_hint_y: None
            foreground_color: [1, 1, 1, 1]
            background_color: (0.2, 0.4, 0.8, 0.9) # root.color
            font_name: 'C:\kivy_venv\Graphics\GIL_____.TTF'
            font_size: self.height*0.2
            background_normal: root.imagesource
            text: root.text





  ScreenTwo:

    rv2: rv2


    canvas.before:


            Rectangle:
                    size:self.size           #100, 100
                    pos: self.pos
                    source: "C:\kivy_venv\Graphics\Jetfireback.png" 



    RecycleView_B:


            bar_width: 6
            size_hint: (None, None)
            do_scroll_y: True
            id: scrlv2
            size: (500, 500)
            pos_hint: {'center_x': .75, 'center_y': .64}
            multiline:True



            ProjectRV:


                    viewclass: 'SelectableReportTextbox'        #  
                    orientation: "horizontal"
                    scroll_type: ['bars', 'content']
                    scroll_wheel_distance: dp(114)
                    key_size: "height"

                    padding:1, 1
                    space_x: self.size[0]/3
                    id: rv2
                    pos_hint: {'center_x': 0.32, 'center_y': 0.525}
                    bar_width: dp(25)
                    bar_color: (0.7, 0.1, 0.3, 0.7)
                    bar_inactive_color: (0.1, 0.1, 0.1 , 1)
                    scroll_y : 0



                    SelectableRecycleBoxLayout:
                            rv2:rv2

                            spacing : '6'
                            default_size_hint: 1, None
                            size_hint_y: None
                            size_hint_x: 1

                            height: self.minimum_height
                            multiselect: True
                            touch_multiselect: True
                            orientation: 'vertical'

main.py:

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



 class SelectableReportTextbox(RecycleDataViewBehavior):
        selectable = BooleanProperty(True)
        index =None

        selected = BooleanProperty(False)
def __init__(self, **kw):
    super().__init__(**kw)
def refresh_view_attrs(self, rv, index, data):

    ''' Catch and handle the view changes '''
    self.index = index
    return super(SelectableRecycleBoxLayout, self).refresh_view_attrs(
        rv, index, data)


class ProjectRV(RecycleView):


def __init__(self, **kwargs):
    super(ProjectRV, self).__init__(**kwargs)




 class RecycleView_B(RecycleView):

      pass

  class ScreenTwo(Screen):

        TEXT_FILE = open("output.txt", "a")

        zdata = re.search('{.*,', strdata).group(0)   
        splitex = zdata.split(':' + "'")[1]

        print (zdata)

        TEXT_FILE.write(zdata)

        TEXT_FILE.close()

        with open("output.txt", "r") as f:

           txtdata = eval(str('[' + ''.join(f.readlines()) + ']'))#ast.literal_eval

           nstring = type(txtdata)

           self.rv2.data = txtdata
           self.rv2.refresh_from_viewport()
           self.rv2.refresh_from_data()

【问题讨论】:

  • 不看python代码就无法判断发生了什么。请发布minimal reproducible example。我在kv 中看到的一个问题是kv 文件顶部的SelectableReportTextbox@BoxLayout: 应该是一条规则(包含在&lt;&gt; 中)。
  • 我添加了我正在写入recycleview的python代码,基本上我只是从服务器收到一条消息转换为列表格式并写入文本文件,如果它是图像,它也将被写入相同列表中带有图像而不是文本的文件
  • 您的kv 定义了一个ScreenTwo,其中包含一个RecycleView (RecycleView_B),其中包含另一个RecycleView (ProjectRV)。这是你的意图吗?请发送print(txtdata) 并向我们展示结果。
  • 我尝试删除一个 recycleview 它对我打印的问题没有影响 txtdata 这是它的格式 [{'imagesource': 'contacts.png'}, {'text': 'test '}, {'text': 'test'}, {'text': 'test'}, {'text': 'test'},

标签: python-3.x kivy


【解决方案1】:

RecycleView 重复使用 viewclass 对象实例,并根据 data 字典设置这些实例的属性。因此,如果您的字典之一设置了 viewclsas 实例的 imagesource 属性,然后该实例被重新用于未设置 imagesource 属性的字典,则该实例将保留 @ 的先前值987654327@。解决方法是确保data 中的每个字典都包含viewclass 的每个属性的值。每个字典都需要包含 textimagesourcecolor 的值,即使值是 ''

【讨论】:

  • 感谢 John 说清楚,现在说得通了
猜你喜欢
  • 2019-01-01
  • 2021-10-15
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多