【问题标题】:I have written the following code with kivymd(python) but my list is not showing up我已经用 kivymd(python) 编写了以下代码,但我的列表没有显示
【发布时间】:2020-10-27 08:10:35
【问题描述】:

这是我的代码(我想在按下搜索配方时在第二个屏幕上添加一个列表...): 在我的代码中,我制作了一个 kivy 头像图标列表并添加到屏幕中,但出现了第一个屏幕,当我按下按钮时,它会打开一个白色而不是带有列表的屏幕。

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' 

对不起,愚蠢的命名(它的一些乐趣)

【问题讨论】:

    标签: python kivy kivy-language kivymd


    【解决方案1】:

    对象 类存在问题。

    所以,您有“FridgeScreen”类。这个类的 instance 实际上没有名字,但它是在你的 kv 文件中创建的:

        FridgeScreen:
            name:
                'fridge_screen'
    

    这一行:

    self.tim.append(foo)
    

    您修改了 FridgeScreen-instance 的属性。

    这一行:

    for it in range(len(FridgeScreen.tim)):
    

    您正在访问您的 FridgeScreen-class

    的属性

    这些只是两个不同的属性;)。 FirdeScreen.tim 始终是一个空列表,就像在行 tim = [] 中初始化一样。

    解决方案:

    为您的 FridgeScreen 实例命名 (=id) 并创建从您的 RecipeScreen 到您的 FridgeScreen 的引用:

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

    现在您可以使用以下方法在 RecipeScreen-Class 中访问正确的 tim-attribute:

    self.fridge_screen.tim
    

    提示:您需要对 root 执行类似操作,因为默认情况下 root 属性仅在您的 kv 文件中可用。

    RecipeScreen:
        root: root
    

    并且在您的代码中使用self.root

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 2013-06-23
      • 2022-12-03
      • 1970-01-01
      相关资源
      最近更新 更多