So i have most of a code for a menu that displays menu items and their details, the code works (for the most part) the main problem i have is when i press a button i.e. ‘byte burger’ it displays the wrong menu item/details, Also when i open the menu it should initially display the details for ‘byte burger’ but instead its showing the wrong one,
import tkinter as tk
class BurgerMenuApp:
def __init__(self, root):
self.root = root
self.root.title("Codetown Burger Co Menu")
self.current_index = 0
self.label = tk.Label(self.root, text="")
self.label.pack(pady=10)
# define menuu items
self.menu_items = [
{"name": "Byte Burger", "bun": "Milk bun", "sauce": "Tomato sauce", "patties": 1, "cheese": 0, "tomato": False, "lettuce": True, "onion": False},
{"name": "Ctrl-Alt-Delicious", "bun": "Milk bun", "sauce": "Barbecue sauce", "patties": 2, "cheese": 2, "tomato": True, "lettuce": True, "onion": True},
{"name": "Data Crunch", "bun": "Gluten free bun", "sauce": "Tomato sauce", "patties": 0, "cheese": 0, "tomato": True, "lettuce": True, "onion": True},
{"name": "Code Cruncher", "bun": "Milk bun", "sauce": "Tomato sauce", "patties": 3, "cheese": 3, "tomato": True, "lettuce": True, "onion": True},
]
self.display_menu_item()
self.create_buttons()
self.auto_cycle()
def display_menu_item(self):
item = self.menu_items[self.current_index]
details = f"Name: {item['name']}nBun: {item['bun']}nSauce: {item['sauce']}nPatties: {item['patties']}nCheese: {item['cheese']}nTomato: {'Yes' if item['tomato'] else 'No'}nLettuce: {'Yes' if item['lettuce'] else 'No'}nOnion: {'Yes' if item['onion'] else 'No'}"
self.label.config(text=details)
def create_buttons(self):
for index, item in enumerate(self.menu_items):
button = tk.Button(self.root, text=item["name"], command=lambda i=index: self.select_menu_item(i))
button.pack(pady=5)
def select_menu_item(self, index):
self.current_index = index
self.display_menu_item()
if getattr(self, 'cycle_timer', None):
self.root.after_cancel(self.cycle_timer)
self.auto_cycle()
def auto_cycle(self):
if getattr(self, 'auto_cycle_enabled', True):
self.current_index = (self.current_index + 1) % len(self.menu_items)
self.display_menu_item()
# Set a timer for the next cycle
self.cycle_timer = self.root.after(5000, self.auto_cycle)
root = tk.Tk()
app = BurgerMenuApp(root)
root.mainloop()
New contributor
GOOBER 501 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.