Apologies for the oversight! The code I provided should be saving the total balance on exit using the pickle module. However, if the file doesn't exist or there's any issue with loading/saving, that can result in the application not retaining the expected amount.
Let's refine the implementation to ensure that the initial balance is set correctly, and any changes to it (like deducting expenses) are saved properly upon exiting and reopening the app.
Here’s an updated and improved version of the application that ensures transactions and the total balance persist across sessions:
import customtkinter as ctk
import pickle
import os
class TransactionTrackerApp:
def __init__(self, master):
self.master = master
self.master.title("Transaction Tracker")
self.data_file = "transaction_data.pkl"
self.transactions_file = "transactions.txt"
self.total_amount = self.load_data()
# Initial amount input
self.initial_frame = ctk.CTkFrame(master)
self.initial_frame.pack(pady=10)
self.initial_label = ctk.CTkLabel(self.initial_frame, text="Set your initial amount:")
self.initial_label.pack(side=ctk.LEFT)
self.initial_entry = ctk.CTkEntry(self.initial_frame)
self.initial_entry.pack(side=ctk.LEFT)
self.set_button = ctk.CTkButton(self.initial_frame, text="Set Amount", command=self.set_initial_amount)
self.set_button.pack(side=ctk.LEFT)
# Transaction input
self.transaction_frame = ctk.CTkFrame(master)
self.transaction_frame.pack(pady=10)
self.description_label = ctk.CTkLabel(self.transaction_frame, text="Website/Description:")
self.description_label.pack(side=ctk.LEFT)
self.description_entry = ctk.CTkEntry(self.transaction_frame)
self.description_entry.pack(side=ctk.LEFT)
self.transaction_label = ctk.CTkLabel(self.transaction_frame, text="Transaction Amount:")
self.transaction_label.pack(side=ctk.LEFT)
self.transaction_entry = ctk.CTkEntry(self.transaction_frame)
self.transaction_entry.pack(side=ctk.LEFT)
self.add_button = ctk.CTkButton(self.transaction_frame, text="Add Transaction", command=self.add_transaction)
self.add_button.pack(side=ctk.LEFT)
# Balance display
self.balance_frame = ctk.CTkFrame(master)
self.balance_frame.pack(pady=10)
self.balance_label = ctk.CTkLabel(self.balance_frame, text=f"Current Balance: ${self.total_amount:.2f}")
self.balance_label.pack()
# Transaction log display
self.log_frame = ctk.CTkFrame(master)
self.log_frame.pack(pady=10)
self.log_label = ctk.CTkLabel(self.log_frame, text="Transaction Log:")
self.log_label.pack()
self.log_text = ctk.CTkTextbox(self.log_frame, width=300, height=200)
self.log_text.pack()
self.log_text.configure(state='disabled')
self.show_transactions()
def set_initial_amount(self):
try:
self.total_amount = float(self.initial_entry.get())
self.balance_label.configure(text=f"Current Balance: ${self.total_amount:.2f}")
self.initial_entry.delete(0, ctk.END)
self.save_data() # Save the initial amount to file
except ValueError:
self.initial_entry.delete(0, ctk.END)
def add_transaction(self):
try:
description = self.description_entry.get()
transaction_amount = float(self.transaction_entry.get())
if description and transaction_amount:
self.total_amount -= transaction_amount # Deduct the transaction
self.balance_label.configure(text=f"Current Balance: ${self.total_amount:.2f}")
self.log_transaction(description, transaction_amount)
self.description_entry.delete(0, ctk.END)
self.transaction_entry.delete(0, ctk.END)
self.save_data() # Save updated total balance and transactions
self.show_transactions()
except ValueError:
self.transaction_entry.delete(0, ctk.END)
def log_transaction(self, description, amount):
with open(self.transactions_file, "a") as f:
f.write(f"{description}: -${amount:.2f}\n")
def show_transactions(self):
self.log_text.configure(state='normal')
self.log_text.delete(1.0, ctk.END) # Clear current log
if os.path.exists(self.transactions_file):
with open(self.transactions_file, "r") as f:
transactions = f.readlines()
for transaction in transactions:
self.log_text.insert(ctk.END, transaction)
self.log_text.configure(state='disabled')
def save_data(self):
with open(self.data_file, 'wb') as f:
pickle.dump(self.total_amount, f)
def load_data(self):
if os.path.exists(self.data_file):
with open(self.data_file, 'rb') as f:
return pickle.load(f)
return 0.0
if __name__ == "__main__":
root = ctk.CTk()
app = TransactionTrackerApp(root)
root.mainloop()
Key Improvements:
-
Loading Data on Start:
- Loads the total amount when the app starts using the
load_data method.
-
Saving Data on Set & Add:
- The balance is saved every time a new initial amount is set or a transaction is added.
-
File Handling:
- The application now correctly reads from and writes to both the transaction logs and the balance file.
-
Improved Validation:
- Ensures that the user inputs valid values.
Ensure Data Is Persistent:
Make sure that:
- The Python script has the necessary permissions to read/write in the directory you are running it from.
- You don't have any background processes or open files that might lock these files when you try to save or read them.
How to Run This Enhanced Application
- Save the code into a file (e.g.,
persistent_transaction_tracker.py).
- Run the application using Python:
python persistent_transaction_tracker.py
Now, your app should correctly save and load the balance and transaction history every time you open or close the application.