对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
我在这里想念什么?
提前致谢。