首页 > 解决方案 > 通过 Kivy 中的选定项目更改变量

问题描述

所以我学习了 Kivy,但我坚持使用ListView或者,因为ListView似乎已被弃用,RecycleView.

我的问题是,我希望species_textID 为标签的标签会根据我单击的项目进行更改,一旦标签可见。

该文档帮助我制作SelectableLabel并能够单击/着色,但我不知道如何species_text通过数据列表更改文本RecycleView或如何将数据列表保存在ScreenTest类中。

这是我的代码:

from kivy.app import App
from kivy.uix.button import Label, Button
from kivy.lang import Builder
from kivy.properties import BooleanProperty
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.screenmanager import ScreenManager, Screen

Builder.load_string("""

<ScreenTest>:
    Label:
        pos_hint: {"x": .45, "top": 1}
        text: "Headline"
        size_hint: .1, .1


    BoxLayout:
        pos_hint: {"x": 0.02, "top": .8}

        RecycleView:
            id: species_list_view
            data: [{'name': "Species1", "text": "S1"}, {'name': "Species2", "text": "S2"}]
            viewclass: 'SelectableLabel'

            SelectableRecycleBoxLayout:
                default_size: None, dp(56)
                default_size_hint: 1, None
                size_hint_y: None
                orientation: 'vertical'
                multiselect: False
                touch_multiselect: False

        Label:
            id: species_text
            text: "Speciestext"

    BoxLayout:
        size_hint_y: None
        height: 30
        spacing: 10

        canvas:
            Color:
                rgba: .5, .2, .1, 1
            Rectangle: 
                pos: self.pos
                size: self.size

        Button:
            text: "Go Back"

        Button:
            text: "Next"


<SelectableLabel>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size
""")


class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                 RecycleBoxLayout):
    pass


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

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableLabel, self).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]))


class ScreenTest(Screen):
    pass


screen_manager = ScreenManager()
screen_manager.add_widget(ScreenTest(name="screen_test"))


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


test_app = TestApp()
test_app.run()

谢谢你的帮助!

标签: pythonpython-3.xkivy

解决方案


apply_selection()方法有RecycleView参数,考虑到我们可以创建一个具有选定文本的属性,然后我们与 的文本进行绑定Label

    ...
    RecycleView:
        id: species_list_view
        data: [{'name': "Species1", "text": "S1"}, {'name': "Species2", "text": "S2"}]
        viewclass: 'SelectableLabel'
        text_selected: "" # create property

        SelectableRecycleBoxLayout:
            ...

    Label:
        id: species_text
        text: "Speciestext" if not species_list_view.text_selected else species_list_view.text_selected # apply binding
    ...

 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]))
        rv.text_selected  = rv.data[index]['text'] # set text
    else:
        print("selection removed for {0}".format(rv.data[index]))

推荐阅读