删除列表项错误
我是 Python 新手,我创建了一个带有回收视图的简单应用程序。它有 2 个按钮,分别是添加项目和删除项目。当单击添加或删除项目按钮时,应用程序将在回收视图中更新。它还包含预加载到回收视图中的项目列表。
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from functools import partial
Window.size = (350, 600)
Builder.load_string('''
<RootWidget>:
inputbutton : inputbutton
inputbutton2 : inputbutton2
inputcontent : inputcontent
outputcontent : outputcontent
BoxLayout:
orientation: "vertical"
size_hint : 1, .9
spacing: 10
padding: 20
Label :
text :'Favourite Pizza'
size_hint : 1, .2
font_size : 30
color : 0,0,0,1
TextInput :
id : inputcontent
font_size: 25
focus: True
multiline : False
size_hint : .8, .15
pos_hint : {'x': .1, 'y': .8}
Button :
id : inputbutton
size_hint : .8, .1
pos_hint : {'x': .1, 'y': .8}
text : 'Add Item '
on_press : root.add_item()
Button :
id : inputbutton2
size_hint : .8, .1
pos_hint : {'x': .1, 'y': .8}
text : 'Delete Item '
on_press : root.delete_item()
ListWidget:
viewclass: 'Label'
orientation : 'vertical'
id : outputcontent
RecycleBoxLayout:
default_size: None, 30
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation : 'vertical'
''')
class ListWidget(RecycleView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.items = ["Pepperoni", "Cheese","Papper", "Hawaii", "Seafood", "Ham", "Taco", "Onion"]
self.data = [{'text': str(item)} for item in self.items]
def update(self):
self.data = [{'text': str(item)} for item in self.items]
class RootWidget(BoxLayout):
inputbutton = ObjectProperty(None)
inputbutton2 = ObjectProperty(None)
inputcontent = ObjectProperty(None)
outputcontent = ObjectProperty(None)
def delete_item(self):
if self.inputcontent.text != '':
self.outputcontent.items.remove(self.inputcontent.text)
self.outputcontent.update()
self.inputcontent.text = ''
def add_item(self):
if self.inputcontent.text != '':
formatted = f'\n {self.inputcontent.text}'
self.outputcontent.items.append(formatted)
self.outputcontent.update()
self.inputcontent.text = ''
class MainApp(App):
title='Search App'
def build(self):
Window.clearcolor = (51/255, 153/255, 1, 1)
return RootWidget()
MainApp().run()
当我输入一个Apple并单击添加项目按钮时,它将被添加到回收视图中,但是,当我输入一个Apple并单击删除时,会发生以下错误。如何解决呢?
Traceback (most recent call last):
File "c:\Users\Kelvin Loh\Documents\kivygui\app\add_delete.py", line 105, in <module>
MainApp().run()
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\app.py", line 950, in run
runTouchApp()
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\base.py", line 582, in runTouchApp
EventLoop.mainloop()
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\base.py", line 347, in mainloop
self.idle()
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\base.py", line 391, in idle
self.dispatch_input()
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\base.py", line 342, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\base.py", line 248, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\core\window\__init__.py", line 1412, in on_motion
self.dispatch('on_touch_down', me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\core\window\__init__.py", line 1428, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
self.dispatch('on_press')
File "kivy\_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1132, in kivy._event.EventObservers._dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\lang\builder.py", line 57, in custom_callback
exec(__kvlang__.co_value, idmap)
File "<string>", line 41, in <module>
File "c:\Users\Kelvin Loh\Documents\kivygui\app\add_delete.py", line 84, in delete_item
self.outputcontent.items.remove(self.inputcontent.text)
ValueError: list.remove(x): x not in list
I am new to Python and I have created a simple app with a Recycle view. It has 2 buttons which are adding an item and deleting an item. When the click the added or deleted item button, the app will be updated in the Recycle View. It also contains a list of the item that is preloaded into the recycle view.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from functools import partial
Window.size = (350, 600)
Builder.load_string('''
<RootWidget>:
inputbutton : inputbutton
inputbutton2 : inputbutton2
inputcontent : inputcontent
outputcontent : outputcontent
BoxLayout:
orientation: "vertical"
size_hint : 1, .9
spacing: 10
padding: 20
Label :
text :'Favourite Pizza'
size_hint : 1, .2
font_size : 30
color : 0,0,0,1
TextInput :
id : inputcontent
font_size: 25
focus: True
multiline : False
size_hint : .8, .15
pos_hint : {'x': .1, 'y': .8}
Button :
id : inputbutton
size_hint : .8, .1
pos_hint : {'x': .1, 'y': .8}
text : 'Add Item '
on_press : root.add_item()
Button :
id : inputbutton2
size_hint : .8, .1
pos_hint : {'x': .1, 'y': .8}
text : 'Delete Item '
on_press : root.delete_item()
ListWidget:
viewclass: 'Label'
orientation : 'vertical'
id : outputcontent
RecycleBoxLayout:
default_size: None, 30
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation : 'vertical'
''')
class ListWidget(RecycleView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.items = ["Pepperoni", "Cheese","Papper", "Hawaii", "Seafood", "Ham", "Taco", "Onion"]
self.data = [{'text': str(item)} for item in self.items]
def update(self):
self.data = [{'text': str(item)} for item in self.items]
class RootWidget(BoxLayout):
inputbutton = ObjectProperty(None)
inputbutton2 = ObjectProperty(None)
inputcontent = ObjectProperty(None)
outputcontent = ObjectProperty(None)
def delete_item(self):
if self.inputcontent.text != '':
self.outputcontent.items.remove(self.inputcontent.text)
self.outputcontent.update()
self.inputcontent.text = ''
def add_item(self):
if self.inputcontent.text != '':
formatted = f'\n {self.inputcontent.text}'
self.outputcontent.items.append(formatted)
self.outputcontent.update()
self.inputcontent.text = ''
class MainApp(App):
title='Search App'
def build(self):
Window.clearcolor = (51/255, 153/255, 1, 1)
return RootWidget()
MainApp().run()
When I have typed an Apple and click add item button, it will be added in the Recycle View, however, when I type an Apple and click delete, the following error was incurred. How to resolve it?
Traceback (most recent call last):
File "c:\Users\Kelvin Loh\Documents\kivygui\app\add_delete.py", line 105, in <module>
MainApp().run()
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\app.py", line 950, in run
runTouchApp()
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\base.py", line 582, in runTouchApp
EventLoop.mainloop()
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\base.py", line 347, in mainloop
self.idle()
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\base.py", line 391, in idle
self.dispatch_input()
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\base.py", line 342, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\base.py", line 248, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\core\window\__init__.py", line 1412, in on_motion
self.dispatch('on_touch_down', me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\core\window\__init__.py", line 1428, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
self.dispatch('on_press')
File "kivy\_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1132, in kivy._event.EventObservers._dispatch
File "C:\Users\Kelvin Loh\Documents\kivygui\kivy_venv\lib\site-packages\kivy\lang\builder.py", line 57, in custom_callback
exec(__kvlang__.co_value, idmap)
File "<string>", line 41, in <module>
File "c:\Users\Kelvin Loh\Documents\kivygui\app\add_delete.py", line 84, in delete_item
self.outputcontent.items.remove(self.inputcontent.text)
ValueError: list.remove(x): x not in list
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您修改
delete_item()
代码以打印正在发生的情况,如下所示:您将看到您正在尝试删除不在
items
列表中的内容,因为您在文本前面添加了\n
。解决方法是始终仅使用文本添加到itmes
(或者可能使用strip()
)。这是add_item()
方法的修改版本,可修复该问题:If you modify your
delete_item()
code to print what is going on, like this:You will see that you are trying to delete something that is not in the
items
list, because you are prepending a\n
to the text. The fix is to always use just the text to add to theitmes
(or perhaps usestrip()
). Here is a modified version of youradd_item()
method that fixes the problem: