如何在 BoxLayout 中显示一个简单的列表和一个带有字典的列表
How to display a simple list and a list with dictionaries in BoxLayout
我不知道如何在 BoxLayout 中显示一个简单的列表和一个带有字典的列表(class 内容),我试图通过以下方式显示“data_name_list”列表行行,“data_all_list”逐行列出,但只有键“item_name”的值。
这是.py文件
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()
这是.kv文件
#: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
您可以只定义一个方法来执行您想要的操作,然后使用 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))
我不知道如何在 BoxLayout 中显示一个简单的列表和一个带有字典的列表(class 内容),我试图通过以下方式显示“data_name_list”列表行行,“data_all_list”逐行列出,但只有键“item_name”的值。
这是.py文件
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()
这是.kv文件
#: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
您可以只定义一个方法来执行您想要的操作,然后使用 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))