Python Kivy ListView: How to delete selected ListItemButton? -
i'm trying learn kivy building simple todo-list app suggested dusty phillips, author of book "creating apps in kivy".
this code far:
from kivy.app import app kivy.uix.boxlayout import boxlayout kivy.properties import objectproperty kivy.uix.listview import listitembutton class taskbutton(listitembutton): pass class todoroot(boxlayout): task_input = objectproperty() task_list = objectproperty() def add_task(self): self.task_list.adapter.data.extend([self.task_input.text]) self.task_list._trigger_reset_populate() def del_task(self): pass class todoapp(app): def build(self): return todoroot() if __name__ == '__main__': todoapp().run()
and kv file:
#: import main todo #: import listadapter kivy.adapters.listadapter.listadapter #: import listitembutton kivy.uix.listview.listitembutton todoroot: <todoroot>: orientation: "vertical" task_input: task_input_view task_list: tasks_list_view boxlayout: size_hint_y: none height: "40dp" textinput: id: task_input_view size_hint_x: 70 button: text: "add" size_hint_x: 15 on_press: root.add_task() button: text: "del" size_hint_x: 15 on_press: root.del_task() listview: id: tasks_list_view adapter: listadapter(data=[], cls=main.taskbutton)
this looks like:
i know listview api still experimental , i'm complaining examples on using adapters / converters, google & search didn't either. code needed make del-button work , remove selected listitembutton?
after lot of reading listview api docs & examples, found out myself. need selection-property of listadapter-class, can call inherited remove method of adapter.data-listproperty.
so interesested code:
def del_task(self, *args): if self.task_list.adapter.selection: selection = self.task_list.adapter.selection[0].text self.task_list.adapter.data.remove(selection) self.task_list._trigger_reset_populate()
Comments
Post a Comment