Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 7603dd7

Browse files
Merge pull request #633 from Gabriela20103967/issue-632
feat: Add a Delete Button in Budget-manager.
2 parents 50ad3e4 + 3a40808 commit 7603dd7

2 files changed

Lines changed: 72 additions & 8 deletions

File tree

projects/Budget-manager/main.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
conn.commit()
1919

2020

21-
# Function to add a transaction to the database
21+
# Function to add a transaction to the database.
2222
def add_transaction():
23+
"""
24+
Adds a transaction to the database based on user input.
25+
"""
2326
date = date_entry.get()
2427
description = description_entry.get()
2528
amount = amount_entry.get()
@@ -39,25 +42,52 @@ def add_transaction():
3942
messagebox.showwarning("Warning", "Please fill in all fields.")
4043

4144

42-
# Function to clear input fields
45+
def delete_transaction():
46+
"""
47+
Deletes a selected transaction from the database.
48+
"""
49+
selected_index = transaction_listbox.curselection()
50+
if selected_index:
51+
transaction_id = transaction_listbox.get(selected_index)[0]
52+
c.execute("DELETE FROM transactions WHERE id=?", (transaction_id,))
53+
conn.commit()
54+
update_transaction_list()
55+
update_balance()
56+
messagebox.showinfo("Success", "Transaction deleted successfully!")
57+
else:
58+
messagebox.showwarning("Warning", "Please select a transaction to delete")
59+
60+
61+
# Function to clear input fields.
62+
4363
def clear_entries():
64+
"""
65+
Clears all input fields.
66+
"""
4467
date_entry.delete(0, tk.END)
4568
description_entry.delete(0, tk.END)
4669
amount_entry.delete(0, tk.END)
4770
category_combobox.set("")
71+
category_combobox.config(state="readonly")
4872

4973

50-
# Function to update the transaction list
74+
# Function to update the transaction list.
5175
def update_transaction_list():
76+
"""
77+
Updates the displayed transaction list based on the database entries.
78+
"""
5279
transaction_listbox.delete(0, tk.END)
5380
c.execute("SELECT * FROM transactions")
5481
transactions = c.fetchall()
5582
for transaction in transactions:
5683
transaction_listbox.insert(tk.END, transaction)
5784

5885

59-
# Function to calculate and display the current balance
86+
# Function to calculate and display the current balance.
6087
def update_balance():
88+
"""
89+
Calculates and displays the current balance based on income and expenses.
90+
"""
6191
c.execute("SELECT SUM(amount) FROM transactions")
6292
total_income = c.fetchone()[0]
6393
if total_income is None:
@@ -70,11 +100,15 @@ def update_balance():
70100
balance_label.config(text=f"Current Balance: ${balance:.2f}")
71101

72102

73-
# Create the main window
103+
# Create the main window.
74104
root = tk.Tk()
75105
root.title("Personal Budget Manager")
106+
root.columnconfigure(1, weight=1)
107+
root.columnconfigure(3, weight=1)
108+
root.columnconfigure(5, weight=1)
76109

77-
# Create and configure widgets
110+
# Create and configure widgets.
111+
delete_button = tk.Button(root, text="Delete Transaction", command=delete_transaction)
78112
date_label = tk.Label(root, text="Date:")
79113
date_entry = tk.Entry(root, width=15)
80114
description_label = tk.Label(root, text="Description:")
@@ -99,7 +133,8 @@ def update_balance():
99133
transaction_listbox = tk.Listbox(root, width=50)
100134
balance_label = tk.Label(root, text="Current Balance: $0.00")
101135

102-
# Place widgets in the window
136+
# Place widgets in the window.
137+
delete_button.grid(row=0, column=9, padx=10, pady=10)
103138
date_label.grid(row=0, column=0, padx=10, pady=10)
104139
date_entry.grid(row=0, column=1, padx=10, pady=10)
105140
description_label.grid(row=0, column=2, padx=10, pady=10)
@@ -112,7 +147,7 @@ def update_balance():
112147
transaction_listbox.grid(row=1, column=0, columnspan=9, padx=10, pady=10)
113148
balance_label.grid(row=2, column=0, columnspan=9, padx=10, pady=10)
114149

115-
# Initialize the transaction list and balance
150+
# Initialize the transaction list and balance.
116151
update_transaction_list()
117152
update_balance()
118153

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
3+
4+
class Budget:
5+
def __init__(self):
6+
self.transactions = []
7+
8+
def add_transaction(self, transaction):
9+
self.transactions.append(transaction)
10+
11+
def delete_transaction(self, transaction):
12+
self.transactions.remove(transaction)
13+
14+
class TestBudgetFunctionality(unittest.TestCase):
15+
def test_delete_transaction(self):
16+
# Arrange
17+
budget = Budget()
18+
transaction_to_delete = {"date": "2023-01-01", "description": "Test Transaction", "amount": 100.0, "category": "Test"}
19+
# Act
20+
budget.add_transaction(transaction_to_delete)
21+
budget.delete_transaction(transaction_to_delete)
22+
23+
# Assert ()
24+
self.assertNotIn(transaction_to_delete, budget.transactions)
25+
26+
if __name__ == '__main__':
27+
unittest.main()
28+
29+

0 commit comments

Comments
 (0)