Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Kivy文件中初始化ListAdapter_Python_Kivy_Listadapter - Fatal编程技术网

Python 在Kivy文件中初始化ListAdapter

Python 在Kivy文件中初始化ListAdapter,python,kivy,listadapter,Python,Kivy,Listadapter,我是Kivy新手,非常喜欢Kivy文件中代码和UI规范之间的分离。在理解如何在Kivy文件中定义ListAdapter时,我遇到了一个障碍。例如,下面是一个在Python代码中完全定义的ListAdapter: class TestPop(Popup): a = ['a', 'b', 'c', 'd', 'e', 'f','g'] def __init__(self, **kwargs): super(TestPop, self).__init__(**kwa

我是Kivy新手,非常喜欢Kivy文件中代码和UI规范之间的分离。在理解如何在Kivy文件中定义ListAdapter时,我遇到了一个障碍。例如,下面是一个在Python代码中完全定义的ListAdapter:

class TestPop(Popup):

    a = ['a', 'b', 'c', 'd', 'e', 'f','g']

    def __init__(self, **kwargs):
        super(TestPop, self).__init__(**kwargs)
        aAdapter = ListAdapter(data=self.a, cls=ListItemButton,
            args_converter = lambda row_index, x: {'text': x,'size_hint_y': None,
                'height': 50})
        aAdapter.bind(on_selection_change=self.click)
        myBox = BoxLayout(orientation = 'vertical')
        myBox.add_widget(ListView(size_hint = (1,0.8), adapter = aAdapter))
        myBox.add_widget(Label(text = 'my pop', size_hint = (1,0.2)))
        self.add_widget(myBox)
        self.open

    def click(self, *args):
        print('click:{}'.format(args[0].selection[0].text))
现在,通过将Python代码更改为:

class TestPop2(Popup):

    a = ['a', 'b', 'c', 'd', 'e', 'f','g']

    def __init__(self, **kwargs):
        self.aAdapter = ListAdapter(data=self.a, cls=ListItemButton,
            args_converter = lambda row_index, x: {'text': x,'size_hint_y': None,
                'height': 50})
        self.aAdapter.bind(on_selection_change=self.click)
        super(TestPop2, self).__init__(**kwargs)
        self.open

    def click(self, *args):
        print('click:{0}'.format(args[0].selection[0].text))
并创建一个Kivy文件:

<TestPop2>:
    title: 'type2 popup'
    BoxLayout:
        orientation: 'vertical'
        ListView:
            size_hint: (1,0.8)
            adapter: root.aAdapter
        Label:
            text: 'Test2 pop'
            size_hint: (1,0.2)
使用此文件:

<TestPop3>:
    title: 'type3 popup'
    BoxLayout:
        orientation: 'vertical'
        ListView:
            size_hint: (1,0.8)
            adapter: ListAdapter(data=root.a,cls=ListItemButton,args_converter= lambda row_i, x:{'text':x,'size_hint_y':None,'height':50},on_selection_change= root.click)
        Label:
            text: 'Test3 pop'
            size_hint: (1,0.2)           
:
标题:“type3弹出窗口”
盒子布局:
方向:“垂直”
列表视图:
尺寸提示:(1,0.8)
适配器:ListAdapter(data=root.a,cls=ListItemButton,args\u converter=lambda row\u i,x:{'text':x,'size\u hint\u y':无,'height':50},在\u selection\u change=root上单击)
标签:
文本:“test3pop”
尺寸提示:(1,0.2)
但是on_选择_更改不起作用。我假设在将函数绑定到事件的时间或方式上存在一些关键问题,并试图通过ObjectProperty将引用传递回Python代码,但我无法理解

Kivy ListAdapter文档位于,但我在那里找不到解决方案。一些优秀的代码示例位于,但我没有看到任何有助于解决此问题的示例

我也是Python新手,所以如果有人评论我的代码更像Python,或者用更准确的术语来描述这个问题,我会洗耳恭听

<TestPop3>:
    title: 'type3 popup'
    BoxLayout:
        orientation: 'vertical'
        ListView:
            size_hint: (1,0.8)
            adapter: ListAdapter(data=root.a,cls=ListItemButton,args_converter= lambda row_i, x:{'text':x,'size_hint_y':None,'height':50},on_selection_change= root.click)
        Label:
            text: 'Test3 pop'
            size_hint: (1,0.2)