删除列表项错误
Deleting list item errors
我是 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()
当我输入一个苹果并点击添加项目按钮时,它会被添加到回收视图中,但是,当我输入一个苹果并点击删除时,出现以下错误。如何解决?
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
如果您修改 delete_item()
代码以打印正在发生的事情,如下所示:
def delete_item(self):
print('items:', self.outputcontent.items)
print('trying to delete:', self.inputcontent.text)
if self.inputcontent.text != '':
self.outputcontent.items.remove(self.inputcontent.text)
self.outputcontent.update()
self.inputcontent.text = ''
您会看到您正在尝试删除不在 items
列表中的内容,因为您在文本前添加了 \n
。解决方法是始终只使用文本添加到 itmes
(或者可能使用 strip()
)。这是修复问题的 add_item()
方法的修改版本:
def add_item(self):
if self.inputcontent.text != '':
self.outputcontent.items.append(self.inputcontent.text.strip())
self.outputcontent.update()
self.inputcontent.text = ''
我是 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()
当我输入一个苹果并点击添加项目按钮时,它会被添加到回收视图中,但是,当我输入一个苹果并点击删除时,出现以下错误。如何解决?
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
如果您修改 delete_item()
代码以打印正在发生的事情,如下所示:
def delete_item(self):
print('items:', self.outputcontent.items)
print('trying to delete:', self.inputcontent.text)
if self.inputcontent.text != '':
self.outputcontent.items.remove(self.inputcontent.text)
self.outputcontent.update()
self.inputcontent.text = ''
您会看到您正在尝试删除不在 items
列表中的内容,因为您在文本前添加了 \n
。解决方法是始终只使用文本添加到 itmes
(或者可能使用 strip()
)。这是修复问题的 add_item()
方法的修改版本:
def add_item(self):
if self.inputcontent.text != '':
self.outputcontent.items.append(self.inputcontent.text.strip())
self.outputcontent.update()
self.inputcontent.text = ''