Trying to clear an MDList
New and trying to learn :)
I have a simple gui using kivy and kivymd.
one button adds a list of TwoLineListItem's and i would like the other button to clear the previously generated list.
the idea is for one button to add the list, the other button to clear it so i can populate the list and clear it over and over but by clicking the relevant buttons.
from kivy.app import App
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivymd.uix.list import TwoLineListItem
#designate our kv design file
Builder.load_file('shd_proto_kv_cfg.kv')
class Search(TwoLineListItem):
pass
class MyLayout(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def add_entries(self):
for x in range(0, 10):
item = Search()
self.ids.List.add_widget(item)
def rem_entries(self):
self.ids.List.remove_widget()
pass
class ShdApp(MDApp):
def build(self):
return MyLayout()
if __name__ == '__main__':
ShdApp().run()
<Search>:
text: "Title"
secondary_text: "Description"
<MyLayout>:
orientation: "horizontal"
padding: 25
spacing: 10
BoxLayout:
orientation: "vertical"
Label:
text: "Marker01"
font_size: 25
background_color: (196/255, 140/255, 96/255, 1)
canvas.before:
Color:
rgba: self.background_color
Rectangle:
size: self.size
pos: self.pos
Button:
text: "add em"
on_press: root.add_entries()
Button:
text: "clear em"
on_press: root.rem_entries()
BoxLayout:
orientation: "vertical"
cols: 1
ScrollView:
MDList:
id: List
the error i get from running the above code -
TypeError: Layout.remove_widget() missing 1 required positional argument: 'widget'
as mentioned i would like to populate with one button and clear with the other and be able to populate, clear, populate, clear.
im stuck on this and i do feel its my lack of understanding as a learner thats holding me back can someone please help me with this code so i can get it working and play around to understand it better? eventually i will repurpose it into my first project but i need some help understanding where im going wrong here lol
Thanks!
Use:
self.ids.List.clear_widgets()
instead of:
self.ids.List.remove_widget()
The clear_widgets()
method removes all the children of the object. The remove_widget()
method removes just one child (and that child must be specified).