-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorders_tk.py
More file actions
94 lines (75 loc) · 2.88 KB
/
orders_tk.py
File metadata and controls
94 lines (75 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# orders_tk.py
# -*- coding: utf-8 -*-
'''
Creación de un panel de control que permita tomar órdenes/comidas de un restaurante
El programa está por varios platos/comidas:
Están los items predefinidos, se introduce los precios y la cantidad de cada uno de los items
También se puede añadir la propina (tip)
Cuando se pulsa en "Calculate total" se visualiza el precio final, el IVA correspondiente y el total, incluida la propina
'''
import tkinter as tk
from tkinter import ttk
IVA_RATE = 0.21 # Impuesto (tanto por uno)
def safe_float(value):
"""Convierte a float o devuelve 0 si está vacío o es inválido."""
try:
return float(value)
except:
return 0.0
def safe_int(value):
"""Convierte a int o devuelve 0 si está vacío o es inválido."""
try:
return int(value)
except:
return 0
def calculate_total():
subtotal = 0
for item in menu_items:
price = safe_float(item['price'].get())
quantity = safe_int(item['quantity'].get())
subtotal += price * quantity
tip = safe_float(tip_entry.get())
iva = subtotal * IVA_RATE
total = subtotal + iva + tip
result_label.config(
text=f"Subtotal: €{subtotal:.2f}\n"
f"IVA (21%): €{iva:.2f}\n"
f"Tip: €{tip:.2f}\n"
f"Total: €{total:.2f}"
)
# Ventana principal
root = tk.Tk()
root.title("Food Menu")
# Comidas
menu_items = [
{'name': 'Burger'},
{'name': 'Pizza'},
{'name': 'Salad'},
{'name': 'Soda'},
{'name': 'Dessert'}
]
# Encabezados
ttk.Label(root, text="Item", font=("Arial", 11, "bold")).grid(row=0, column=0, padx=10)
ttk.Label(root, text="Price (€)", font=("Arial", 11, "bold")).grid(row=0, column=1, padx=10)
ttk.Label(root, text="Quantity", font=("Arial", 11, "bold")).grid(row=0, column=2, padx=10)
# Campos de entrada
for i, item in enumerate(menu_items, start=1):
ttk.Label(root, text=item['name']).grid(row=i, column=0, padx=10, pady=5)
item['price'] = ttk.Entry(root, width=10)
item['price'].grid(row=i, column=1, padx=10)
item['quantity'] = ttk.Entry(root, width=10)
item['quantity'].grid(row=i, column=2, padx=10)
# Espacio adicional antes del "tip"
ttk.Label(root, text="").grid(row=len(menu_items)+1)
# Tip
ttk.Label(root, text="Tip (€):", font=("Arial", 11)).grid(row=len(menu_items)+2, column=0, padx=10, pady=10)
tip_entry = ttk.Entry(root, width=10)
tip_entry.grid(row=len(menu_items)+2, column=1, padx=10)
# Botón de cálculo
calculate_button = ttk.Button(root, text="Calculate Total", command=calculate_total)
calculate_button.grid(row=len(menu_items)+3, column=0, columnspan=3, pady=15)
# Resultado
result_label = ttk.Label(root, text="", font=("Arial", 11))
result_label.grid(row=len(menu_items)+4, column=0, columnspan=3, pady=10)
# Main
root.mainloop()