1. <tfoot id='DbeB7'></tfoot>
    <i id='DbeB7'><tr id='DbeB7'><dt id='DbeB7'><q id='DbeB7'><span id='DbeB7'><b id='DbeB7'><form id='DbeB7'><ins id='DbeB7'></ins><ul id='DbeB7'></ul><sub id='DbeB7'></sub></form><legend id='DbeB7'></legend><bdo id='DbeB7'><pre id='DbeB7'><center id='DbeB7'></center></pre></bdo></b><th id='DbeB7'></th></span></q></dt></tr></i><div id='DbeB7'><tfoot id='DbeB7'></tfoot><dl id='DbeB7'><fieldset id='DbeB7'></fieldset></dl></div>

    • <bdo id='DbeB7'></bdo><ul id='DbeB7'></ul>
  2. <legend id='DbeB7'><style id='DbeB7'><dir id='DbeB7'><q id='DbeB7'></q></dir></style></legend>

      <small id='DbeB7'></small><noframes id='DbeB7'>

      Python Kivy ListView:如何删除选定的 ListItemButton?

      时间:2023-06-07
      • <bdo id='1cUPF'></bdo><ul id='1cUPF'></ul>

            <tbody id='1cUPF'></tbody>

            <i id='1cUPF'><tr id='1cUPF'><dt id='1cUPF'><q id='1cUPF'><span id='1cUPF'><b id='1cUPF'><form id='1cUPF'><ins id='1cUPF'></ins><ul id='1cUPF'></ul><sub id='1cUPF'></sub></form><legend id='1cUPF'></legend><bdo id='1cUPF'><pre id='1cUPF'><center id='1cUPF'></center></pre></bdo></b><th id='1cUPF'></th></span></q></dt></tr></i><div id='1cUPF'><tfoot id='1cUPF'></tfoot><dl id='1cUPF'><fieldset id='1cUPF'></fieldset></dl></div>

            <small id='1cUPF'></small><noframes id='1cUPF'>

              <legend id='1cUPF'><style id='1cUPF'><dir id='1cUPF'><q id='1cUPF'></q></dir></style></legend>
              • <tfoot id='1cUPF'></tfoot>
                本文介绍了Python Kivy ListView:如何删除选定的 ListItemButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试通过构建一个简单的待办事项列表应用程序来学习 kivy,就像在 Kivy 中创建应用程序"一书的作者 Dusty Phillips 所建议的那样.

                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".

                这是目前为止的代码:

                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()
                

                这是kv文件:

                #: 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)
                

                这是它的样子:

                我知道 ListView API 仍处于试验阶段,我抱怨有关使用适配器/转换器、google &所以搜索也没有帮助.那么需要什么代码才能使 Del-Button 工作并删除选定的 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 docs &例子,我终于找到了自己.我们需要的是listadapter-Class的selection-Property,那么我们可以简单的调用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.

                所以对于任何有兴趣的人来说,这是代码:

                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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Kivy'对象没有属性'错误 下一篇:在 Kivy 中显示一个 numpy 数组

                相关文章

                  <small id='Zr8uO'></small><noframes id='Zr8uO'>

                  <i id='Zr8uO'><tr id='Zr8uO'><dt id='Zr8uO'><q id='Zr8uO'><span id='Zr8uO'><b id='Zr8uO'><form id='Zr8uO'><ins id='Zr8uO'></ins><ul id='Zr8uO'></ul><sub id='Zr8uO'></sub></form><legend id='Zr8uO'></legend><bdo id='Zr8uO'><pre id='Zr8uO'><center id='Zr8uO'></center></pre></bdo></b><th id='Zr8uO'></th></span></q></dt></tr></i><div id='Zr8uO'><tfoot id='Zr8uO'></tfoot><dl id='Zr8uO'><fieldset id='Zr8uO'></fieldset></dl></div>
                  <tfoot id='Zr8uO'></tfoot>

                1. <legend id='Zr8uO'><style id='Zr8uO'><dir id='Zr8uO'><q id='Zr8uO'></q></dir></style></legend>
                  • <bdo id='Zr8uO'></bdo><ul id='Zr8uO'></ul>