Bus Booking System in tkinter

 

Bus Booking System Tkinter

 

Code:

import tkinter as tk

from tkinter import messagebox, simpledialog

 

fares = {

    ("Chennai", "Kovai"): 400,

    ("Trichy", "Salem"): 350,

    ("Salem", "Chennai"): 410,

    ("Chennai", "KanyaKumari"): 500

}

 

class BusBookingApp:

    def __init__(self, root):

        self.root = root

        self.root.title("Bus Ticket Booking System")

 

        tk.Label(root, text="Available Routes:").grid(row=0, column=0, columnspan=3, pady=5)

        row_index = 1

        for route, price in fares.items():

            tk.Label(root, text=f"{route[0]} → {route[1]} : ₹{price}").grid(row=row_index, column=0, columnspan=3)

            row_index += 1

 

        # Source input

        tk.Label(root, text="Source:").grid(row=row_index, column=0, pady=5, sticky="e")

        self.source_var = tk.StringVar()

        tk.Entry(root, textvariable=self.source_var).grid(row=row_index, column=1, pady=5)

 

        # Destination input

        row_index += 1

        tk.Label(root, text="Destination:").grid(row=row_index, column=0, pady=5, sticky="e")

        self.dest_var = tk.StringVar()

        tk.Entry(root, textvariable=self.dest_var).grid(row=row_index, column=1, pady=5)

 

        # Number of seats input

        row_index += 1

        tk.Label(root, text="Number of seats:").grid(row=row_index, column=0, pady=5, sticky="e")

        self.seats_var = tk.IntVar(value=1)

        tk.Entry(root, textvariable=self.seats_var).grid(row=row_index, column=1, pady=5)

 

        # Button to enter passenger details

        row_index += 1

        self.passenger_button = tk.Button(root, text="Enter Passenger Details", command=self.get_passenger_details)

        self.passenger_button.grid(row=row_index, column=0, columnspan=2, pady=10)

 

        # Button to book ticket

        row_index += 1

        self.book_button = tk.Button(root, text="Book Ticket", command=self.book_ticket)

        self.book_button.grid(row=row_index, column=0, columnspan=2, pady=10)

 

        # Passenger details storage

        self.passengers = []

 

    def get_passenger_details(self):

        try:

            num_seats = self.seats_var.get()

            if num_seats <= 0:

                messagebox.showerror("Error", "Number of seats must be positive")

                return

        except Exception:

            messagebox.showerror("Error", "Please enter a valid number of seats")

            return

 

        self.passengers = []

        for i in range(num_seats):

            name = simpledialog.askstring("Passenger", f"Enter name for Passenger {i+1}")

            if not name:

                messagebox.showerror("Error", "Name is required")

                return

            age = simpledialog.askinteger("Passenger", f"Enter age for Passenger {i+1}")

            if age is None or age <= 0:

                messagebox.showerror("Error", "Valid age is required")

                return

            gender = simpledialog.askstring("Passenger", f"Enter gender (M/F) for Passenger {i+1}")

            if gender not in ('M', 'F', 'm', 'f'):

                messagebox.showerror("Error", "Gender must be M or F")

                return

            self.passengers.append({"Name": name, "Age": age, "Gender": gender.upper()})

 

        messagebox.showinfo("Success", "Passenger details saved")

 

    def book_ticket(self):

        source = self.source_var.get()

        destination = self.dest_var.get()

        if not source or not destination:

            messagebox.showerror("Error", "Source and Destination cannot be empty")

            return

 

        if (source, destination) not in fares:

            messagebox.showerror("Error", "Sorry, no bus available for this route.")

            return

 

        price_per_seat = fares[(source, destination)]

 

        num_seats = self.seats_var.get()

        if num_seats <= 0:

            messagebox.showerror("Error", "Number of seats must be positive")

            return

 

        if len(self.passengers) != num_seats:

            messagebox.showerror("Error", "Please enter passenger details for all seats")

            return

 

        total_price = num_seats * price_per_seat

 

        # Payment method

        payment_window = tk.Toplevel(self.root)

        payment_window.title("Payment Method")

 

        tk.Label(payment_window, text="Choose Payment Method:").pack(pady=5)

        payment_var = tk.IntVar()

 

        def on_payment_choice():

            choice = payment_var.get()

            if choice == 1:

                card_no = simpledialog.askstring("Credit Card", "Enter Card Number:")

                if not card_no or len(card_no) < 4:

                    messagebox.showerror("Error", "Invalid card number.")

                    return

                messagebox.showinfo("Payment", f"Payment successful using Credit Card ending with {card_no[-4:]}")

            elif choice == 2:

                upi_id = simpledialog.askstring("UPI", "Enter UPI ID:")

                if not upi_id:

                    messagebox.showerror("Error", "Invalid UPI ID.")

                    return

                messagebox.showinfo("Payment", f"Payment successful using UPI ID: {upi_id}")

            elif choice == 3:

                messagebox.showinfo("Payment", "Payment received in Cash.")

            else:

                messagebox.showerror("Error", "Invalid choice. Booking failed!")

                payment_window.destroy()

                return

 

            payment_window.destroy()

            self.show_confirmation(source, destination, num_seats, price_per_seat, total_price)

 

        tk.Radiobutton(payment_window, text="Credit Card", variable=payment_var, value=1).pack(anchor='w')

        tk.Radiobutton(payment_window, text="UPI", variable=payment_var, value=2).pack(anchor='w')

        tk.Radiobutton(payment_window, text="Cash", variable=payment_var, value=3).pack(anchor='w')

        tk.Button(payment_window, text="Pay", command=on_payment_choice).pack(pady=10)

 

    def show_confirmation(self, source, destination, num_seats, price_per_seat, total_price):

        confirmation_text = (

            f"=== Booking Confirmed ===\n"

            f"From: {source} → To: {destination}\n"

            f"Total Seats: {num_seats}\n"

            f"Fare per Seat: ₹{price_per_seat}\n"

            f"Total Fare: ₹{total_price}\n\n"

            f"Passenger Details:\n"

        )

        for p in self.passengers:

            confirmation_text += f"- {p['Name']} ({p['Age']} yrs, {p['Gender']})\n"

        confirmation_text += "\nHave a safe journey!"

 

        messagebox.showinfo("Booking Confirmation", confirmation_text)

 

 

if __name__ == "__main__":

    root = tk.Tk()

    app = BusBookingApp(root)

    root.mainloop()

 

Output:

 

 

 

 

 

Comments

Popular posts from this blog

Bus Booking System