Python Kivy:从其他文件访问类中的应用程序方法

Python Kivy:从其他文件访问类中的应用程序方法,python,kivy,Python,Kivy,我有四个不同的文件,例如,看起来像这样(这不是一个工作代码,但想法应该很清楚): main.py: from app import MyApp def main(): fmapp = MyApp() fmapp.run() if __name__ == '__main__': main() from kivy.app import App from SelectableLabel import SelectableLabel class MyApp(App):

我有四个不同的文件,例如,看起来像这样(这不是一个工作代码,但想法应该很清楚):

main.py

from app import MyApp

def main():
    fmapp = MyApp()
    fmapp.run()

if __name__ == '__main__':
    main()
from kivy.app import App
from SelectableLabel import SelectableLabel

class MyApp(App):
    def show_items(self):
        someList = ['1', 'someText']
        self.root.ids.someotherRecID.data = [{'text': str(x)} for x in someList]
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty


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(SelectableRecipeLabel, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableRecipeLabel, 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. '''
        global selection
        self.selected = is_selected
        if is_selected:
            selection = rv.data[index]['text']
            # MyApp().show_items() <-- How to call this to show items
app.py

from app import MyApp

def main():
    fmapp = MyApp()
    fmapp.run()

if __name__ == '__main__':
    main()
from kivy.app import App
from SelectableLabel import SelectableLabel

class MyApp(App):
    def show_items(self):
        someList = ['1', 'someText']
        self.root.ids.someotherRecID.data = [{'text': str(x)} for x in someList]
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty


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(SelectableRecipeLabel, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableRecipeLabel, 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. '''
        global selection
        self.selected = is_selected
        if is_selected:
            selection = rv.data[index]['text']
            # MyApp().show_items() <-- How to call this to show items
SelectableLabel.py

from app import MyApp

def main():
    fmapp = MyApp()
    fmapp.run()

if __name__ == '__main__':
    main()
from kivy.app import App
from SelectableLabel import SelectableLabel

class MyApp(App):
    def show_items(self):
        someList = ['1', 'someText']
        self.root.ids.someotherRecID.data = [{'text': str(x)} for x in someList]
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty


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(SelectableRecipeLabel, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableRecipeLabel, 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. '''
        global selection
        self.selected = is_selected
        if is_selected:
            selection = rv.data[index]['text']
            # MyApp().show_items() <-- How to call this to show items
基本上有一个RecycleView(
id:someotherRecID
)列表,当在另一个RecycleView(
id:recID
)中选择一个项目时,我希望更新该列表。当SelectableLabel类和MyApp类通过全局变量在一个文件中时,我可以很容易地做到这一点,但如何在单独的文件中做到这一点?有没有一个很好的方法来处理我想要完成的事情?提前谢谢

例如,这里的示例适用于我,但我想将其拆分:

allinoneapp.py:

from kivy.app import App
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty


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(SelectableRecipeLabel, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableRecipeLabel, 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. '''
        global selection
        self.selected = is_selected
        if is_selected:
            selection = rv.data[index]['text']
            fmapp.show_items() # <-- accessing method through global app object

class MyApp(App):
    def show_items(self):
        someList = ['1', 'someText']
        self.root.ids.someotherRecID.data = [{'text': str(x)} for x in someList]


if __name__ == '__main__':
    fmapp = MyApp()
    fmapp.run()
从kivy.app导入应用
从kivy.uix.recycleview.views导入RecycleDataViewBehavior
从kivy.uix.label导入标签
从kivy.properties导入布尔属性
类SelectableLabel(RecycleDataViewBehavior,Label):
''将选择支持添加到标签''
索引=无
selected=布尔属性(False)
可选=布尔属性(真)
def刷新\视图\属性(自身、rv、索引、数据):
''捕获并处理视图更改''
self.index=索引
返回超级(SelectableRecipeLabel,self)。刷新\u视图\u属性(rv,索引,数据)
def on_触控向下(自身,触控):
''在触地时添加选择''
如果超级(可选择RecipeLabel,self.)。打开触摸屏(触摸屏):
返回真值
如果自碰撞点(*touch.pos)和自选择:
返回self.parent。使用触摸键选择(self.index,touch)
def应用选项(选择了自身、rv、索引):
''响应视图中的项目选择''
全局选择
self.selected=是否选中
如果选择了以下选项:
selection=rv.data[索引]['text']
fmapp.show_items()#