首页 > 解决方案 > 提交和删除按钮在 Python 和 Kivy (ListItemButton) 中不起作用

问题描述

我目前在列表项按钮方面遇到问题并使其正常工作。

关键是当我使用 Kivy 和 Python 文件时它运行良好。但是当使用函数生成器将我的 Kivy 文件放入 Python 时 - 它会运行程序,但如果我按下“提交”或“删除”按钮,程序就会退出。这是Python中的部分代码以及错误描述。任何帮助,将不胜感激

#####################################################################
import kivy

kivy.require('1.9.0')

from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.listview import ListItemButton
from kivy.lang.builder import Builder

class BOXlayOUT(BoxLayout):
    pass

class LibraryWindow(Screen):
    pass

class ItemsListButton(ListItemButton):

def submit_item(self):
    model_id = self.model_id_text_input.text + self.type_text_input.text + self.location_text_input.text + self.weight_text_input.text + self.size_text_input.text + self.price_text_input.text + self.availability_text_input.text

    # Add the student to the ListView
    self.item_list.adapter.data.extend([model_id])

    # Reset the ListView
    self.item_list._trigger_reset_populate()

def delete_item(self, *args):

    # If a list item is selected
    if self.item_list.adapter.selection:
        # Get the text from the item selected
        selection = self.item_list.adapter.selection[0].text

        # Remove the matching item
        self.item_list.adapter.data.remove(selection)

        # Reset the ListView
        self.item_list._trigger_reset_populate()

def replace_item(self, *args):

    # If a list item is selected
    if self.item_list.adapter.selection:
        # Get the text from the item selected
        selection = self.item_list.adapter.selection[0].text

        # Remove the matching item
        self.item_list.adapter.data.remove(selection)

        # Get the student name from the TextInputs
        model_id = self.model_id_text_input.text + self.type_text_input.text + self.location_text_input.text + self.weight_text_input.text + self.size_text_input.text + self.price_text_input.text + self.availability_text_input.text

        # Add the updated data to the list
        self.item_list.adapter.data.extend([model_id])

        # Reset the ListView
        self.item_list._trigger_reset_populate()

model_id_text_input = ObjectProperty()
location_text_input = ObjectProperty()
type_text_input = ObjectProperty()
weight_text_input = ObjectProperty()
size_text_input = ObjectProperty()
price_text_input = ObjectProperty()
availability_text_input = ObjectProperty()
item_list = ObjectProperty()


Builder.load_string("""


<LibraryWindow>

BOXlayOUT:
    orientation: "vertical" .......

这是错误描述

File "D:/Projects/Python/FitMe/core/main.py", line 53, in <module>
 sample_app.run()
File "C:\Users\DELL\venv\lib\site-packages\kivy\app.py", line 828, in run
 runTouchApp()
File "C:\Users\DELL\venv\lib\site-packages\kivy\base.py", line 504, in runTouchApp
 EventLoop.window.mainloop()
File "C:\Users\DELL\venv\lib\site-packages\kivy\core\window\window_sdl2.py", line 663, in mainloop
 self._mainloop()
File "C:\Users\DELL\venv\lib\site-packages\kivy\core\window\window_sdl2.py", line 405, in _mainloop
 EventLoop.idle()
File "C:\Users\DELL\venv\lib\site-packages\kivy\base.py", line 342, in idle
 self.dispatch_input()
File "C:\Users\DELL\venv\lib\site-packages\kivy\base.py", line 327, in dispatch_input
 post_dispatch_input(*pop(0))
File "C:\Users\DELL\venv\lib\site-packages\kivy\base.py", line 233, in post_dispatch_input
 listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\DELL\venv\lib\site-packages\kivy\core\window\__init__.py", line 1188, in on_motion
 self.dispatch('on_touch_down', me)
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\DELL\venv\lib\site-packages\kivy\core\window\__init__.py", line 1204, in on_touch_down
 if w.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\DELL\venv\lib\site-packages\kivy\uix\screenmanager.py", line 1189, in on_touch_down
 return super(ScreenManager, self).on_touch_down(touch)
File "C:\Users\DELL\venv\lib\site-packages\kivy\uix\widget.py", line 457, in on_touch_down
 if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\DELL\venv\lib\site-packages\kivy\uix\relativelayout.py", line 288, in on_touch_down
 ret = super(RelativeLayout, self).on_touch_down(touch)
File "C:\Users\DELL\venv\lib\site-packages\kivy\uix\widget.py", line 457, in on_touch_down
 if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\DELL\venv\lib\site-packages\kivy\uix\widget.py", line 457, in on_touch_down
 if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\DELL\venv\lib\site-packages\kivy\uix\widget.py", line 457, in on_touch_down
  if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\DELL\venv\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
 self.dispatch('on_press')
File "kivy\_event.pyx", line 714, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8146)
File "kivy\_event.pyx", line 1225, in kivy._event.EventObservers.dispatch (kivy\_event.c:14035)
File "kivy\_event.pyx", line 1109, in kivy._event.EventObservers._dispatch (kivy\_event.c:12816)
File "C:\Users\DELL\venv\lib\site-packages\kivy\lang\builder.py", line 64, in custom_callback
 exec(__kvlang__.co_value, idmap)
File "<string>", line 70, in <module>
File "kivy\weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__ (kivy\weakproxy.c:1386)
AttributeError: 'LibraryWindow' object has no attribute 'delete_item'

标签: pythonpython-3.xpython-2.7kivykivy-language

解决方案


LibraryWindow在您的示例中没有属性delete_item,因为您只是通过了。


推荐阅读