Pankaj Project Final Cs File
Pankaj Project Final Cs File
LLAGEDELHI
-94
COMPUTERSCI
ENCE
PROJECTFI
LE
Sessi
on:
-2023-
24
NAME:
-PANKAJKUMAR
CLASS:
-11t
hB
ROLLNO.
:-11239
SUBMI
TTEDTO:
-MR.MOHI
TGI
RI
CERTIFICATE
1. **Initialization:**
- The `FinanceTracker` class is initialized with a Tkinter
`root` window.
- Variables are created using `tk.StringVar()` and
`tk.DoubleVar()` to store transaction category, amount, and
budget values.
2. **Database Setup:**
- An SQLite in-memory database is created using
`sqlite3.connect(":memory:")`.
- `create_tables` method sets up a table named "transactions"
if it doesn't exist, with columns for transaction id, category,
amount, and date.
4. **Adding Transactions:**
- The `add_transaction` method fetches category and amount
from the respective variables.
- The current date and time are obtained using
`datetime.now()` and stored in the SQLite database.
5. **Calculating Balance:**
- `calculate_balance` fetches the total amount of all
transactions from the database and subtracts it from the budget.
6. **Updating Balance:**
- `update_balance` updates the balance label with the current
calculated balance.
class FinanceTracker:
def __init__(self, root):
self.root = root
self.root.title("Finance Tracker")
# Variables
self.transaction_category_var = tk.StringVar()
self.transaction_amount_var = tk.DoubleVar()
self.budget_var = tk.DoubleVar(value=1000) # Set your
initial budget here
# Database
self.conn = sqlite3.connect(":memory:")
self.create_tables()
# UI Setup
ttk.Label(root, text="Category:").grid(row=0, column=0,
padx=10, pady=10, sticky="e")
ttk.Label(root, text="Amount:").grid(row=1, column=0,
padx=10, pady=10, sticky="e")
ttk.Entry(root,
textvariable=self.transaction_category_var).grid(row=0, column=1,
padx=10, pady=10)
ttk.Entry(root,
textvariable=self.transaction_amount_var).grid(row=1, column=1,
padx=10, pady=10)
ttk.Button(root, text="Add Transaction",
command=self.add_transaction).grid(row=2, column=0, columnspan=2,
pady=10)
# Transaction History
self.display_transaction_history()
def create_tables(self):
cursor = self.conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS transactions
(id INTEGER PRIMARY KEY, category TEXT, amount REAL, date TEXT)")
self.conn.commit()
def add_transaction(self):
category = self.transaction_category_var.get()
amount = self.transaction_amount_var.get()
self.display_transaction_history()
self.update_balance()
def calculate_balance(self):
cursor = self.conn.cursor()
cursor.execute("SELECT SUM(amount) FROM transactions")
total_amount = cursor.fetchone()[0] or 0
return self.budget_var.get() - total_amount
def update_balance(self):
balance = self.calculate_balance()
self.budget_var.set(balance)
def display_transaction_history(self):
cursor = self.conn.cursor()
cursor.execute("SELECT category, amount, date FROM
transactions")
transactions = cursor.fetchall()
ttk.Label(self.root, text="Transaction
History:").grid(row=5, column=0, columnspan=2, pady=10)
tree = ttk.Treeview(self.root, columns=("Category",
"Amount", "Date"), show="headings", height=5)
tree.heading("Category", text="Category")
tree.heading("Amount", text="Amount")
tree.heading("Date", text="Date")
tree.grid(row=6, column=0, columnspan=2, padx=10,
pady=10)
if __name__ == "__main__":
root = tk.Tk()
app = FinanceTracker(root)
root.mainloop()
OUTPUT:-
Output No. 1-
Output No.2:-