You are on page 1of 5

import mysql.

connector as ms
from tkinter import *
from tkinter import messagebox as msg

#banking operations
def banking():
tk = Tk()
tk.title("Banking")
tk.geometry("300x300")
tk.configure(background="white")
lbl = Label(tk, text="Banking", font=("Arial Bold", 20), bg="white")
lbl.pack()
btn1 = Button(tk, text="Create an account", command=create_account)
btn1.pack()
btn2 = Button(tk, text="Log in", command=log_in)
btn2.pack()
tk.mainloop()

#create an account
def create_account():
def insertDB(name,age,balance,password):
mydb = ms.connect(host="localhost", user="root", passwd="",
database="banking")
mycursor = mydb.cursor()
mycursor.execute("create table if not exists accounts(name varchar(50),age
int,balance int);")
mycursor.execute("INSERT INTO accounts (name, age, balance,password) VALUES
(%s, %s, %s, %s)", (name, age, balance, password))
mydb.commit()
banking()

tk = Tk()
tk.title("Create an account")
tk.geometry("300x300")
tk.configure(background="white")
lbl = Label(tk, text="Create an account", font=("Arial Bold", 20), bg="white")
lbl.pack()
lbl1 = Label(tk, text="Name: ", font=("Arial Bold", 10), bg="white")
lbl1.pack()
name = Entry(tk)
name.pack()
lbl2 = Label(tk, text="Age: ", font=("Arial Bold", 10), bg="white")
lbl2.pack()
age = Entry(tk)
age.pack()
lbl3 = Label(tk, text="Balance: ", font=("Arial Bold", 10), bg="white")
lbl3.pack()
balance = Entry(tk)
balance.pack()
lbl4 = Label(tk, text="Password", font=("Arial Bold", 10), bg="white")
lbl4.pack()
password = Entry(tk)
password.pack()
btn = Button(tk, text="Create an account", command=lambda:
insertDB(name.get(),int(age.get()),int(balance.get()),password.get()))
btn.pack()
tk.mainloop()

#log in
def log_in():
def submit(un, pwd):
tk.destroy()
mydb = ms.connect(host="localhost", user="root", passwd="",
database="banking")
myCur = mydb.cursor()
myCur.execute("SELECT * FROM accounts")
value = myCur.fetchall()

for i in range(len(value)):
if value[i][0] == un:
if value[i][3] == pwd:
msg.showinfo(title='Alert', message='Login succesful')
mainApp()
break
else:
msg.showinfo(title='Alert', message='Incorrect password')
break
else:
msg.showinfo(title='Alert', message='User not registered')

tk = Tk()
tk.title("Log in")
tk.geometry("300x300")
tk.configure(background="white")
lbl = Label(tk, text="Log in", font=("Arial Bold", 20), bg="white")
lbl.pack()
lbl1 = Label(tk, text="Username: ", font=("Arial Bold", 10), bg="white")
lbl1.pack()
un = Entry(tk)
un.pack()
lbl2 = Label(tk, text="Password: ", font=("Arial Bold", 10), bg="white")
lbl2.pack()
pwd = Entry(tk)
pwd.pack()
btn = Button(tk, text="Log in", command=lambda: submit(un.get(), pwd.get()))
btn.pack()
tk.mainloop()

#main app
def mainApp():
tk = Tk()
tk.title("Banking")
tk.geometry("300x300")
tk.configure(background="white")
btn1 = Button(tk, text="Deposit", command=deposit)
btn1.pack()
btn2 = Button(tk, text="Withdraw", command=withdraw)
btn2.pack()
btn3 = Button(tk, text="Check Balance", command=balance)
btn3.pack()
btn4 = Button(tk, text="Log out", command=banking)
btn4.pack()
btn5 = Button(tk, text="Register", command=register)
btn5.pack()
tk.mainloop()

#register
def register():
def submit(name,password,balance):
mydb = ms.connect(host="localhost", user="root", passwd="",
database="banking")
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM accounts WHERE name = %s", (name,))
myresult = mycursor.fetchall()
for x in myresult:
if x[0] == name:
msg.showinfo(title='Alert', message='User already registered')
break
else:
mycursor.execute("INSERT INTO accounts (name, password, balance) VALUES
(%s, %s, %s)", (name, password, balance))
mydb.commit()
msg.showinfo(title='Alert', message='Registration successful')

tk = Tk()
tk.title("Register")
tk.geometry("300x300")
tk.configure(background="white")
lbl = Label(tk, text="Register", font=("Arial Bold", 20), bg="white")
lbl.pack()
lbl1 = Label(tk, text="Name: ", font=("Arial Bold", 10), bg="white")
lbl1.pack()
name = Entry(tk)
name.pack()
lbl2 = Label(tk, text="Password: ", font=("Arial Bold", 10), bg="white")
lbl2.pack()
password = Entry(tk)
password.pack()
lbl3 = Label(tk, text="Balance: ", font=("Arial Bold", 10), bg="white")
lbl3.pack()
balance = Entry(tk)
balance.pack()
button = Button(tk, text="Register", command=lambda:
submit(name.get(),password.get(),int(balance.get())))
button.pack()
mainApp()
tk.mainloop()

#withdraw
def withdraw():
def submit(name,amount):
mydb = ms.connect(host="localhost", user="root", passwd="",
database="banking")
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM accounts WHERE name = %s", (name,))
myresult = mycursor.fetchall()
for x in myresult:
if x[0] == name:
mycursor.execute("UPDATE accounts SET balance = balance - %s WHERE
name = %s", (amount, name))
mydb.commit()
msg.showinfo(title='Alert', message='Withdrawal successful')
break

tk = Tk()
tk.title("Withdraw")
tk.geometry("300x300")
tk.configure(background="white")
lbl = Label(tk, text="Withdraw", font=("Arial Bold", 20), bg="white")
lbl.pack()
lbl1 = Label(tk, text="Name: ", font=("Arial Bold", 10), bg="white")
lbl1.pack()
name = Entry(tk)
name.pack()
lbl2 = Label(tk, text="Amount to withdraw ", font=("Arial Bold", 10),
bg="white")
lbl2.pack()
amount = Entry(tk)
amount.pack()
button = Button(tk, text="Withdraw", command=lambda:
submit(name.get(),int(amount.get())))
button.pack()
mainApp()
tk.mainloop()

#deposit
def deposit():
def submit(name,amount):
mydb = ms.connect(host="localhost", user="root", passwd="",
database="banking")
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM accounts WHERE name = %s", (name,))
myresult = mycursor.fetchall()
for x in myresult:
if x[0] == name:
mycursor.execute("UPDATE accounts SET balance = balance + %s WHERE
name = %s", (amount, name))
mydb.commit()
msg.showinfo(title='Alert', message='Deposit successful')
break

tk = Tk()
tk.title("Deposit")
tk.geometry("300x300")
tk.configure(background="white")
lbl = Label(tk, text="Deposit", font=("Arial Bold", 20), bg="white")
lbl.pack()
lbl1 = Label(tk, text="Name: ", font=("Arial Bold", 10), bg="white")
lbl1.pack()
name = Entry(tk)
name.pack()
lbl2 = Label(tk, text="Amount to deposit ", font=("Arial Bold", 10),
bg="white")
lbl2.pack()
amount = Entry(tk)
amount.pack()
button = Button(tk, text="Deposit", command=lambda:
submit(name.get(),int(amount.get())))
button.pack()
mainApp()
tk.mainloop()

#balance
def balance():
def submit(name):
mydb = ms.connect(host="localhost", user="root", passwd="",
database="banking")
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM accounts WHERE name=%s", (name,))
myresult = mycursor.fetchall()
for x in myresult:
if x[0] == name:
msg.showinfo(title='Alert', message='Your balance is ' + str(x[1]))
break
tk = Tk()
tk.title("Balance")
tk.geometry("300x300")
tk.configure(background="white")
lbl = Label(tk, text="Balance", font=("Arial Bold", 20), bg="white")
lbl.pack()
lbl1 = Label(tk, text="Name: ", font=("Arial Bold", 10), bg="white")
lbl1.pack()
name = Entry(tk)
name.pack()
button = Button(tk, text="Check Balance", command=lambda: submit(name.get()))
button.pack()
mainApp()
tk.mainloop()

banking()

You might also like