所以,我正在制作一个食谱应用程序。当我在我的 ingredients_list 中放入正确的食材来做饭,然后点击“pot”按钮时,即使有正确的食材,它也会说“不能做饭”。继承人的代码:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.dropdown import DropDown
class IngredientPopup(Popup):
def __init__(self, **kwargs):
super(IngredientPopup, self).__init__(**kwargs)
# Create a GridLayout for the pop-up window
layout = GridLayout(cols=2)
# Add a label to display current ingredients
self.current_ingredients_label = Label(text='Current Ingredients:\n')
layout.add_widget(self.current_ingredients_label)
# Add a button to add ingredients
add_button = Button(text='Add Ingredient')
add_button.bind(on_press=self.add_ingredient_popup)
layout.add_widget(add_button)
# Add a button to remove ingredients
remove_button = Button(text='Remove Ingredient')
remove_button.bind(on_press=self.remove_ingredient_popup)
layout.add_widget(remove_button)
# Add the GridLayout to the pop-up window
self.content = layout
# Load the current ingredients list
self.load_ingredients_list()
def add_ingredient_popup(self, instance):
# Create a new pop-up window for adding ingredients
add_popup = AddIngredientPopup()
add_popup.bind(on_dismiss=self.update_ingredient_list)
add_popup.open()
def remove_ingredient_popup(self, instance):
# Create a new pop-up window for removing ingredients
remove_popup = RemoveIngredientPopup()
remove_popup.bind(on_dismiss=self.update_ingredient_list)
remove_popup.open()
def load_ingredients_list(self):
# Ingredients list
ingredients_list = ['salt', 'pepper', 'sugar', 'flour', 'rice', 'chicken', 'carrots', 'onion']
# Update the label to display the current ingredients list
self.current_ingredients_label.text = 'Current Ingredients:\n' + '\n'.join(ingredients_list)
def update_ingredient_list(self, instance):
# Update the list of ingredients displayed in the pop-up window
self.load_ingredients_list()
class AddIngredientPopup(Popup):
def __init__(self, **kwargs):
super(AddIngredientPopup, self).__init__(**kwargs)
# Create a GridLayout for the pop-up window
layout = GridLayout(cols=2)
# Add a label to ask for ingredient name
ingredient_name_label = Label(text='Ingredient Name:')
layout.add_widget(ingredient_name_label)
# Add a TextInput widget to enter the ingredient name
self.ingredient_name_input = TextInput()
layout.add_widget(self.ingredient_name_input)
# Add a button to add the new ingredient
add_ingredient_button = Button(text='Add')
add_ingredient_button.bind(on_press=self.add_ingredient)
layout.add_widget(add_ingredient_button)
# Add the GridLayout to the pop-up window
self.content = layout
def add_ingredient(self, instance):
# Get the ingredient name entered by the user
ingredient_name = self.ingredient_name_input.text
# Save the new ingredient
# Print the ingredient name to the console
print(f"Added ingredient: {ingredient_name}")
# Close the pop-up window
self.dismiss()
class RemoveIngredientPopup(Popup):
def __init__(self, **kwargs):
super(RemoveIngredientPopup, self).__init__(**kwargs)
# Create a GridLayout for the pop-up window
layout = GridLayout(cols=2)
# Add a label to select ingredient to remove
ingredient_label = Label(text='Select Ingredient:')
layout.add_widget(ingredient_label)
# Add a dropdown menu to select the ingredient to remove
self.ingredient_dropdown = DropDown()
self.load_ingredients_list()
layout.add_widget(self.ingredient_dropdown)
# Add a button to remove the selected ingredient
remove_ingredient_button = Button(text='Remove')
remove_ingredient_button.bind(on_press=self.remove_ingredient)
layout.add_widget(remove_ingredient_button)
# Add the GridLayout to the pop-up window
self.content = layout
def load_ingredients_list(self):
# Ingredients
ingredients_list = ['salt', 'pepper', 'sugar', 'flour', 'rice', 'chicken', 'carrots', 'onion']
# Add the ingredients to the dropdown menu
for ingredient in ingredients_list:
self.ingredient_dropdown.add_widget(Button(text=ingredient, on_release=self.remove_ingredient, size_hint_y=None, height=44))
def remove_ingredient(self, instance):
# Get the name of the selected ingredient
selected_ingredient = instance.text
# Remove the ingredient
# Print the ingredient name to the console
print(f"Removed ingredient: {selected_ingredient}")
# Close the pop-up window
self.dismiss()
class TopStorage(Button):
def __init__(self, **kwargs):
super(TopStorage, self).__init__(**kwargs)
self.opacity = 0
self.size_hint = (None, None)
self.size = (0, 0)
def on_press(self):
# create popup window
popup = IngredientPopup(title='Top Storage', size_hint=(None, None), size=(400, 400))
# open popup window
popup.open()
class TopFridge(Button):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.opacity = 0
self.size_hint = (None, None)
self.size = (0, 0)
def on_press(self):
# create popup window
popup = Popup(title='Top Fridge', size_hint=(None, None), size=(400, 400))
# add content to popup window
label = Label(text='top fridge')
popup.content = label
# open popup window
popup.open()
class BottomFridge(Button):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.opacity = 0
self.size_hint = (None, None)
self.size = (0, 0)
def on_press(self):
# create popup window
popup = Popup(title='Bottom Fridge', size_hint=(None, None), size=(400, 400))
# add content to popup window
label = Label(text='bottom fridge')
popup.content = label
# open popup window
popup.open()
class Pot(Button):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.opacity = 1
self.size_hint = (None, None)
self.size = (0, 0)
def on_press(self):
ingredients = ['rice', 'chicken', 'carrots', 'onion']
meals = ['chicken and rice', 'chicken stew', 'chicken curry']
possible_meals = [meal for meal in meals if set(ingredients).issuperset(set(meal.split()))]
content = Label(text='\n'.join(possible_meals) if possible_meals else 'No meals can be made')
popup = Popup(title='Possible Meals', content=content, size_hint=(0.8, 0.8))
popup.open()
class MyApp(App):
def build(self):
# Create the main layout
layout = FloatLayout()
# Create the background image
background = Image(source='C:/Users/Nikola/Desktop/KuhinjaV2.jpg')
# Create the buttons
button1 = TopStorage(text='storage gore')
button2 = TopFridge(text='frizider gore')
button3 = BottomFridge(text='frizider dole')
button4 = Pot(text='pot')
# Position the buttons on top of the background image
button1.pos_hint = {'x': 0.08, 'y': 0.75}
button1.size_hint = (0.85, 0.2)
button2.pos_hint = {'x': 0.8, 'y': 0.4}
button2.size_hint = (0.15, 0.2)
button3.pos_hint = {'x': 0.8, 'y': 0.035}
button3.size_hint = (0.15, 0.35)
button4.pos_hint = {'x': 0.094, 'y': 0.38}
button4.size_hint = (0.07, 0.07)
# Add the background and buttons to the layout
layout.add_widget(background)
layout.add_widget(button1)
layout.add_widget(button2)
layout.add_widget(button3)
layout.add_widget(button4)
return layout
return TopStorageButton(text='Top Storage')
if __name__ == '__main__':
MyApp().run()
我是初学者,所以非常感谢一些解释!
我尝试将成分添加到 ingredients_list,但似乎没有任何效果。甚至尝试去除我不需要做那顿饭的其他成分,没有用。