代码之家  ›  专栏  ›  技术社区  ›  Zu Jiry

通过Kivy中的选定项目更改变量

  •  0
  • Zu Jiry  · 技术社区  · 6 年前

    所以我学习Kivy,但我被 ListView 或者,作为 列表视图 似乎有人不赞成 RecycleView .

    我的问题是我希望标签上有 species_text 只要标签在视图中,就可以根据我单击的项目更改ID。

    这些文件帮助我做出了决定 SelectableLabel 并且能够单击/为其着色,但我不知道如何更改 物种与文本 通过 回收回顾 或者如何将数据列表保存在 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()
    

    谢谢你的帮助!

    2 回复  |  直到 6 年前
        1
  •  1
  •   eyllanesc Raja    6 年前

    这个 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]))
    
        2
  •  0
  •   noEmbryo    6 年前

    选择SelectableLabel时,必须找到“物种文本”标签并更改其文本。 一种方法是通过“应用选择”方法。

        if is_selected:
            for screen in App.get_running_app().root.screens:
                if screen.name == "screen_test":
                    screen.ids.species_text.text = rv.data[index]["name"]
    

    注意:kv代码中有一个输入错误: “touch_multiselect”属性写为“touc_multiselect”