Python Kivy-使用on selection Recycleview分配变量

Python Kivy-使用on selection Recycleview分配变量,python,class,reference,kivy,recycle,Python,Class,Reference,Kivy,Recycle,我有一个问题,这可能是相当无知的python而不是kivy的问题 我不能用python编程kivy,所以我有KV文件和py文件 我用kivy文档中的SelectableLabel修改了Recycleview。然而,与所有其他小部件不同,这个小部件的工作方式不同 这是main.py #!/usr/bin/env python # -*- coding: utf-8 -*- from kivy.config import Config try: import kivy except I

我有一个问题,这可能是相当无知的python而不是kivy的问题

我不能用python编程kivy,所以我有KV文件和py文件

我用kivy文档中的SelectableLabel修改了Recycleview。然而,与所有其他小部件不同,这个小部件的工作方式不同

这是main.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.config import Config   
try:
    import kivy
except ImportError:
    raise ImportError("this backend requires Kivy to be installed.")


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

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

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


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
        self.change_this_variable = 'Here'
        """I wish to change this variable that belongs to MainWindow instance
        """
        if is_selected:
            print("selection changed to {0}".format(rv.data[index]))
        else:
            print("selection removed for {0}".format(rv.data[index]))



class MainWindow(BoxLayout):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.change_this_variable = '' # Variable I want to change from 
        #                                select_event

        #My whole app logic is here
        #
        #

class guitApp(App):
    pass

if __name__ == '__main__':
    guitApp().run()
这里是guit.kv

MainWindow:                                
    BoxLayout:

        RecycleView:
            id: rv
            data:[{'text': str(x)} for x in range(10)] # #
            viewclass: 'SelectableLabel'
            SelectableRecycleBoxLayout:
                id: slr
                default_size: None, dp(56)
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'
                multiselect: False
                touch_multiselect: True


<SelectableLabel>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (1, 0.866, .0, 1) if self.selected else (0, 0.419, 1,1)
        Rectangle:
            pos: self.pos
            size: self.size
main窗口:
盒子布局:
回收审查:
id:rv
数据:[{'text':str(x)}表示范围(10)]内的x#
viewclass:“SelectableLabel”
可选择的可循环利用布局:
单反
默认大小:无,dp(56)
默认大小提示:1,无
尺寸提示:无
高度:自身最小高度
方向:“垂直”
多重选择:错误
触摸多选:真
:
#绘制背景以指示选择
在以下情况之前:
颜色:
rgba:(1,0.866,.0,1)如果自行选择,则为其他(0,0.419,1,1)
矩形:
pos:self.pos
大小:self.size
我的问题是,我通常用kivy语言创建小部件,通过id引用或kivy lang中的on_event:root.function()在main.py中为其指定id并执行逻辑。但通过此SelecteableLabel和文档中的RecycleView,我在名为apply_selection的_event函数中指定了它,但它在Gui对象之外(主窗口,所有逻辑发生的地方)所以我无法访问它。我不想用全局函数解决这个问题。 所以我的问题是,如何在我的Gui对象中获得apply_selection,这样我就可以给MainWindow的变量赋值(比如self.change_this_variable)?

没有必要在MainWindow()中获得apply_selection()方法。解决方法是使用
App.get_running_App().root
来获取MainWindow()的实例您可以参照其属性和方法,如下所示:

片段 不需要在MainWindow()中获取apply_selection()方法。解决方案是使用
App.get_running_App().root
获取MainWindow()的实例,您可以引用其属性和方法,如下所示:

片段
def apply_selection(self, rv, index, is_selected):
    ''' Respond to the selection of items in the view. '''
    self.selected = is_selected

    App.get_running_app().root.change_this_variable = 'Here'

    """I wish to change this variable that belongs to MainWindow instance
    """

    if is_selected:
        print("selection changed to {0}".format(rv.data[index]))
    else:
        print("selection removed for {0}".format(rv.data[index]))