【问题标题】:Submit and delete buttons won't work in Python and Kivy (ListItemButton)提交和删除按钮在 Python 和 Kivy (ListItemButton) 中不起作用
【发布时间】:2018-04-25 08:49:48
【问题描述】:

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

重点是当我使用 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'

【问题讨论】:

标签: python python-3.x python-2.7 kivy kivy-language


【解决方案1】:

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

【讨论】:

  • 谢谢!我在 'class LibraryWindow(Screen):' 下移动了所有具有 'def submit、delete、replace 和 Object 属性的函数:现在它告诉我这个错误 self.dispatch('on_press') File "C:\Users\DELL\venv \lib\site-packages\kivy\lang\builder.py”,第 64 行,在 custom_callback exec(kvlang.co_value, idmap) 文件“”,第 66 行,在 文件“D:\Projects\Python\FitMe\windows\Library.py”,第 40 行,在 submit_item + self.availability_text_input.text AttributeError: 'NoneType' object has no attribute 'text'跨度>
  • @Ваня Олексий 我假设这是在 submit_item 方法中。查看您提供的代码,您永远不会分配availability_text_input。另请注意,ObjectProperies 不会自动具有文本属性。您必须为您可以使用的 ObjectProperty 分配一些东西。
  • 好的,我看到你了。关键是,在这种情况下,当我运行程序时,它运行得很好——只有当我按下其中一个按钮(提交、删除、替换)时才会出现问题。错误因我按的内容而异。如果我按提交,这是我已经提到的错误。如果我按下删除或替换 - 它说对象没有属性适配器。所以看起来python文件无法到达代码的一部分。 ***** 如果这可以帮助你 - 当我将它用作单独的文件时它工作正常 - 但是当我将 kivy 文件分配为窗口时 它会显示此错误。那会是什么?
  • 我认为问题在于您使用的是 ObjectProperties 而不是 StringProperties。但是,如果没有更清晰的代码图片,我不能肯定地说。正如 eyllanesc 在上面评论的那样,最小、完整和可验证的示例将帮助您获得答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多