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

Python 通过Kivy中的选定项目更改变量,python,python-3.x,kivy,Python,Python 3.x,Kivy,因此,我学习Kivy,但我仍然坚持使用列表视图,或者,由于列表视图似乎已被弃用,因此回收视图 我的问题是,一旦标签在视图中,我希望带有species\u text作为ID的标签根据我单击的项目进行更改 文档帮助我制作了SelectableLabel并能够单击/着色,但我不知道如何通过RecycleView的数据列表更改species\u text的文本,或者如何将数据列表保存在ScreenTest类中 这是我的密码: from kivy.app import App from kivy.uix.

因此,我学习Kivy,但我仍然坚持使用
列表视图
,或者,由于
列表视图
似乎已被弃用,因此
回收视图

我的问题是,一旦标签在视图中,我希望带有
species\u text
作为ID的标签根据我单击的项目进行更改

文档帮助我制作了
SelectableLabel
并能够单击/着色,但我不知道如何通过
RecycleView
的数据列表更改
species\u text
的文本,或者如何将数据列表保存在
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()
从kivy.app导入应用
从kivy.uix.button导入标签,按钮
从kivy.lang导入生成器
从kivy.properties导入布尔属性
从kivy.uix.behaviors导入焦点行为
从kivy.uix.recycleboxlayout导入recycleboxlayout
从kivy.uix.recycleview.layout导入LayoutSelectionBehavior
从kivy.uix.recycleview.views导入RecycleDataViewBehavior
从kivy.uix.screenmanager导入screenmanager,屏幕
生成器。加载\u字符串(“”)
:
标签:
pos_提示:{“x”:.45,“top”:1}
正文:“标题”
大小提示:.1,.1
盒子布局:
pos_提示:{“x”:0.02,“top”:.8}
回收审查:
id:物种列表视图
数据:[{'name':“Species1”,“text”:“S1”},{'name':“Species2”,“text”:“S2”}]
viewclass:“SelectableLabel”
可选择的可循环利用布局:
默认大小:无,dp(56)
默认大小提示:1,无
尺寸提示:无
方向:“垂直”
多重选择:错误
触摸多选:错误
标签:
id:种类/文本
文本:“Speciestext”
盒子布局:
尺寸提示:无
身高:30
间距:10
画布:
颜色:
rgba:.5、.2、.1、1
矩形:
pos:self.pos
大小:self.size
按钮:
文本:“返回”
按钮:
正文:“下一个”
:
#绘制背景以指示选择
在以下情况之前:
颜色:
rgba:(.0,0.9,1,3)如果自选,则为其他(0,0,0,1)
矩形:
pos:self.pos
大小:self.size
""")
类SelectableRecycleBoxLayout(焦点行为、布局选择行为、,
可回收性布局):
通过
类SelectableLabel(RecycleDataViewBehavior,Label):
''将选择支持添加到标签''
索引=无
selected=布尔属性(False)
可选=布尔属性(真)
def刷新\视图\属性(自身、rv、索引、数据):
''捕获并处理视图更改''
self.index=索引
返回超级(SelectableLabel,self)。刷新\u视图\u属性(
rv、索引、数据)
def on_触控向下(自身,触控):
''在触地时添加选择''
如果是超级(SelectableLabel,self)。打开时触摸下(触摸):
返回真值
如果自碰撞点(*touch.pos)和自选择:
返回self.parent。使用触摸键选择(self.index,touch)
def应用选项(选择了自身、rv、索引):
''响应视图中的项目选择''
self.selected=是否选中
如果选择了以下选项:
打印(“选择更改为{0}”。格式(rv.data[index]))
其他:
打印(“删除了对{0}的选择”。格式(rv.data[index]))
类屏幕测试(屏幕):
通过
屏幕管理器=屏幕管理器()
screen\u manager.添加小部件(ScreenTest(name=“screen\u test”))
类TestApp(应用程序):
def生成(自):
返回屏幕管理器
test_app=TestApp()
测试应用程序运行()

谢谢你的帮助

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

注意:kv代码中有一个输入错误: “touch\u multiselect”属性写为“touc\u multiselect”

apply\u selection()

    ...
    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]))

非常感谢你!同样对于标签中的if子句,我不知道这样做是否有效
    ...
    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]))