我似乎不理解这个错误,因为如果我将图标作为参数从OneLineIconListItem中删除,它可以工作,但没有食物图标
class FoodScreen(Screen):
pass
class FoodApp(MDApp):
image = StringProperty()
meal = StringProperty()
category = StringProperty()
origin = StringProperty()
ingre = StringProperty()
source =StringProperty()
def __init__(self,**kwargs):
Window.size = (500,800)
super().__init__(**kwargs)
def on_start(self):
ingredient = self.root.ids['foods'].ids['list']
url="https://www.themealdb.com/api/json/v1/1/random.php"
respond = requests.get(url=url).json()
icons = list(md_icons.keys())
icon=icons[2193]
for i in range(1,20):
if respond['meals'][0][f'strIngredient{i}'] :
l= OneLineIconListItem(text=respond['meals'][0][f'strIngredient{i}'],icon=icon)
ingredient.add_widget(l)
FoodApp().run()
基维姆仍然是个新手 .kv文件:
ScreenManager:
FoodScreen:
id:foods
name:"foods"
<FoodScreen>:
FloatLayout:
ScrollView:
pos_hint:{'top':.4}
size_hint:1,.5
MDList:
id:list
经过@Xyanight的大量搜索和提示, 首先添加:
class ListItem(OneLineIconListItem): icon = StringProperty("food")
并将on_start
方法调整为for i in range(1,20): if respond['meals'][0][f'strIngredient{i}'] : l= ListItem(text=respond['meals'][0][f'strIngredient{i}'], icon=self.icon) ingredient.add_widget(l)
在kv文件中添加:<ListItem>: IconLeftWidget: icon: app.icon
其中FoodApp类变量
icon =StringProperty()
和局部变量self.icon = icons[2193]
相关问题 更多 >
编程相关推荐