9. Deleting Orders
Objective: Add functionality to delete orders.
Content:
Creating a delete order button.
Creating a cancel order button.
Writing the delete_selected_pizza function.
Writing the cancel_order Function
9.3. Writing the delete_selected_pizza Function
# Delete selected pizza
def delete_selected_pizza():
"""Removes the selected item from the order list."""
order_selection = order_list.curselection()
if not order_selection:
messagebox.showerror("Input Error", "Please select a pizza to delete.")
else:
order_index = order_selection[0]
# Block deleting the dynamic total line
if order_index == order_list.size() - 1:
messagebox.showerror("Input Error", "Cannot delete the total cost line.")
else:
del orders[order_index]
update_order_list()
order_selection = order_list.curselection(): Gets the index of the selected item in the Listbox as a tuple like (0,) for line 0, or (1, ) for line 1 or (0, 1, 2) for lines 0 to 2.messagebox.showerror("Input Error", "Please select a pizza to delete."): Shows an error message if no pizza is selected.order_index = order_selection[0]gets the selected line number from the tuple at index 0.if order_index == order_list.size() - 1: messagebox.showerror("Input Error", "Cannot delete the total cost line."): Shows an error message if the selected item is the total cost line.del orders[order_index]: Deletes the selected order from the orders list.
9.4. Writing the cancel_order Function
# Cancel whole order
def cancel_order():
orders.clear()
update_order_list()
orders.clear: empties the list of orders.update_order_list(): updates the displayed order, which in effect clears it.