如何在从main.py(python和kivymd)添加的OneLineavariconListItem上添加_press函数

2024-10-04 01:29:50 发布

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

我想为名为plutoOneLineAvatarIconListItem添加一个onpress语句。我想打开另一个屏幕时,按下它的项目,但我不明白如何把该声明

这段代码需要输入一些配料,然后我有一个文件,其中有配方配料和配方的名称。 这是我的代码(很抱歉我的名字太傻了)

MAIN.PY:

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()

.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: root.manager.current = 'recipe_screen'

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


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


<FullRecipesScreen>:
    Label:
        id: ingredients
        text: 'kuch bhi'
    Label:
        id: full_recipe
        text: 'bakwaas'


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

    RecipeScreen:
        id: recipe_screen
        fridge_screen: fridge_screen
        name:
            'recipe_screen'

    FullRecipesScreen:
        id: full_recipes_screen
        name:
            'full_recipes_screen'

Tags: textinfromimportselfidforrecipe
1条回答
网友
1楼 · 发布于 2024-10-04 01:29:50

在python代码中,在创建OneLineAvatarIconListItem的地方,可以指定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

相关问题 更多 >