如何在从main.py添加的OneLineAvatarIconListItem上添加on_press函数?(python和kivymd)

时间:2020-11-03 07:05:42

标签: python kivy kivy-language kivymd

我想为名为OneLineAvatarIconListItem的{​​{1}}添加一个onpress语句。按下项目时,我想打开另一个屏幕,但是我不明白如何放置该语句。

该代码应该接受一些配料的输入,然后我有一个文件,其中有配方配料和配方的名称。 这是我的代码(对不起的名字很抱歉)

MAIN.PY:

pluto

.KV文件:

import pandas as pd
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 = []
tim = []


class FridgeScreen(Screen):
    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_except = book.drop(columns='Recipe')
        lst2 = []
        recipe_freq = {}

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

        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_except[recipe_except['Ingredients'] == top_recipe[z][0]].index.values
            foo = recipe_except['Name'][int(tommy)]
            tim.append(foo)
            z += 1

        a = 0
        for it in range(len(tim)):
            pluto = OneLineAvatarIconListItem(text=tim[a])
            self.recipe_screen.ids.recipe.add_widget(pluto)
            pluto.on_press()
            a += 1


class FullRecipesScreen(Screen):
    pass


class RecipeScreen(Screen):
    pass


class RootScreen(ScreenManager):
    pass


class MainApp(MDApp):
    def build(self):
        return RootScreen()


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

1 个答案:

答案 0 :(得分:0)

在您创建OneLineAvatarIconListItem的python代码中,您可以指定on_press,如下所示:

    for x in range(len(fridge_items)):
        lst = OneLineAvatarIconListItem(text=fridge_items[i], on_press=partial(print, fridge_items[i]))
        i += 1

使用partial设置要调用的方法及其参数。当然,您可以使用任何可用的方法来代替print