简体   繁体   中英

Submit and delete buttons won't work in Python and Kivy (ListItemButton)

I'm currently having a problem with list Item Button and making it work well.

Point is that it works well when I use Kivy and Python files. But when put my Kivy file inside Python using function Builder - it would run the program but if I press the "submit" or "delete" button the program quit. Here is the part of the code in Python as well as the error description. Any help would be appreciated

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

Here is the error description

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM