1818conn .commit ()
1919
2020
21- # Function to add a transaction to the database
21+ # Function to add a transaction to the database.
2222def 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+
4363def 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.
5175def 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.
6087def 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.
74104root = tk .Tk ()
75105root .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 )
78112date_label = tk .Label (root , text = "Date:" )
79113date_entry = tk .Entry (root , width = 15 )
80114description_label = tk .Label (root , text = "Description:" )
@@ -99,7 +133,8 @@ def update_balance():
99133transaction_listbox = tk .Listbox (root , width = 50 )
100134balance_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 )
103138date_label .grid (row = 0 , column = 0 , padx = 10 , pady = 10 )
104139date_entry .grid (row = 0 , column = 1 , padx = 10 , pady = 10 )
105140description_label .grid (row = 0 , column = 2 , padx = 10 , pady = 10 )
@@ -112,7 +147,7 @@ def update_balance():
112147transaction_listbox .grid (row = 1 , column = 0 , columnspan = 9 , padx = 10 , pady = 10 )
113148balance_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.
116151update_transaction_list ()
117152update_balance ()
118153
0 commit comments