Kivy:为什么ListView在.kv文件中声明不同?

对list_simple_in_kv.py进行一些改动,以使用ListAdapter代替SimpleListAdapter,我发现方法1起作用:


from kivy.uix.modalview import ModalView

from kivy.uix.listview import ListView

from kivy.uix.gridlayout import GridLayout

from kivy.lang import Builder

from kivy.app import App


#using 'sla'...whatever that means...

Builder.load_string("""

#:import ListItemButton kivy.uix.listview

#:import sla kivy.adapters.listadapter


<ListViewModal>:

    ListView:

        size_hint: .8, .8

        adapter:

            sla.ListAdapter(

            data=["Item #{0}".format(i) for i in range(100)],

            cls=ListItemButton.ListItemButton)

""")


class ListViewModal(ModalView):

    def __init__(self, **kwargs):

        super(ListViewModal, self).__init__(**kwargs)



class MainView(GridLayout):


    def __init__(self, **kwargs):

        kwargs['cols'] = 1

        super(MainView, self).__init__(**kwargs)


        listview_modal = ListViewModal()


        self.add_widget(listview_modal)


class TestApp(App):

    def build(self):

        root = MainView()

        return root


if __name__ == '__main__':

    TestApp().run()

但是将kv文件更改为这种方式(与我遇到的其他kv API最一致的方式)失败:


Builder.load_string("""

#:import ListItemButton kivy.uix.listview

#:import ListAdapter kivy.adapters.listadapter


<ListViewModal>:

    size_hint: None, None

    size: 400, 400

    ListView:

        size_hint: .8, .8

        ListAdapter:

            data: ["Item #{0}".format(i) for i in range(100)]

            cls: ListItemButton.ListItemButton

""")

我什至碰到了这篇文章,这篇文章也是如此,说方法2应该可以在master上工作,但是它却产生了一个异常:


File "C:\Python27\lib\site-packages\kivy\adapters\adapter.py", line 102, in __init__

     raise Exception('adapter: input must include data argument')

 Exception: adapter: input must include data argument

我在这里想念什么?


交互式爱情
浏览 237回答 3
3回答

MMMHUHU

ListAdapter不是窗口小部件,因此您无法使用尝试使用的语法。我认为,为使代码更具Kivyonic外观,您可以采取的最接近的措施如下。注意,我更改了导入。这看起来类似于向画布添加指令的方式:Builder.load_string("""#:import ListItemButton kivy.uix.listview.ListItemButton#:import ListAdapter kivy.adapters.listadapter.ListAdapter<ListViewModal>:&nbsp; &nbsp; size_hint: None, None&nbsp; &nbsp; size: 400, 400&nbsp; &nbsp; ListView:&nbsp; &nbsp; &nbsp; &nbsp; size_hint: .8, .8&nbsp; &nbsp; &nbsp; &nbsp; adapter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListAdapter(data=["Item #{0}".format(i) for i in range(100)], cls = ListItemButton)""")** 编辑 **您还更改了size_hint,因此先前的代码看起来与原始代码有很大不同。这看起来像是原始的。Builder.load_string("""#:import ListItemButton kivy.uix.listview.ListItemButton#:import ListAdapter kivy.adapters.listadapter.ListAdapter<ListViewModal>:&nbsp; &nbsp; ListView:&nbsp; &nbsp; &nbsp; &nbsp; size_hint: .8, .8&nbsp; &nbsp; &nbsp; &nbsp; adapter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListAdapter(data=["Item #{0}".format(i) for i in range(100)], cls = ListItemButton)""")

红糖糍粑

我不确定这个答案,但您没有在此adapter属性中添加任何内容。尝试这个:Builder.load_string("""#:import ListItemButton kivy.uix.listview#:import ListAdapter kivy.adapters.listadapter<ListViewModal>:&nbsp; &nbsp; size_hint: None, None&nbsp; &nbsp; size: 400, 400&nbsp; &nbsp; ListView:&nbsp; &nbsp; &nbsp; &nbsp; size_hint: .8, .8&nbsp; &nbsp; &nbsp; &nbsp; adapter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListAdapter.ListAdapter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: ["Item #{0}".format(i) for i in range(100)]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls: ListItemButton.ListItemButton""")我同意sla.ListAdapter不清楚。通常我们可以直接访问该类。告诉我这是否有效:Builder.load_string("""#:import ListItemButton kivy.uix.listview.ListItemButton#:import ListAdapter kivy.adapters.listadapter.ListAdapter<ListViewModal>:&nbsp; &nbsp; size_hint: None, None&nbsp; &nbsp; size: 400, 400&nbsp; &nbsp; ListView:&nbsp; &nbsp; &nbsp; &nbsp; size_hint: .8, .8&nbsp; &nbsp; &nbsp; &nbsp; adapter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListAdapter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: ["Item #{0}".format(i) for i in range(100)]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls: ListItemButton""")请注意,我更改了ListItemButton和的导入ListAdapter。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python