import tkinter as tk
from tkinter import messagebox
import mysql.connector
# Connect to the MySQL database
def connect_to_db():
return mysql.connector.connect(
host=”localhost”,
user=”root”,
password=””, # Your MySQL root password, if any
database=”bank_db”
)
# Function to add a new record
def add_record():
conn = connect_to_db()
cursor = conn.cursor()
sql = “INSERT INTO customers (name, account_number, balance) VALUES (%s, %s, %s)”
values = (entry_name.get(), entry_account_number.get(), entry_balance.get())
cursor.execute(sql, values)
conn.commit()
conn.close()
messagebox.showinfo(“Success”, “Record added successfully!”)
clear_entries()
show_records()
# Function to update a record
def update_record():
conn = connect_to_db()
cursor = conn.cursor()
sql = “UPDATE customers SET name=%s, balance=%s WHERE account_number=%s”
values = (entry_name.get(), entry_balance.get(), entry_account_number.get())
cursor.execute(sql, values)
conn.commit()
conn.close()
messagebox.showinfo(“Success”, “Record updated successfully!”)
clear_entries()
show_records()
# Function to delete a record
def delete_record():
conn = connect_to_db()
cursor = conn.cursor()
sql = “DELETE FROM customers WHERE account_number=%s”
values = (entry_account_number.get(),)
cursor.execute(sql, values)
conn.commit()
conn.close()
messagebox.showinfo(“Success”, “Record deleted successfully!”)
clear_entries()
show_records()
# Function to show all records
def show_records():
conn = connect_to_db()
cursor = conn.cursor()
cursor.execute(“SELECT * FROM customers”)
records = cursor.fetchall()
conn.close()
listbox_records.delete(0, tk.END)
for record in records:
listbox_records.insert(tk.END, f”ID: {record[0]}, Name: {record[1]}, Account Number: {record[2]}, Balance: {record[3]}”)
# Function to clear input fields
def clear_entries():
entry_name.delete(0, tk.END)
entry_account_number.delete(0, tk.END)
entry_balance.delete(0, tk.END)
# GUI setup
root = tk.Tk()
root.title(“Bank Management System”)
# Name
label_name = tk.Label(root, text=”Name”)
label_name.grid(row=0, column=0)
entry_name = tk.Entry(root)
entry_name.grid(row=0, column=1)
# Account Number
label_account_number = tk.Label(root, text=”Account Number”)
label_account_number.grid(row=1, column=0)
entry_account_number = tk.Entry(root)
entry_account_number.grid(row=1, column=1)
# Balance
label_balance = tk.Label(root, text=”Balance”)
label_balance.grid(row=2, column=0)
entry_balance = tk.Entry(root)
entry_balance.grid(row=2, column=1)
# Buttons
button_add = tk.Button(root, text=”Add Record”, command=add_record)
button_add.grid(row=3, column=0)
button_update = tk.Button(root, text=”Update Record”, command=update_record)
button_update.grid(row=3, column=1)
button_delete = tk.Button(root, text=”Delete Record”, command=delete_record)
button_delete.grid(row=4, column=0)
button_show = tk.Button(root, text=”Show Records”, command=show_records)
button_show.grid(row=4, column=1)
# Listbox to display records
listbox_records = tk.Listbox(root, width=80)
listbox_records.grid(row=5, column=0, columnspan=2)
# Run the GUI application
root.mainloop()