0% found this document useful (0 votes)
10 views4 pages

C T

Uploaded by

gunjanc080
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

C T

Uploaded by

gunjanc080
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

from kivy.

app import App


from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout

class CalorieTrackerApp(App):
def build(self):
# Set up the root layout (BoxLayout)
self.layout = BoxLayout(orientation='vertical', padding=10, spacing=10)

# Title Label
self.title_label = Label(text="Calorie Tracker", font_size=24)
self.layout.add_widget(self.title_label)

# Target Calorie input


self.target_calories_input = TextInput(hint_text="Set your target
calories", size_hint_y=None, height=30)
self.target_calories_input.bind(text=self.on_target_calories_change)
self.layout.add_widget(self.target_calories_input)

# Food item input


self.food_input = TextInput(hint_text="Food Item (e.g., chicken)",
size_hint_y=None, height=30)
self.food_input.bind(text=self.on_food_input_change)
self.layout.add_widget(self.food_input)

# ScrollView for food item suggestions


self.food_scroll = ScrollView(size_hint=(1, None), size=(400, 200))
self.food_list_layout = GridLayout(cols=1, padding=10, spacing=5,
size_hint_y=None)

self.food_list_layout.bind(minimum_height=self.food_list_layout.setter('height'))
self.food_scroll.add_widget(self.food_list_layout)
self.layout.add_widget(self.food_scroll)

# Quantity input (in grams)


self.quantity_input = TextInput(hint_text="Quantity in grams (e.g., 150g)",
size_hint_y=None, height=30)
self.layout.add_widget(self.quantity_input)

# Add meal button


self.add_meal_button = Button(text="Add Meal", size_hint_y=None, height=40)
self.add_meal_button.bind(on_press=self.add_meal)
self.layout.add_widget(self.add_meal_button)

# ScrollView for meal list (scrollable meal list with remove buttons)
self.meal_scroll = ScrollView(size_hint=(1, None), size=(400, 200))
self.meal_list_layout = GridLayout(cols=1, padding=10, spacing=5,
size_hint_y=None)

self.meal_list_layout.bind(minimum_height=self.meal_list_layout.setter('height'))
self.meal_scroll.add_widget(self.meal_list_layout)
self.layout.add_widget(self.meal_scroll)

# Total calories label


self.total_calories_label = Label(text="Total Calories Consumed: 0",
font_size=18)
self.layout.add_widget(self.total_calories_label)

# Remaining calories label


self.remaining_calories_label = Label(text="Remaining Calories: 0",
font_size=18)
self.layout.add_widget(self.remaining_calories_label)

# Total calories variable


self.total_calories = 0 # Track total calories
self.target_calories = 0 # Track target calories

return self.layout

def on_target_calories_change(self, instance, value):


"""Update target calories if input is valid."""
try:
# Set target calories based on user input
self.target_calories = int(value)
self.update_remaining_calories()
except ValueError:
self.target_calories = 0

def on_food_input_change(self, instance, value):


"""Handle text input change for food item, filter based on typed text."""
self.food_list_layout.clear_widgets() # Clear the list before adding new
suggestions
suggestions = self.get_food_suggestions(value)
self.show_food_suggestions(suggestions)

def get_food_suggestions(self, query):


"""Return a filtered list of food suggestions based on user input."""
food_items = [
"peanuts", "cashews", "dates", "milk", "rice", "chapati", "chapati with
ghee", "egg", "chicken", "butter naan",
"palak paneer", "mixed vegetables", "papad", "dal bhat", "buttermilk",
"raisins", "khichdi", "dal", "jeera rice", "stir fried potatoes",
"bhujiya", "roasted chana", "roasted peanuts",
"pista", "tal chikki", "peanut chikki", "lauki and chana sabji",
"okra", "onion and potato sabji",
"ravaiya", "mug pulao", "curd", "milk with bournevita", "chicken
biryani", "raw onions", "proteinex", "ghee"
]
return [item for item in food_items if
item.lower().startswith(query.lower())]

def show_food_suggestions(self, suggestions):


"""Display food suggestions as buttons."""
for item in suggestions:
btn = Button(text=item, size_hint_y=None, height=40)
btn.bind(on_press=self.on_food_item_select)
self.food_list_layout.add_widget(btn)

def on_food_item_select(self, instance):


"""When a food item button is selected, fill the food_input with the
selected item."""
self.food_input.text = instance.text

def add_meal(self, instance):


"""Add the meal, calculate total calories, and update meal list."""
food_item = self.food_input.text.strip()
quantity_text = self.quantity_input.text.strip()

try:
# Extract quantity in grams
quantity = int(quantity_text.replace('g', '').strip())
except ValueError:
return

if food_item:
# Get calorie data for food item
calories_per_gram = self.get_food_calories(food_item)

if calories_per_gram > 0:
# Calculate total calories based on quantity
total_item_calories = calories_per_gram * quantity

# Create a layout for the meal entry with a remove button


meal_entry_layout = BoxLayout(size_hint_y=None, height=40)
meal_label = Label(text=f"{food_item} ({quantity}g) -
{total_item_calories} cal", size_hint_x=0.8)
remove_button = Button(text="Remove", size_hint_x=0.2)
remove_button.bind(on_press=lambda btn:
self.remove_meal(meal_entry_layout, total_item_calories))

meal_entry_layout.add_widget(meal_label)
meal_entry_layout.add_widget(remove_button)
self.meal_list_layout.add_widget(meal_entry_layout)

# Update total calories


self.total_calories += total_item_calories
self.total_calories_label.text = f"Total Calories Consumed:
{self.total_calories}"

# Update remaining calories


self.update_remaining_calories()

# Clear input fields after adding the meal


self.food_input.text = ""
self.quantity_input.text = ""

def remove_meal(self, meal_entry_layout, meal_calories):


"""Remove the meal entry from the list and adjust total calories."""
self.meal_list_layout.remove_widget(meal_entry_layout)
self.total_calories -= meal_calories
self.total_calories_label.text = f"Total Calories Consumed:
{self.total_calories}"
self.update_remaining_calories()

def update_remaining_calories(self):
"""Update the remaining calories based on target and total consumed."""
if self.target_calories > 0:
remaining_calories = self.target_calories - self.total_calories
self.remaining_calories_label.text = f"Remaining Calories:
{remaining_calories if remaining_calories > 0 else 0}"
else:
self.remaining_calories_label.text = "Remaining Calories: 0"
def get_food_calories(self, food_item):
"""Return calorie values per gram for a comprehensive list of food
items."""
# Calorie values per 100g
food_calories_per_100g = {
"peanuts": 567, "cashews": 553, "dates": 277, "milk": 61, "rice": 130,
"chapati": 300, "chapati with ghee": 315, "egg": 155, "chicken": 293,
"butter naan": 294, "paneer sabji": 299, "manchow soup": 314.5, "french
fries": 312,
"carrot": 41, "cucumber": 16, "tinde ki sabji": 45, "moong ki sabji":
170,
"chana ki sabji": 107, "lauki and chana sabji": 90, "okra": 33,
"onion and potato sabji": 110, "ravaiya": 80.6, "mug pulao": 152,
"curd": 98,
"milk with bournevita": 90, "chicken biryani": 140, "raw onions": 40,
"tuver ki sabji": 80, "matar ki sabji": 90, "cauliflower sabji": 25,
"chhash": 10, "glucose powder": 380, "fruity bread": 265, "rusk": 407,
"britannia biscuits": 480, "parle g biscuits": 453, "bhujiya": 591,
"proteinex": 401, "ghee": 123
}
# Normalize to calories per gram
return food_calories_per_100g.get(food_item.lower(), 0) / 100

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy