I can’t figure out how to display a simple list and a list with dictionaries in BoxLayout (class Content), I’m trying to display the “data_name_list” list line by line, and the “data_all_list” list line by line, but only the values by the key “item_name”.
Here is .py file
from kivy.app import App from kivy.uix.boxlayout import BoxLayout data_all_list = [ {"list_id": 1, "list_name": "List 1", "item1": {"item_id": 1, "item_name": "Product 1", "item_quantity": "1", "item_weight": "2", "item_price": "35"}, "item2": {"item_id": 2, "item_name": "Product 2", "item_quantity": "2", "item_weight": "2", "item_price": "35"}}, {"list_id": 2, "list_name": "List 2", "item1": {"item_id": 1, "item_name": "Product 1", "item_quantity": "2", "item_weight": "2", "item_price": "40"}, "item2": {"item_id": 2, "item_name": "Product 2", "item_quantity": "2", "item_weight": "2", "item_price": "35"}, "item3": {"item_id": 3, "item_name": "Product 3", "item_quantity": "1", "item_weight": "2", "item_price": "35"}} ] data_name_list = ["name_1", "name_2", "name_3"] class Main(BoxLayout): pass class Content(BoxLayout): pass class MyApp(App): def build(self): return Main() if __name__ == "__main__": MyApp().run()
Here is .kv file
#:kivy 2.0.0 Main: <Main>: orientation: "vertical" canvas.before: Color: rgba: 255, 255, 255, 1 Rectangle: pos: self.pos size: self.size Content: orientation: "vertical" spacing: 15 canvas.before: Color: rgba: 0.16, 0.62, 0.39, 1 Rectangle: pos: self.pos size: self.size
Advertisement
Answer
You can just define a method that does what you want, then call that method using Clock.schedule_once()
:
class MyApp(App): def build(self): Clock.schedule_once(self.add_lists) return Main() def add_lists(self, dt): content = self.root.children[0] # this can be replaced by an ids access if an id is assigned to Content for name in data_name_list: content.add_widget(Label(text=name)) for d in data_all_list: for key, d1 in d.items(): if isinstance(d1, dict): item_name = d1['item_name'] content.add_widget(Label(text=item_name))