'Deleting list item errors
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
