Introduction:
This project is a Homestay Booking System developed using Python's Tkinter
module for the graphical user interface and MySQL as the supporting database. The system
is designed to help staff manage homestay bookings efficiently. Key features include staff
login, guest booking entry, price calculation, and the ability to view, search, update, and
delete bookings. This system simplifies the overall process and ensures that all booking
records are stored safely in the database.
Work Flowchart:
Full Coding:
from tkinter import *
from tkinter import messagebox, ttk
import [Link]
import sys
# =DB CONNECTION=
def connect_db():
return [Link](
host="localhost",
user="root",
password="",
database="homestay_db"
# =LOGIN=
def show_login():
login_window = Tk()
login_window.geometry("300x180")
login_window.title("LOGIN STAFF HOMESTAY")
login_window.configure(bg="#e6f2ff")
def clearinput():
[Link]("")
[Link]("")
[Link]("")
def checklogin():
if [Link]() == "staff" and [Link]() == "12345":
[Link]("Success", "Welcome")
login_window.destroy()
show_booking_system()
elif [Link]() == "" or [Link]() == "":
[Link]("Alert", "Credentials cannot be empty!")
else:
[Link]("Error", "Wrong Credentials inserted!")
def exitApp():
confirm = [Link]("Exit", "Are you sure you want to exit the
application?")
if confirm:
[Link]()
global user, ID, pswrd
user = StringVar()
ID = StringVar()
pswrd = StringVar()
Label(login_window, text="ID", bg="#e6f2ff").place(x=30, y=20)
Entry(login_window, textvariable=ID).place(x=120, y=20)
Label(login_window, text="Username", bg="#e6f2ff").place(x=30, y=50)
Entry(login_window, textvariable=user).place(x=120, y=50)
Label(login_window, text="Password", bg="#e6f2ff").place(x=30, y=80)
Entry(login_window, textvariable=pswrd, show="*").place(x=120, y=80)
Button(login_window, text="LOGIN", width=10, command=checklogin).place(x=20, y=120)
Button(login_window, text="DELETE", width=10, command=clearinput).place(x=110,
y=120)
Button(login_window, text="EXIT", width=10, command=exitApp).place(x=200, y=120)
login_window.mainloop()
# =BOOKING SYSTEM=
def show_booking_system():
homestay = Tk()
[Link]("Homestay Booking System")
[Link]("750x480")
[Link](bg="#e6f2ff")
Label(homestay, text="Homestay Booking System", font=("Arial", 24, "bold")).place(x=150,
y=10)
def init_db():
conn = connect_db()
cursor = [Link]()
[Link]("""
CREATE TABLE IF NOT EXISTS bookings (
guest_id VARCHAR(10) PRIMARY KEY,
guest_name VARCHAR(100),
room_type VARCHAR(10),
nights INT,
price_per_night FLOAT,
total_price FLOAT
""")
[Link]()
[Link]()
def calculate_total():
try:
nights = int(spin_room.get().strip())
price = float(entry_price.get().strip())
total = nights * price
entry_total.config(state="normal")
entry_total.delete(0, END)
entry_total.insert(0, f"{total:.2f}")
entry_total.config(state="readonly")
except ValueError:
[Link]("Error", "Enter valid numbers for nights and price.")
def save_booking():
try:
if not entry_total.get():
[Link]("Error", "Please calculate total price before saving.")
return
conn = connect_db()
cursor = [Link]()
[Link]("INSERT INTO bookings VALUES (%s, %s, %s, %s, %s, %s)", (
entry_id.get(), entry_name.get(), combo_room.get(),
int(spin_room.get()), float(entry_price.get()), float(entry_total.get())
))
[Link]()
[Link]()
[Link]("Saved", "Booking saved successfully!")
view_booking()
except [Link]:
[Link]("Warning", "Guest ID already exists!")
except:
[Link]("Error", "Please complete all fields correctly.")
def view_booking():
for row in tree.get_children():
[Link](row)
conn = connect_db()
cursor = [Link]()
[Link]("SELECT * FROM bookings")
rows = [Link]()
for row in rows:
[Link]("", END, values=row)
[Link]()
def reset_fields():
for entry in [entry_id, entry_name, entry_price]:
[Link](0, END)
combo_room.set("")
spin_room.set("1")
entry_total.config(state="normal")
entry_total.delete(0, END)
entry_total.config(state="readonly")
def search_booking():
guest_id = entry_id.get()
if not guest_id:
[Link]("Warning", "Please enter Guest ID to search.")
return
for row in tree.get_children():
[Link](row)
conn = connect_db()
cursor = [Link]()
[Link]("SELECT * FROM bookings WHERE guest_id = %s", (guest_id,))
row = [Link]()
[Link]()
if row:
[Link]("", END, values=row)
else:
[Link]("Not Found", f"No booking found for Guest ID: {guest_id}")
def update_booking():
try:
if not entry_total.get():
[Link]("Error", "Please calculate total price before updating.")
return
guest_id = entry_id.get()
guest_name = entry_name.get()
room_type = combo_room.get()
nights = int(spin_room.get())
price_per_night = float(entry_price.get())
total_price = float(entry_total.get())
conn = connect_db()
cursor = [Link]()
[Link]("""
UPDATE bookings
SET guest_name=%s, room_type=%s, nights=%s, price_per_night=%s,
total_price=%s
WHERE guest_id=%s
""", (guest_name, room_type, nights, price_per_night, total_price, guest_id))
[Link]()
[Link]()
view_booking()
[Link]("Updated", "Booking updated successfully.")
except:
[Link]("Error", "Please fill all fields correctly for update.")
def delete_booking():
selected = [Link]()
if not selected:
[Link]("Warning", "No booking selected!")
return
values = [Link](selected, "values")
guest_id = values[0]
conn = connect_db()
cursor = [Link]()
[Link]("DELETE FROM bookings WHERE guest_id = %s", (guest_id,))
[Link]()
[Link]()
view_booking()
[Link]("Deleted", f"Booking ID {guest_id} deleted.")
Label(homestay, text="Guest ID").place(x=50, y=70)
entry_id = Entry(homestay)
entry_id.place(x=200, y=70)
Label(homestay, text="Guest Name").place(x=50, y=110)
entry_name = Entry(homestay)
entry_name.place(x=200, y=110)
Label(homestay, text="Room Type").place(x=50, y=150)
combo_room = [Link](homestay, values=["A", "B", "C"])
combo_room.place(x=200, y=150)
Label(homestay, text="Number of Nights").place(x=50, y=190)
spin_room = [Link](homestay, values=[str(i) for i in range(1, 11)])
spin_room.place(x=200, y=190)
spin_room.set("1")
Label(homestay, text="Price per Night").place(x=50, y=230)
entry_price = Entry(homestay)
entry_price.place(x=200, y=230)
Label(homestay, text="Total Price").place(x=50, y=270)
entry_total = Entry(homestay, state="readonly")
entry_total.place(x=200, y=270)
btn_color = "#4CAF50"
btn_fg = "black"
Button(homestay, text="Calculate", bg="light blue", fg=btn_fg,
command=calculate_total).place(x=420, y=270)
Button(homestay, text="Save", width=10, bg="light blue", fg=btn_fg,
command=save_booking).place(x=500, y=70)
Button(homestay, text="View", width=10, bg="light blue", fg=btn_fg,
command=view_booking).place(x=500, y=110)
Button(homestay, text="Reset", width=10, bg="#E56F56", fg=btn_fg,
command=reset_fields).place(x=500, y=150)
Button(homestay, text="Exit", width=10, bg="#E56F56", fg=btn_fg,
command=[Link]).place(x=500, y=190)
tree = [Link](homestay, columns=("ID", "Name", "Room", "Nights", "Price", "Total"),
show="headings")
for col in ["ID", "Name", "Room", "Nights", "Price", "Total"]:
[Link](col, text=col)
[Link](col, width=100)
[Link](x=50, y=320, width=600, height=120)
scrollbar = Scrollbar(homestay, orient=VERTICAL, command=[Link])
[Link](yscrollcommand=[Link])
[Link](x=650, y=320, height=120)
Button(homestay, text="Search", width=10, bg="light blue", fg=btn_fg,
command=search_booking).place(x=660, y=320)
Button(homestay, text="Update", width=10, bg="light blue", fg=btn_fg,
command=update_booking).place(x=660, y=360)
Button(homestay, text="Delete", width=10, bg="#E56F56", fg=btn_fg,
command=delete_booking).place(x=660, y=400)
init_db()
view_booking()
[Link]()
# Run the app
show_login()
Output: