【发布时间】: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