如果我在.kv文件中初始化一个空的ListView,
<Panel>:
do_default_tab: False
size_hint_y: 0.7
TabbedPanelItem:
id: tab_1
text: 'VIEW'
ListView:
id: VIEWlist
adapter:
ListAdapter(
data=[],
args_converter = root.args_converter,
selection_mode='multiple',
cls=ListItemButton
)
如何从.py端向其添加数据?
答案 0 :(得分:0)
您需要访问VIEWlist.adapter.data
属性。一个例子:
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder
Builder.load_string('''
#:import ListItemButton kivy.uix.listview.ListItemButton
#:import ListAdapter kivy.adapters.listadapter.ListAdapter
<Panel>:
size_hint: .5, .5
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
id: tab_1
text: 'VIEW'
BoxLayout:
orientation: 'vertical'
Button:
text: 'add'
on_press: root.add()
ListView:
id: VIEWlist
adapter:
ListAdapter(
data=[],
selection_mode='multiple',
cls=ListItemButton)
''')
class Panel(TabbedPanel):
def add(self, *args):
self.ids['VIEWlist'].adapter.data.append('txt')
class TabbedPanelApp(App):
def build(self):
return Panel()
if __name__ == '__main__':
TabbedPanelApp().run()