我不明白为什么我的MDlist没有出现(kivyMD)

时间:2020-10-28 05:11:43

标签: python kivymd

我故意在代码中出错以进行检查,似乎没有使用该方法。该代码应该接受一些配料的输入,然后我有一个文件,其中包含配方配料和配方的名称。

因此top_recipe是用户拥有最多配料的食谱,也是其排序列表。然后搜索配方搜索配方并在另一个屏幕上创建一个具有配方名称的MDlist,并且变量tommy, tim, foo, pluto就是这个原因

这是未显示的列表。 我认为错误是在on_press: app.my.add_lst()行请仔细检查的kv文件中。我在互联网上搜索了我认为可能是问题的地方,请检查问题是否在其他地方

这是我的代码(对不起的名字很抱歉)

MAIN.PY

import pandas as pd
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from kivymd.uix.list import OneLineAvatarIconListItem

Builder.load_file('design.kv')
Window.size = (300, 500)

fridge_items = []


class FridgeScreen(Screen):
    tim = []

    def add_item(self):
    global lst
    i = 0
    fridge_items.append(self.ids.inp.text)
    self.ids.inp.text = ''
    for x in range(len(fridge_items)):
        lst = OneLineAvatarIconListItem(text=fridge_items[i])
        i += 1
    self.ids.list.add_widget(lst)

def search_recipe(self):
    book = pd.read_csv('RECIPE_BOOK.csv')
    ingredients = book['Ingredients']
    recipe_chodh = book.drop(columns='Recipe')
    lst2 = []
    recipe_freq = {}

    for items in fridge_items:
        for ill in ingredients:
            if items in ill:
                lst2.append(ill)

    for recipe in lst2:
        if recipe in recipe_freq:
            recipe_freq[recipe] += 1
        else:
            recipe_freq[recipe] = 1

    top_recipe = sorted(recipe_freq.items(),
                        key=lambda kv: kv[1],
                        reverse=True)

    z = 0
    for items in top_recipe:
        tommy = recipe_chodh[recipe_chodh['Ingredients'] == top_recipe[z][0]].index.values
        foo = recipe_chodh['Name'][int(tommy)]
        self.tim.append(foo)
        z += 1

    print(self.tim)


class RecipeScreen(Screen):
    def add_lst(self):
        a = 0
        for it in range(len(FridgeScreen.tim)):
            pluto = OneLineAvatarIconListItem(text=FridgeScreen.tim[a])
            root.ids.recipe.add_widget(pluto)
            a += 1


class My(RecipeScreen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.app = App.get_running_app()


class RootScreen(ScreenManager):
    pass


class MainApp(MDApp):
    my = My()

    def build(self):
        return RootScreen()


if __name__ == '__main__':
    MainApp().run()

DESIGN.KV

<FridgeScreen>:
    Screen:
        GridLayout:
            cols: 1
            MDToolbar:
                title: 'KITCHEN'
                left_action_items: [["menu", lambda x : print(x)]]
            GridLayout:
                cols: 1
                padding: 10

                MDTextField:
                    id: inp
                    hint_text: 'enter your ingredients'
                    size_hint_x: None
                    width: 200

                ScrollView:
                    MDList:
                        id: list

            GridLayout:
                cols: 2
                size_hint: 0.2, 0.2
                padding: 10
                spacing: 80
                MDRectangleFlatButton:
                    text: 'search for recipes'
                    on_press: root.search_recipe()
                    on_press: app.my.add_lst()
                    on_press: root.manager.current = 'recipe_screen'

                MDFloatingActionButton:
                    icon: "plus"
                    on_press: root.add_item()

<RecipeScreen>:
    id: recipe_screen
    Screen:
        ScrollView:
            MDList:
                id: recipe


<RootScreen>:
    FridgeScreen:
        name:
            'fridge_screen'
    RecipeScreen:
        name:
            'recipe_screen'

0 个答案:

没有答案