0% found this document useful (0 votes)
6 views9 pages

Import Sqlite3

This document outlines a Python application for a bank management system using SQLite and Tkinter. It includes functionalities for user registration, login, account creation, balance checking, deposits, withdrawals, transaction viewing, and updating customer details. The application features a graphical user interface and handles database operations for user and account management.

Uploaded by

Hardik Sudha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Import Sqlite3

This document outlines a Python application for a bank management system using SQLite and Tkinter. It includes functionalities for user registration, login, account creation, balance checking, deposits, withdrawals, transaction viewing, and updating customer details. The application features a graphical user interface and handles database operations for user and account management.

Uploaded by

Hardik Sudha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

import sqlite3

import tkinter as tk
from tkinter import messagebox, ttk

# Database Connection
def connect_db():
conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT,
password TEXT)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS accounts (
acc_no INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
phone TEXT,
acc_type TEXT,
email TEXT,
balance REAL)''')

cursor.execute('''CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY


AUTOINCREMENT, acc_no INTEGER, type TEXT, amount REAL, timestamp DATETIME DEFAULT
CURRENT_TIMESTAMP)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS staff (id INTEGER PRIMARY KEY, username TEXT,
password TEXT)''')
cursor.execute("INSERT OR IGNORE INTO staff (id, username, password) VALUES (1, 'admin',
'password')")
conn.commit()
conn.close()

# Register Function
def register():
def submit():
username = entry_user.get()
password = entry_pass.get()
if username and password:
conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username,
password))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Registration Successful!")
reg_window.destroy()
else:
messagebox.showerror("Error", "All fields are required!")

reg_window = tk.Toplevel()
reg_window.title("Register")
reg_window.geometry("400x300")
reg_window.configure(bg="lightblue")
tk.Label(reg_window, text="Username", bg="lightblue").pack()
entry_user = tk.Entry(reg_window)
entry_user.pack()
tk.Label(reg_window, text="Password", bg="lightblue").pack()
entry_pass = tk.Entry(reg_window, show="*")
entry_pass.pack()
tk.Button(reg_window, text="Submit", command=submit, bg="green", fg="white").pack()
# Login Function
def login():
def verify():
username = entry_user.get()
password = entry_pass.get()
conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username,
password))
user = cursor.fetchone()
conn.close()
if user:
messagebox.showinfo("Success", "Login Successful!")
login_window.destroy()
main_menu()
else:
messagebox.showerror("Error", "Invalid Credentials!")

login_window = tk.Tk()
login_window.title("Login")
login_window.geometry("400x300")
login_window.configure(bg="lightgreen")
tk.Label(login_window, text="Username", bg="lightgreen").pack()
entry_user = tk.Entry(login_window)
entry_user.pack()
tk.Label(login_window, text="Password", bg="lightgreen").pack()
entry_pass = tk.Entry(login_window, show="*")
entry_pass.pack()
tk.Button(login_window, text="Login", command=verify, bg="blue", fg="white").pack()
tk.Button(login_window, text="Register", command=register, bg="orange", fg="white").pack()
login_window.mainloop()

# Main Menu
def main_menu():
menu = tk.Tk()
menu.title("Bank Management System")
menu.geometry("800x600")
menu.configure(bg="lightyellow")

tk.Label(menu, text="Bank Management System", font=("Arial", 20, "bold"), fg="blue",


bg="lightyellow").pack(pady=20)

tk.Button(menu, text="Create Account", command=create_account, bg="cyan", font=("Arial", 12),


height=2, width=25).pack(pady=10)
tk.Button(menu, text="View Balance", command=view_balance, bg="cyan", font=("Arial", 12),
height=2, width=25).pack(pady=10)
tk.Button(menu, text="Deposit", command=deposit, bg="cyan", font=("Arial", 12), height=2,
width=25).pack(pady=10)
tk.Button(menu, text="Withdraw", command=withdraw, bg="cyan", font=("Arial", 12), height=2,
width=25).pack(pady=10)
tk.Button(menu, text="View Transactions", command=view_transactions, bg="cyan", font=("Arial",
12), height=2, width=25).pack(pady=10)
tk.Button(menu, text="Update Customer Details", command=update_details, bg="cyan",
font=("Arial", 12), height=2, width=25).pack(pady=10)
tk.Button(menu, text="Account List", command=view_accounts, bg="cyan", font=("Arial", 12),
height=2, width=25).pack(pady=10)
tk.Button(menu, text="Close Account", command=close_account, bg="red", fg="white",
font=("Arial", 12), height=2, width=25).pack(pady=10)
tk.Button(menu, text="Exit", command=menu.quit, bg="gray", fg="white", font=("Arial", 12),
height=2, width=25).pack(pady=20)

menu.mainloop()

# Placeholder functions for account operations


def create_account():
def submit():
name = name_entry.get()
phone = phone_entry.get()
acc_type = acc_var.get()
email = email_entry.get()
balance = 10000 if acc_type == "Saving" else 10000

if name and phone and acc_type and email:


conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO accounts (name, phone, acc_type, email, balance) VALUES
(?, ?, ?, ?, ?)",
(name, phone, acc_type, email, balance))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Account Created Successfully!")
account_window.destroy()
else:
messagebox.showwarning("Input Error", "Please fill all fields!")

account_window = tk.Toplevel()
account_window.title("Create Bank Account")
account_window.geometry("400x400")
account_window.configure(bg="lightblue")

tk.Label(account_window, text="Name:", font=("Arial", 12), bg="lightblue").pack(pady=5)


name_entry = tk.Entry(account_window, font=("Arial", 12))
name_entry.pack(pady=5)

tk.Label(account_window, text="Phone Number:", font=("Arial", 12), bg="lightblue").pack(pady=5)


phone_entry = tk.Entry(account_window, font=("Arial", 12))
phone_entry.pack(pady=5)

tk.Label(account_window, text="Account Type:", font=("Arial", 12), bg="lightblue").pack(pady=5)


acc_var = tk.StringVar(value="Saving")
tk.Radiobutton(account_window, text="Saving", variable=acc_var, value="Saving", font=("Arial",
12), bg="lightblue").pack()
tk.Radiobutton(account_window, text="Current", variable=acc_var, value="Current", font=("Arial",
12), bg="lightblue").pack()

tk.Label(account_window, text="Email ID:", font=("Arial", 12), bg="lightblue").pack(pady=5)


email_entry = tk.Entry(account_window, font=("Arial", 12))
email_entry.pack(pady=5)

tk.Button(account_window, text="Create Account", command=submit, bg="green", fg="white",


font=("Arial", 12), height=2, width=20).pack(pady=10)

account_window.mainloop()
def view_balance():
def check_balance():
acc_no = acc_entry.get()
if acc_no:
conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute("SELECT balance FROM accounts WHERE acc_no = ?", (acc_no,))
result = cursor.fetchone()
conn.close()
if result:
messagebox.showinfo("Balance", f"Your account balance is: {result[0]}")
else:
messagebox.showerror("Error", "Account not found!")
else:
messagebox.showwarning("Input Error", "Please enter an account number!")

balance_window = tk.Toplevel()
balance_window.title("View Balance")
balance_window.geometry("300x200")
balance_window.configure(bg="lightgray")

tk.Label(balance_window, text="Enter Account Number:", font=("Arial", 12),


bg="lightgray").pack(pady=5)
acc_entry = tk.Entry(balance_window, font=("Arial", 12))
acc_entry.pack(pady=5)

tk.Button(balance_window, text="Check Balance", command=check_balance, bg="blue",


fg="white", font=("Arial", 12)).pack(pady=10)

balance_window.mainloop()

def deposit():
def submit_deposit():
acc_no = acc_entry.get()
amount = amount_entry.get()
if acc_no and amount:
try:
amount = float(amount)
conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute("SELECT balance FROM accounts WHERE acc_no = ?", (acc_no,))
result = cursor.fetchone()
if result:
new_balance = result[0] + amount
cursor.execute("UPDATE accounts SET balance = ? WHERE acc_no = ?", (new_balance,
acc_no))
conn.commit()
conn.close()
messagebox.showinfo("Success", f"Amount {amount} deposited successfully! New
Balance: {new_balance}")
deposit_window.destroy()
else:
messagebox.showerror("Error", "Account not found!")
except ValueError:
messagebox.showerror("Error", "Please enter a valid amount!")
else:
messagebox.showwarning("Input Error", "Please enter all details!")

deposit_window = tk.Toplevel()
deposit_window.title("Deposit Money")
deposit_window.geometry("300x200")
deposit_window.configure(bg="lightgreen")

tk.Label(deposit_window, text="Enter Account Number:", font=("Arial", 12),


bg="lightgreen").pack(pady=5)
acc_entry = tk.Entry(deposit_window, font=("Arial", 12))
acc_entry.pack(pady=5)

tk.Label(deposit_window, text="Enter Amount:", font=("Arial", 12), bg="lightgreen").pack(pady=5)


amount_entry = tk.Entry(deposit_window, font=("Arial", 12))
amount_entry.pack(pady=5)

tk.Button(deposit_window, text="Deposit", command=submit_deposit, bg="blue", fg="white",


font=("Arial", 12)).pack(pady=10)

deposit_window.mainloop()

def withdraw():
def submit_withdraw():
acc_no = acc_entry.get()
amount = amount_entry.get()
if acc_no and amount:
try:
amount = float(amount)
conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute("SELECT balance FROM accounts WHERE acc_no = ?", (acc_no,))
result = cursor.fetchone()
if result:
if result[0] >= amount:
new_balance = result[0] - amount
cursor.execute("UPDATE accounts SET balance = ? WHERE acc_no = ?", (new_balance,
acc_no))
conn.commit()
conn.close()
messagebox.showinfo("Success", f"Amount {amount} withdrawn successfully! New
Balance: {new_balance}")
withdraw_window.destroy()
else:
messagebox.showerror("Error", "Insufficient Balance!")
else:
messagebox.showerror("Error", "Account not found!")
except ValueError:
messagebox.showerror("Error", "Please enter a valid amount!")
else:
messagebox.showwarning("Input Error", "Please enter all details!")

withdraw_window = tk.Toplevel()
withdraw_window.title("Withdraw Money")
withdraw_window.geometry("300x200")
withdraw_window.configure(bg="lightcoral")
tk.Label(withdraw_window, text="Enter Account Number:", font=("Arial", 12),
bg="lightcoral").pack(pady=5)
acc_entry = tk.Entry(withdraw_window, font=("Arial", 12))
acc_entry.pack(pady=5)

tk.Label(withdraw_window, text="Enter Amount:", font=("Arial", 12), bg="lightcoral").pack(pady=5)


amount_entry = tk.Entry(withdraw_window, font=("Arial", 12))
amount_entry.pack(pady=5)

tk.Button(withdraw_window, text="Withdraw", command=submit_withdraw, bg="red", fg="white",


font=("Arial", 12)).pack(pady=10)

withdraw_window.mainloop()

# Function to view transactions


def view_transactions():
def show_transactions():
acc_no = acc_entry.get()
if acc_no:
conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute("SELECT type, amount, timestamp FROM transactions WHERE acc_no = ?
ORDER BY timestamp DESC", (acc_no,))
transactions = cursor.fetchall()
conn.close()

if not transactions:
messagebox.showinfo("No Transactions", "No transactions found for this account.")
return

trans_window = tk.Toplevel()
trans_window.title("Transaction History")
trans_window.geometry("600x400")

tk.Label(trans_window, text=f"Transaction History for Account: {acc_no}", font=("Arial", 12,


"bold")).pack(pady=5)

tree = ttk.Treeview(trans_window, columns=("Type", "Amount", "Timestamp"),


show="headings")
tree.heading("Type", text="Transaction Type")
tree.heading("Amount", text="Amount")
tree.heading("Timestamp", text="Timestamp")
tree.pack(expand=True, fill='both', padx=10, pady=10)

for trans in transactions:


tree.insert("", "end", values=trans)
else:
messagebox.showwarning("Input Error", "Please enter a valid account number!")

trans_window = tk.Toplevel()
trans_window.title("View Transactions")
trans_window.geometry("300x200")

tk.Label(trans_window, text="Account Number:").pack(pady=5)


acc_entry = tk.Entry(trans_window)
acc_entry.pack(pady=5)
tk.Button(trans_window, text="View Transactions", command=show_transactions, bg="green",
fg="white").pack(pady=10)

trans_window.mainloop()

# Function to update customer details


def update_details():
def update():
acc_no = acc_entry.get()
new_name = name_entry.get()
new_phone = phone_entry.get()
new_email = email_entry.get()

if acc_no:
conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute("UPDATE accounts SET name = ?, phone = ?, email = ? WHERE acc_no = ?",
(new_name, new_phone, new_email, acc_no))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Customer details updated successfully!")
update_window.destroy()
else:
messagebox.showwarning("Input Error", "Please enter a valid account number!")

update_window = tk.Toplevel()
update_window.title("Update Customer Details")
update_window.geometry("400x300")

tk.Label(update_window, text="Account Number:").pack(pady=5)


acc_entry = tk.Entry(update_window)
acc_entry.pack(pady=5)

tk.Label(update_window, text="New Name:").pack(pady=5)


name_entry = tk.Entry(update_window)
name_entry.pack(pady=5)

tk.Label(update_window, text="New Phone:").pack(pady=5)


phone_entry = tk.Entry(update_window)
phone_entry.pack(pady=5)

tk.Label(update_window, text="New Email:").pack(pady=5)


email_entry = tk.Entry(update_window)
email_entry.pack(pady=5)

tk.Button(update_window, text="Update", command=update, bg="blue",


fg="white").pack(pady=10)

update_window.mainloop()

def view_accounts():
def staff_login():
username = user_entry.get()
password = pass_entry.get()
conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM staff WHERE username = ? AND password = ?", (username,
password))
staff = cursor.fetchone()
conn.close()
if staff:
staff_login_window.destroy()
show_accounts()
else:
messagebox.showerror("Login Failed", "Invalid Credentials")

def show_accounts():
conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM accounts")
accounts = cursor.fetchall()
conn.close()

accounts_window = tk.Toplevel()
accounts_window.title("All Customer Accounts")
accounts_window.geometry("600x400")

tree = ttk.Treeview(accounts_window, columns=("acc_no", "name", "phone", "acc_type",


"email", "balance"), show="headings")
tree.heading("acc_no", text="Account No")
tree.heading("name", text="Name")
tree.heading("phone", text="Phone")
tree.heading("acc_type", text="Account Type")
tree.heading("email", text="Email")
tree.heading("balance", text="Balance")
tree.pack(expand=True, fill='both')

for acc in accounts:


tree.insert("", "end", values=acc)

messagebox.showinfo("Access Denied", "This is for staff only")


staff_login_window = tk.Toplevel()
staff_login_window.title("Staff Login")
staff_login_window.geometry("300x200")

tk.Label(staff_login_window, text="Username:").pack(pady=5)
user_entry = tk.Entry(staff_login_window)
user_entry.pack(pady=5)

tk.Label(staff_login_window, text="Password:").pack(pady=5)
pass_entry = tk.Entry(staff_login_window, show="*")
pass_entry.pack(pady=5)

tk.Button(staff_login_window, text="Login", command=staff_login).pack(pady=10)

staff_login_window.mainloop()

def close_account():
def delete():
acc_no = acc_entry.get()
if acc_no:
confirm = messagebox.askyesno("Confirm", "Are you sure you want to delete this account?")
if confirm:
conn = sqlite3.connect("bank.db")
cursor = conn.cursor()
cursor.execute("DELETE FROM accounts WHERE acc_no = ?", (acc_no,))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Account deleted successfully!")
close_window.destroy()
else:
messagebox.showwarning("Input Error", "Please enter a valid account number!")

close_window = tk.Toplevel()
close_window.title("Close Account")
close_window.geometry("300x200")

tk.Label(close_window, text="Account Number:").pack(pady=5)


acc_entry = tk.Entry(close_window)
acc_entry.pack(pady=5)

tk.Button(close_window, text="Delete Account", command=delete, bg="red",


fg="white").pack(pady=10)

close_window.mainloop()

def exit_application():
confirm = messagebox.askyesno("Exit", "Are you sure you want to exit?")
if confirm:
root.destroy()
# Function to exit application
def exit_application():
confirm = messagebox.askyesno("Exit", "Are you sure you want to exit?")
if confirm:
root.quit()

# Initialize Database
connect_db()
# Start Login
login()

You might also like