如何使用OneLineIconListItem向每个动态项添加图标。我的代码给出了TypeError:object.\uuuu init\uuuu()不接受任何参数

2024-10-03 06:30:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎不理解这个错误,因为如果我将图标作为参数从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

Tags: selfurlinitdeflistclass图标icon
1条回答
网友
1楼 · 发布于 2024-10-03 06:30:12

经过@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]

相关问题 更多 >