如何在Kivy中将更改从一个ListView绑定到另一个ListView

时间:2015-05-01 08:16:28

标签: python listview kivy

我想要实现的是当我点击 BoxLayoutForEmails 中的条目时,它会更改并在 GroupListView 中显示相应的组

我试图通过id访问视图 GroupsListView ,以便动态地在其中实例化适配器,但无济于事。

所以我需要:以某种方式访问​​email_address_clicked()或找到另一种方法。

任何建议都非常感谢。

以下是代码:

class BoxLayoutForEmails(BoxLayout):
    def __init__(self, **kwargs):
        super(BoxLayoutForEmails, self).__init__(**kwargs)

        list_adapter = \
            ListAdapter(data=googleEmails,
                        selection_mode='single',
                        propagate_selection_to_data=False,
                        allow_empty_selection=False,
                        cls=ListItemButton)

        list_view = ListView(adapter=list_adapter)

        groups_view = ObjectProperty(None)

        list_adapter.bind(on_selection_change=groups_view.list_adapter.email_address_clicked)
        self.add_widget(list_view)


class GroupsListView(BoxLayout):
    def __init__(self, **kwargs):
        super(GroupsListView, self).__init__(**kwargs)

        self.list_adapter = GroupsListAdapter(data=["Group 1", "Group 2", "Group 3"],
                                              selection_mode='single',
                                              allow_empty_selection=False,
                                              cls=ListItemButton)

        self.list_view = ListView(adapter=self.list_adapter)
        self.list_adapter.bind()
        self.add_widget(self.list_view)

class GroupsListAdapter(ListAdapter):
    def email_address_clicked(self, email_address_adapter, *args):
        if len(email_address_adapter.selection) == 0:
            self.data = []
            return

        email = \
            email_address_adapter.selection[0].text

        if email == "me@mail.com":
            self.data = ["Good Group"]
        else:
            self.data = ["Hm... group"]

kv文件:

<MainPanel>:
    do_default_tab: False
    my_list: list
    groups_view: view

    TabbedPanelItem:
        text: 'Users'
        BoxLayout:
            BoxLayoutForEmails:
            ServicesPanel:
                do_default_tab: False
                TabbedPanelItem:
                    text: 'Google'
                    BoxLayout:
                        orientation: 'vertical'
                        Label:
                            text: "User Name"
                        GroupsListView:
                            id: view

1 个答案:

答案 0 :(得分:0)

BoxLayoutForEmails构造函数中还不存在

groups_view,所以不要直接访问它的属性。使用一个函数,只有在点击后才能访问它(它将在那时创建):

...
list_adapter.bind(on_selection_change=lambda *x: groups_view.list_adapter.email_address_clicked())
...

您可以使用lambda或class方法。