Python 背后的逻辑';适配器';?(AttributeError:';非类型';对象没有属性';适配器';)

Python 背后的逻辑';适配器';?(AttributeError:';非类型';对象没有属性';适配器';),python,listview,kivy,Python,Listview,Kivy,我正在尝试实现一个代码,该代码用于返回在kivy中选择的项目的列表。 该代码作为独立的应用程序工作。但是我不知道如何在我自己的应用程序中一步一步地实现它(主要使用kv语言) 从kivy.uix.modalview导入modalview 从kivy.uix.listview导入listview 从kivy.uix.gridlayout导入gridlayout 从kivy.properties导入StringProperty 从kivy.properties导入ObjectProperty 从kiv

我正在尝试实现一个代码,该代码用于返回在kivy中选择的项目的列表。 该代码作为独立的应用程序工作。但是我不知道如何在我自己的应用程序中一步一步地实现它(主要使用kv语言)

从kivy.uix.modalview导入modalview
从kivy.uix.listview导入listview
从kivy.uix.gridlayout导入gridlayout
从kivy.properties导入StringProperty
从kivy.properties导入ObjectProperty
从kivy.lang导入生成器
来自kivy工厂进口工厂
#注意适配器声明中缩进的特殊性质,其中
#适配器:在一行上,则必须在一个级别上给出值端
#缩进量。
生成器。加载\u字符串(“”)
#:导入lv kivy.uix.listview
#:导入la kivy.adapters.listadapter
:
ListViewModal
:
列表视图:列表视图id
大小提示:无,无
尺码:400400
列表视图:
id:列表\视图\ id
大小提示:.8,.8
适配器:
莱斯特(
数据=[“项#{0}”。xrange(10)中i的格式(i),
选择_mode='single',
允许\u空\u选择=False,
cls=lv.ListItemButton)
""")
类ListViewModal(ModalView):
所选项目=StringProperty(“无选择”)
列表\视图=对象属性(无)
定义初始(自我,**kwargs):
超级(ListViewModal,self)。\uuuuu初始化(**kwargs)
self.list\u view.adapter.bind(on\u selection\u change=self.selection\u changed)
#这是用于实例化时设置的绑定到列表适配器的
#特殊的选择更改(绑定到它,而不是适配器.selection)。
def选择_已更改(自身,*参数):
选择更改时打印“args”以获取适配器,args
self.selected\u item=args[0]。选择[0]。文本
#这是为了说明另一种绑定类型。这一次是这样的
#类的selected_item StringProperty(其中设置了selected item文本)。
#请参阅如何在事物之间设置绑定的其他示例。这个
#之所以有效,是因为如果在Kivy属性名称前面加上uu,则会出现一个绑定
#是自动为您设置的。
所选项目(自身,*参数)上的def:
打印“列表属性更改时的args”可获取列表属性和更改的项,args
打印“所选项目文本”,参数[1]
类主视图(网格布局):
"""
使用kv语言实现ListView。
"""
#定义初始(自我,**kwargs):
#kwargs['cols']=1
#kwargs['size_hint']=(1.0,1.0)
#超级(主视图,自我)。\uuuuu初始化(**kwargs)
#listview_modal=ListViewModal()
#self.add\u小部件(listview\u模式)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
从kivy.base导入runTouchApp
从kivy.uix.modalview导入modalview运行TouchApp(MainView(宽度=800))
从kivy.uix.listview导入listview
从kivy.uix.gridlayout导入gridlayout
从kivy.properties导入StringProperty
从kivy.properties导入ObjectProperty
从kivy.lang导入生成器
来自kivy工厂进口工厂
#注意适配器声明中缩进的特殊性质,其中
#适配器:在一行上,则必须在一个级别上给出值端
#缩进量。
生成器。加载\u字符串(“”)
#:导入lv kivy.uix.listview
#:导入la kivy.adapters.listadapter
:
ListViewModal
:
列表视图:列表视图id
大小提示:无,无
尺码:400400
列表视图:
id:列表\视图\ id
大小提示:.8,.8
适配器:
莱斯特(
数据=[“项#{0}”。xrange(10)中i的格式(i),
选择_mode='single',
允许\u空\u选择=False,
cls=lv.ListItemButton)
""")
类ListViewModal(ModalView):
所选项目=StringProperty(“无选择”)
列表\视图=对象属性()
定义初始(自我,**kwargs):
超级(ListViewModal,self)。\uuuuu初始化(**kwargs)
self.list\u view.adapter.bind(on\u selection\u change=self.selection\u changed)
#这是用于实例化时设置的绑定到列表适配器的
#特殊的选择更改(绑定到它,而不是适配器.selection)。
def选择_已更改(自身,*参数):
选择更改时打印“args”以获取适配器,args
self.selected\u item=args[0]。选择[0]。文本
#这是为了说明另一种绑定类型。这一次是这样的
#类的selected_item StringProperty(其中设置了selected item文本)。
#请参阅如何在事物之间设置绑定的其他示例。这个
#之所以有效,是因为如果在Kivy属性名称前面加上uu,则会出现一个绑定
#是自动为您设置的。
所选项目(自身,*参数)上的def:
打印“列表属性更改时的args”可获取列表属性和更改的项,args
打印“所选项目文本”,参数[1]
类主视图(网格布局):
"""
使用kv语言实现ListView。
"""
通过
#定义初始(自我,**kwargs):
#kwargs['cols']=1
#kwargs['size_hint']=(1.0,1.0)
#超级(主视图,自我)。\uuuuu初始化(**kwargs)
#listview_modal=ListViewModal()
#self.add\u小部件(listview\u模式)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
从kivy.base导入runTouchApp
runTouchApp(主视图(宽度=800))
这给我带来了一个错误:

“AttributeError:'非类型'对象没有属性'adapter'”


我已经阅读了有关“适配器”的文档,但对我来说这是一个非常新的文档,我不明白为什么我需要一个适配器才能让列表返回结果?

ListView及其内容都是。那么我应该如何替换它呢?使用:)
from kivy.uix.modalview import ModalView
from kivy.uix.listview import ListView
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.factory import Factory

# Note the special nature of indentation in the adapter declaration, where
# the adapter: is on one line, then the value side must be given at one level
# of indentation.

Builder.load_string("""
#:import lv kivy.uix.listview
#:import la kivy.adapters.listadapter
<MainView>:
    ListViewModal

<ListViewModal>:
    list_view: list_view_id
    size_hint: None,None
    size: 400,400
    ListView:
        id: list_view_id
        size_hint: .8,.8
        adapter:
            la.ListAdapter(
            data=["Item #{0}".format(i) for i in xrange(10)],
            selection_mode='single',
            allow_empty_selection=False,
            cls=lv.ListItemButton)
""")

class ListViewModal(ModalView):
    selected_item = StringProperty('no selection')
    list_view = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(ListViewModal, self).__init__(**kwargs)
        self.list_view.adapter.bind(on_selection_change=self.selection_changed)

    # This is for the binding set up at instantiation, to the list adapter's
    # special on_selection_change (bind to it, not to adapter.selection).
    def selection_changed(self, *args):
        print '    args when selection changes gets you the adapter', args
        self.selected_item = args[0].selection[0].text

    # This is to illustrate another type of binding. This time it is to this
    # class's selected_item StringProperty (where the selected item text is set).
    # See other examples of how bindings are set up between things. This one
    # works because if you put on_ in front of a Kivy property name, a binding
    # is set up for you automatically.
    def on_selected_item(self, *args):
        print '    args when a list property changes gets you the list property, and the changed item', args
        print 'selected item text', args[1]

class MainView(GridLayout):
    """
    Implementation of a ListView using the kv language.
    """

    # def __init__(self, **kwargs):
        # kwargs['cols'] = 1
        # kwargs['size_hint'] = (1.0, 1.0)
        # super(MainView, self).__init__(**kwargs)

        # listview_modal = ListViewModal()
        # self.add_widget(listview_modal)

if __name__ == '__main__':
    from kivy.base import runTouchApp
    runTouchApp(MainView(width=800))from kivy.uix.modalview import ModalView
from kivy.uix.listview import ListView
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.factory import Factory

# Note the special nature of indentation in the adapter declaration, where
# the adapter: is on one line, then the value side must be given at one level
# of indentation.

Builder.load_string("""
#:import lv kivy.uix.listview
#:import la kivy.adapters.listadapter
<MainView>:
    ListViewModal

<ListViewModal>:
    list_view: list_view_id
    size_hint: None,None
    size: 400,400
    ListView:
        id: list_view_id
        size_hint: .8,.8
        adapter:
            la.ListAdapter(
            data=["Item #{0}".format(i) for i in xrange(10)],
            selection_mode='single',
            allow_empty_selection=False,
            cls=lv.ListItemButton)
""")

class ListViewModal(ModalView):
    selected_item = StringProperty('no selection')
    list_view = ObjectProperty()

    def __init__(self, **kwargs):
        super(ListViewModal, self).__init__(**kwargs)
        self.list_view.adapter.bind(on_selection_change=self.selection_changed)

    # This is for the binding set up at instantiation, to the list adapter's
    # special on_selection_change (bind to it, not to adapter.selection).
    def selection_changed(self, *args):
        print '    args when selection changes gets you the adapter', args
        self.selected_item = args[0].selection[0].text

    # This is to illustrate another type of binding. This time it is to this
    # class's selected_item StringProperty (where the selected item text is set).
    # See other examples of how bindings are set up between things. This one
    # works because if you put on_ in front of a Kivy property name, a binding
    # is set up for you automatically.
    def on_selected_item(self, *args):
        print '    args when a list property changes gets you the list property, and the changed item', args
        print 'selected item text', args[1]

class MainView(GridLayout):
    """
    Implementation of a ListView using the kv language.
    """
    pass
    # def __init__(self, **kwargs):
        # kwargs['cols'] = 1
        # kwargs['size_hint'] = (1.0, 1.0)
        # super(MainView, self).__init__(**kwargs)

        # listview_modal = ListViewModal()
        # self.add_widget(listview_modal)

if __name__ == '__main__':
    from kivy.base import runTouchApp
    runTouchApp(MainView(width=800))