Python的Kivy的ListView:如何删除选定ListItemButton? [英] Python Kivy ListView: How to delete selected ListItemButton?

查看:1550
本文介绍了Python的Kivy的ListView:如何删除选定ListItemButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过构建一个简单的待办事项列表应用程序像尘土飞扬菲利普斯书中Kivy创建应用一书的作者建议学习kivy。

I'm trying to learn kivy by building a simple todo-list app like suggested by Dusty Phillips, author of the book "Creating apps in Kivy".

这是code迄今:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from 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()

和这是千伏文件:

#: 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 is what it looks like:

我知道在ListView API还是有点实验和我抱怨使用的适配器/转换器的例子,谷歌和放大器;所以搜索没有帮助。因此$需要什么C $ C,使德尔按钮的工作和删除选定ListItemButton?

I know the ListView API is still somewhat experimental and I'm complaining about the examples on using adapters / converters, google & SO search didn't help either. So what code is needed to make the Del-Button work and remove a selected ListItemButton?

推荐答案

阅读大量的ListView API文档和之后;例子中,我终于找到了自己。我们需要的是listadapter级的选择 - 属性,那么我们就可以简单地调用adapter.data-的ListProperty的继承remove方法。

After a lot of reading ListView API docs & examples, I finally found out myself. What we need is the selection-Property of the listadapter-Class, then we can simply call the inherited remove method of the adapter.data-ListProperty.

因此​​,对于任何人interesested这是code:

So for anyone interesested this is the 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()

这篇关于Python的Kivy的ListView:如何删除选定ListItemButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆