首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kivy中的RecycleView模块

Kivy中的RecycleView模块
EN

Stack Overflow用户
提问于 2018-01-16 17:55:37
回答 2查看 1.5K关注 0票数 0

新成员,第一个职位。我会尽量说得更具体和清楚。下面的代码摘自kivy关于RecycleView模块的网页。我想使用这段代码,但是,我不想使用KV和Builder,而是用纯Python3编写代码。我尝试将RecycleBoxLayout类作为一个小部件添加是完全失败的,因为结果只是一个黑色窗口。只有添加"viewclass“才有效。很明显,这里有些东西我不明白或错过了。我还附上了重写代码的尝试。

任何帮助都将不胜感激。谢谢,提前。

原始代码:

代码语言:javascript
复制
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView

Builder.load_string('''
<RV>:
    viewclass: 'Label'
    RecycleBoxLayout:
    default_size: None, dp(56)
    default_size_hint: 1, None
    size_hint_y: None
    height: self.minimum_height
    orientation: 'vertical'
''')

class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(100)]

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


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

My 失败尝试:

代码语言:javascript
复制
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.label import Label
from kivy.uix.recycleboxlayout import RecycleBoxLayout

class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(100)]
        self.viewclass = Label
        layout = RecycleBoxLayout()
        self.add_widget(layout)

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


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

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-17 10:17:54

我不是Kivy方面的专家,但我强烈建议您尽可能多地使用kv lang。它不过是一个简洁的python,它将使您的代码更加简洁。

如果您阅读了RecycleBoxLayout的文档,那么它具有很强的实验性,并且您应该利用它的工作原理。

尽管如此,您的代码看起来还不错,但是有一些问题。

1)将self.viewclass = Label替换为self.viewclass = 'Label'

2)您没有在python代码中为您的heightsizeorientation参数指定RecycleBoxLayout参数。

票数 0
EN

Stack Overflow用户

发布于 2018-03-09 00:58:27

我在寻找同样的东西,我不喜欢kv,并且还没有确信它比在代码中做任何事情都要好。

来自https://kivy.org/docs/api-kivy.uix.recycleview.html#kivy.uix.recycleview.RecycleView的示例将按以下方式创建。

代码语言:javascript
复制
from kivy.app import App
from kivy.graphics import Color, Rectangle
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                 RecycleBoxLayout):
    ''' Adds selection and focus behaviour to the view. '''
    def __init__(self, **kw):
        super().__init__(**kw, default_size=(0, 28), default_size_hint=(1, None), size_hint_y=None,
                         touch_multiselect=True, multiselect=True, orientation='vertical')

        self.bind(minimum_height=self._min)

    def _min(self, inst, val):
        self.height = val

class SelectableLabel(RecycleDataViewBehavior, Label):
    ''' Add selection support to the Label '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def __init__(self, **kw):
        super().__init__(**kw)
        self.canvas.before.clear()
        with self.canvas.before:
            if self.selected:
                Color(.0, 0.9, .1, .3)
            else:
                Color(0, 0, 0, 1)
            self.rect = Rectangle(size=self.size, pos=self.pos)
        self.bind(size=self._update_rect, pos=self._update_rect)

    def _update_rect(self, inst, value):
        self.rect.pos = inst.pos
        self.rect.size = inst.size

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

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableLabel, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected
        if is_selected:
            print("selection changed to {0}".format(rv.data[index]))
        else:
            print("selection removed for {0}".format(rv.data[index]))
        self.canvas.before.clear()
        with self.canvas.before:
            if self.selected:
                Color(.0, 0.9, .1, .3)
            else:
                Color(0, 0, 0, 1)
            self.rect = Rectangle(size=self.size, pos=self.pos)


class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.add_widget(SelectableRecycleBoxLayout())
        self.viewclass = 'SelectableLabel'
        self.data = [{'text': str(x)} for x in range(100)]


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

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

KV类中的顺序很重要。不确定需要在什么位置,但是在添加了小部件之后,需要修改self.data。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48287204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档