diff --git a/Shopping-list.py b/Shopping-list.py index e93e7b4..04b3602 100644 --- a/Shopping-list.py +++ b/Shopping-list.py @@ -1,5 +1,6 @@ import tkinter as tk -from tkinter import messagebox +from tkinter import ttk, messagebox +from ttkbootstrap import Style # Global variables shopping_list = {} # Initialize an empty shopping list @@ -11,7 +12,7 @@ def display_list(): listbox.delete(0, tk.END) for item, amount in shopping_list.items(): - listbox.insert(tk.END, "- " + item + " (Amount: " + str(amount) + ")") + listbox.insert(tk.END, f"{item} (Amount: {amount})") # Function to add an item to the shopping list def add_item(): @@ -19,15 +20,18 @@ def add_item(): item = entry_item.get() amount = entry_amount.get() if item and amount: - amount = int(amount) - if item in shopping_list: - shopping_list[item] += amount - else: - shopping_list[item] = amount - entry_item.delete(0, tk.END) - entry_amount.delete(0, tk.END) - display_list() - messagebox.showinfo("Success", str(amount) + " " + item + "(s) have been added to your shopping list.") + try: + amount = int(amount) + if item in shopping_list: + shopping_list[item] += amount + else: + shopping_list[item] = amount + entry_item.delete(0, tk.END) + entry_amount.delete(0, tk.END) + display_list() + messagebox.showinfo("Success", f"{amount} {item}(s) have been added to your shopping list.") + except ValueError: + messagebox.showerror("Error", "Please enter a valid number for the amount.") else: messagebox.showerror("Error", "Please enter both item and amount.") @@ -39,59 +43,78 @@ def remove_item(): del shopping_list[item] entry_item.delete(0, tk.END) display_list() - messagebox.showinfo("Success", item + " has been removed from your shopping list.") + messagebox.showinfo("Success", f"{item} has been removed from your shopping list.") else: - messagebox.showerror("Error", item + " is not in your shopping list.") + messagebox.showerror("Error", f"{item} is not in your shopping list.") # Function to calculate the total amount of all items def calculate_total(): total = sum(shopping_list.values()) - messagebox.showinfo("Total Items", "Total number of items in the shopping list: " + str(total)) + messagebox.showinfo("Total Items", f"Total number of items in the shopping list: {total}") # Main function def main(): global entry_item, entry_amount, listbox + root = tk.Tk() root.title("Shopping List") - - frame_logo = tk.Frame(root) - frame_logo.pack(padx=10, pady=10) - - label_logo = tk.Label(frame_logo, text="SHOPPING LIST", font=("Helvetica", 24, "bold")) - label_logo.pack() - - frame = tk.Frame(root) - frame.pack(padx=10, pady=10) - - label_item = tk.Label(frame, text="Item:") - label_item.grid(row=0, column=0, padx=5, pady=5, sticky="e") - - entry_item = tk.Entry(frame) - entry_item.grid(row=0, column=1, padx=5, pady=5) - - label_amount = tk.Label(frame, text="Amount:") - label_amount.grid(row=1, column=0, padx=5, pady=5, sticky="e") - - entry_amount = tk.Entry(frame) - entry_amount.grid(row=1, column=1, padx=5, pady=5) - - button_add = tk.Button(frame, text="Add Item", command=add_item) - button_add.grid(row=2, column=0, columnspan=2, padx=5, pady=5, sticky="we") - - button_remove = tk.Button(frame, text="Remove Item", command=remove_item) - button_remove.grid(row=3, column=0, columnspan=2, padx=5, pady=5, sticky="we") - - button_display = tk.Button(frame, text="Display List", command=display_list) - button_display.grid(row=4, column=0, columnspan=2, padx=5, pady=5, sticky="we") - - button_calculate = tk.Button(frame, text="Calculate Total", command=calculate_total) - button_calculate.grid(row=5, column=0, columnspan=2, padx=5, pady=5, sticky="we") - - listbox = tk.Listbox(frame) - listbox.grid(row=6, column=0, columnspan=2, padx=5, pady=5, sticky="nsew") + root.geometry("500x600") + + style = Style(theme="flatly") + + # Create a main frame + main_frame = ttk.Frame(root, padding="20 20 20 20") + main_frame.pack(fill=tk.BOTH, expand=True) + + # Logo + label_logo = ttk.Label(main_frame, text="SHOPPING LIST", font=("Helvetica", 24, "bold"), foreground="#3498db") + label_logo.pack(pady=(0, 20)) + + # Input frame + input_frame = ttk.Frame(main_frame) + input_frame.pack(fill=tk.X, pady=(0, 20)) + + # Item input + label_item = ttk.Label(input_frame, text="Item:", font=("Helvetica", 12)) + label_item.grid(row=0, column=0, padx=(0, 10), pady=5, sticky="e") + entry_item = ttk.Entry(input_frame, font=("Helvetica", 12), width=30) + entry_item.grid(row=0, column=1, pady=5) + + # Amount input + label_amount = ttk.Label(input_frame, text="Amount:", font=("Helvetica", 12)) + label_amount.grid(row=1, column=0, padx=(0, 10), pady=5, sticky="e") + entry_amount = ttk.Entry(input_frame, font=("Helvetica", 12), width=30) + entry_amount.grid(row=1, column=1, pady=5) + + # Buttons frame + buttons_frame = ttk.Frame(main_frame) + buttons_frame.pack(fill=tk.X, pady=(0, 20)) + + # Add and Remove buttons + button_add = ttk.Button(buttons_frame, text="Add Item", command=add_item, style="success.TButton") + button_add.pack(side=tk.LEFT, expand=True, padx=(0, 5)) + button_remove = ttk.Button(buttons_frame, text="Remove Item", command=remove_item, style="danger.TButton") + button_remove.pack(side=tk.RIGHT, expand=True, padx=(5, 0)) + + # Display and Calculate buttons + button_display = ttk.Button(main_frame, text="Display List", command=display_list, style="info.TButton") + button_display.pack(fill=tk.X, pady=(0, 10)) + button_calculate = ttk.Button(main_frame, text="Calculate Total", command=calculate_total, style="warning.TButton") + button_calculate.pack(fill=tk.X) + + # Listbox + listbox_frame = ttk.Frame(main_frame) + listbox_frame.pack(fill=tk.BOTH, expand=True, pady=(20, 0)) + + listbox = tk.Listbox(listbox_frame, font=("Helvetica", 12), bg="#f0f0f0", selectbackground="#3498db") + listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) + + scrollbar = ttk.Scrollbar(listbox_frame, orient=tk.VERTICAL, command=listbox.yview) + scrollbar.pack(side=tk.RIGHT, fill=tk.Y) + listbox.config(yscrollcommand=scrollbar.set) root.mainloop() # Run the main function if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a69642b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pillow==10.4.0 +ttkbootstrap==1.10.1