C T
C T
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)
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)
# 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)
return self.layout
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
meal_entry_layout.add_widget(meal_label)
meal_entry_layout.add_widget(remove_button)
self.meal_list_layout.add_widget(meal_entry_layout)
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()