Advertisement
imroz229

Untitled

Apr 18th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 19.29 KB | Source Code | 0 0
  1. from tkinter import *
  2. from PIL import ImageTk, Image
  3. from tkinter import messagebox
  4. from tkcalendar import Calendar
  5. import connection
  6. import subprocess
  7.  
  8.  
  9. class LoginForm:
  10.     def __init__(self, window):
  11.         self.window = window
  12.         self.window.geometry('1200x628')
  13.         self.window.resizable(0, 0)
  14.         self.window.state('zoomed')
  15.  
  16.  
  17.         # Background image
  18.         self.bg_frame = Image.open('images\\background1.png')
  19.         photo = ImageTk.PhotoImage(self.bg_frame)
  20.         self.bg_panel = Label(self.window, image=photo)
  21.         self.bg_panel.image = photo
  22.         self.bg_panel.pack(fill='both', expand='yes')
  23.  
  24.         # Login frame
  25.         self.lgn_frame = Frame(self.window, bg='black', width='843', height='777')
  26.         self.lgn_frame.place(x=300, y=25)
  27.         self.txt = 'WELCOME'
  28.         self.heading = Label(self.lgn_frame, text=self.txt, font=('yu gothic ui', 25, 'bold'), bg='black', fg='white')
  29.         self.heading.place(x=80, y=30, width=300, height=30)
  30.  
  31.         # Left side image
  32.         self.side_image = Image.open('images\\trainnobg.jpg')
  33.         photo = ImageTk.PhotoImage(self.side_image)
  34.         self.side_image_label = Label(self.lgn_frame, image=photo, bg='#040405')
  35.         self.side_image_label.image = photo
  36.         self.side_image_label.place(x=-5, y=-159, width=600, height=900)
  37.  
  38.         # Sign in image
  39.         self.sign_in_image = Image.open('images\\irctclogo.png')
  40.         photo = ImageTk.PhotoImage(self.sign_in_image)
  41.         self.sign_in_image_label = Label(self.lgn_frame, image=photo, bg='black')
  42.         self.sign_in_image_label.image = photo
  43.         self.sign_in_image_label.place(x=750, y=75)
  44.  
  45.         # Book Ticket label
  46.         self.sign_in_image_label = Label(self.lgn_frame, text='Book Ticket', bg='#040405', fg='white', font=('yu gothic ui', 13, 'bold'))
  47.         self.sign_in_image_label.place(x=663, y=300)
  48.  
  49.         # From location
  50.         self.username_label = Label(self.lgn_frame, text='From: ', bg='#040405', font=('yu gothic ui', 13, 'bold'), fg='white')
  51.         self.username_label.place(x=600, y=350)
  52.         self.from_entry = Entry(self.lgn_frame, highlightthickness=0, relief=FLAT, bg='#040405', fg='white', font=('yu gothic ui', 13, 'bold'))
  53.         self.from_entry.place(x=685, y=350, width=270)
  54.         self.username_line = Canvas(self.lgn_frame, width=300, height=2.0, bg='#bdb9b1', highlightthickness=0)
  55.         self.username_line.place(x=600, y=375)
  56.  
  57.         # To location
  58.         self.username_label = Label(self.lgn_frame, text='To: ', bg='#040405', font=('yu gothic ui', 13, 'bold'), fg='white')
  59.         self.username_label.place(x=600, y=450)
  60.         self.to_entry = Entry(self.lgn_frame, highlightthickness=0, relief=FLAT, bg='#040405', fg='white', font=('yu gothic ui', 13, 'bold'))
  61.         self.to_entry.place(x=685, y=450, width=270)
  62.         self.username_line = Canvas(self.lgn_frame, width=300, height=2.0, bg='#bdb9b1', highlightthickness=0)
  63.         self.username_line.place(x=600, y=475)
  64.  
  65.         # Seats
  66.         self.username_label = Label(self.lgn_frame, text='Seats: ', bg='#040405', font=('yu gothic ui', 13, 'bold'), fg='white')
  67.         self.username_label.place(x=600, y=520)
  68.         self.seats_entry = Entry(self.lgn_frame, highlightthickness=0, relief=FLAT, bg='#040405', fg='white', font=('yu gothic ui', 13, 'bold'))
  69.         self.seats_entry.place(x=685, y=520, width=270)
  70.  
  71.         # Scrollbar
  72.         scrollbar_frame = Frame(self.lgn_frame, bg='#040405')
  73.         scrollbar_frame.place(x=600, y=575, height=100)
  74.  
  75.         scrollbar_canvas = Canvas(scrollbar_frame, bg='#040405', highlightthickness=0)
  76.         scrollbar_canvas.pack(side=LEFT, fill=BOTH, expand=True)
  77.  
  78.         scrollbar = Scrollbar(scrollbar_frame, orient=VERTICAL, command=scrollbar_canvas.yview)
  79.         scrollbar.pack(side=RIGHT, fill=Y)
  80.  
  81.         options = ['General', 'Ladies', 'Lower Berth', 'Person with Disability', 'Tatkal', 'Duty Pass']
  82.         self.option_listbox = Listbox(scrollbar_canvas, bg='#040405', fg='white', bd=0, highlightthickness=0, font=('yu gothic ui', 12, 'bold'))
  83.  
  84.         self.username_line = Canvas(self.lgn_frame, width=300, height=2.0, bg='#bdb9b1', highlightthickness=0)
  85.         self.username_line.place(x=600, y=550)
  86.  
  87.         for option in options:
  88.             self.option_listbox.insert(END, option)
  89.         self.option_listbox.pack(side=LEFT, fill=BOTH, expand=True)
  90.  
  91.         self.option_listbox.config(yscrollcommand=scrollbar.set)
  92.  
  93.         # Label to display selected item
  94.         self.selected_label = Label(self.lgn_frame, text="", bg="#040405", fg="white",
  95.                                     font=('yu gothic ui', 12, 'bold'))
  96.         self.selected_label.place(x=680, y=520)
  97.  
  98.         # Bind listbox select event
  99.         self.option_listbox.bind("<<ListboxSelect>>", self.on_select)
  100.  
  101.         # Date
  102.         self.date_label = Label(self.lgn_frame, text='Date: ', bg='#040405', font=('yu gothic ui', 13, 'bold'), fg='white')
  103.         self.date_label.place(x=315, y=500)
  104.         self.username_line = Canvas(self.lgn_frame, width=250, height=2.0, bg='#bdb9b1', highlightthickness=0)
  105.         self.username_line.place(x=315, y=545)
  106.  
  107.         self.cal = Calendar(self.lgn_frame, selectmode="day", year=2024, month=2, day=18, background='#040405',
  108.                             foreground='white', headersbackground='#040405', headersforeground='white')
  109.         self.cal.place(x=300, y=570, width=270)
  110.  
  111.         # Button to get selected date
  112.         self.get_date_button = Button(self.lgn_frame, text="Get Date", command=self.get_date, bg='#040405', fg='white',
  113.                                       font=('yu gothic ui', 13, 'bold'))
  114.         self.get_date_button.place(x=390, y=502, height=25, width=75)
  115.  
  116.         # Label to display selected date
  117.         self.selected_date_label = Label(self.lgn_frame, text="", bg="#040405", fg="white", font=('yu gothic ui', 12, 'bold'))
  118.         self.selected_date_label.place(x=480, y=500)
  119.  
  120.         # Search button
  121.         self.search_button = Button(self.lgn_frame, text="Search", bg='red', fg='white', font=('yu gothic ui', 13, 'bold'), command=self.search_trains_wrapper, cursor='hand2')
  122.         self.search_button.place(x=600, y=695, height=30, width=100)
  123.  
  124.     def execute_signin_script(self):
  125.         subprocess.Popen(['python', 'signin.py'])
  126.  
  127.     def get_date(self):
  128.         selected_date = self.cal.get_date()
  129.         self.selected_date_label.config(text=selected_date)
  130.  
  131.     def on_select(self, event):
  132.         selection = event.widget.curselection()
  133.         if selection:
  134.             index = selection[0]
  135.             data = event.widget.get(index)
  136.             self.selected_label.config(text=data)
  137.  
  138.     def fetch_available_dates(self, ac_tier):
  139.  
  140.         available_dates = [f"2024-03-{day}" for day in range(20, 31)]
  141.         return available_dates
  142.  
  143.     def display_available_dates(self, ac_tier):
  144.         available_dates = self.fetch_available_dates(ac_tier)
  145.  
  146.         dates_window = Toplevel(self.window)
  147.         dates_window.title(f"Available Dates for {ac_tier}")
  148.         dates_window.geometry('1500x400')
  149.  
  150.         dates_frame = Frame(dates_window, bg='#040405')
  151.         dates_frame.pack(fill='both', expand=True)
  152.  
  153.         dates_label = Label(dates_frame, text=f"Available Dates for {ac_tier}", bg='#040405', fg='white', font=('yu gothic ui', 16, 'bold'))
  154.         dates_label.pack(pady=10)
  155.  
  156.         # Create a horizontal scrollbar to display dates
  157.         dates_canvas = Canvas(dates_frame, bg='#040405', highlightthickness=0)
  158.         dates_canvas.pack(side=TOP, fill=BOTH, expand=True)
  159.  
  160.         scrollbar = Scrollbar(dates_frame, orient=HORIZONTAL, command=dates_canvas.xview)
  161.         scrollbar.pack(side=BOTTOM, fill=X)
  162.  
  163.         dates_canvas.configure(xscrollcommand=scrollbar.set)
  164.  
  165.         dates_frame_inner = Frame(dates_canvas, bg='#040405')
  166.         dates_canvas.create_window((0, 0), window=dates_frame_inner, anchor='nw')
  167.  
  168.         # Add date buttons to the frame
  169.         for i, date in enumerate(available_dates):
  170.             date_button = Button(dates_frame_inner, text=date, bg='red', fg='white', font=('yu gothic ui', 12, 'bold'),
  171.                                  command=lambda d=date: self.display_available_seats(ac_tier, d))
  172.             date_button.grid(row=0, column=i, padx=5, pady=5)
  173.  
  174.         # Update scroll region
  175.         dates_frame_inner.update_idletasks()
  176.         dates_canvas.config(scrollregion=dates_canvas.bbox("all"))
  177.  
  178.     def display_available_seats(self, ac_tier, selected_date):
  179.         available_seats = self.fetch_available_seats(ac_tier, selected_date)
  180.  
  181.         seats_window = Toplevel(self.window)
  182.         seats_window.title(f"Available Seats for {ac_tier} on {selected_date}")
  183.         seats_window.geometry('500x400')
  184.  
  185.         seats_frame = Frame(seats_window, bg='#040405')
  186.         seats_frame.pack(fill='both', expand=True)
  187.  
  188.         seats_label = Label(seats_frame, text=f"Available Seats for {ac_tier} on {selected_date}", bg='#040405',
  189.                             fg='white', font=('yu gothic ui', 16, 'bold'))
  190.         seats_label.pack(pady=10)
  191.  
  192.         if available_seats:
  193.             for i, seat_type in enumerate(available_seats):
  194.                 seat_button = Button(seats_frame, text=seat_type, bg='red', fg='white',
  195.                                      font=('yu gothic ui', 12, 'bold'))
  196.                 seat_button.pack(pady=5)
  197.         else:
  198.             no_seats_label = Label(seats_frame, text="No seats available for the selected date.", bg='#040405',
  199.                                    fg='white', font=('yu gothic ui', 12))
  200.             no_seats_label.pack(pady=10)
  201.  
  202.  
  203.     def search_trains_wrapper(self):
  204.         from_location = self.from_entry.get()
  205.         to_location = self.to_entry.get()
  206.         date = self.cal.get_date()
  207.  
  208.         if not (from_location and to_location and date):
  209.             # If any of the fields are empty, display a message
  210.             messagebox.showerror("Error", "Please fill in all the required fields.")
  211.             return
  212.  
  213.         # Call search_trains function from connection module
  214.         results = connection.search_trains(from_location, to_location, date)
  215.  
  216.         # Display search results
  217.         if results:
  218.             self.display_search_results(results)
  219.         else:
  220.             messagebox.showinfo("No Results", "No trains found for the given criteria.")
  221.  
  222.  
  223.     def display_search_results(self, results):
  224.         # Function to display search results
  225.         # You can update this method to display the results in a separate window or in the current window
  226.         results_window = Toplevel(self.window)
  227.         results_window.title("Search Results")
  228.         results_window.geometry('800x600')
  229.  
  230.         # Create a frame to hold the results
  231.         results_frame = Frame(results_window, bg='#040405')
  232.         results_frame.pack(fill='both', expand=True)
  233.  
  234.         # Add a label for the heading
  235.         heading_label = Label(results_frame, text="Search Results", bg='#040405', fg='white',
  236.                               font=('yu gothic ui', 18, 'bold'))
  237.         heading_label.pack(pady=10)
  238.  
  239.         if results:
  240.             # Iterate over the results and display them
  241.             for result in results:
  242.                 train_frame = Frame(results_window, bg='#040405')
  243.                 train_frame.pack(fill='both', expand=True)
  244.                 result_label = Label(results_frame, text=result, bg='#040405', fg='white', font=('yu gothic ui', 12))
  245.                 result_label.pack(pady=15, padx=10)
  246.  
  247.         else:
  248.             # If no results found, display a message
  249.             no_results_label = Label(results_frame, text="No trains found for the given criteria.", bg='#040405',
  250.                                      fg='white', font=('yu gothic ui', 12))
  251.             no_results_label.pack(pady=10)
  252.  
  253.         close_button = Button(results_frame, text="Close", command=results_window.destroy, bg='red', fg='white',font=('yu gothic ui', 12, 'bold'),cursor='hand2')
  254.         close_button.place(x=350, y=380)
  255.  
  256.         ACTier1A = Button(results_frame, text="AC Tier 1", bg='red', fg='white', font=('yu gothic ui', 12, 'bold'), command=lambda: self.display_available_dates("AC Tier 1"), cursor='hand2')
  257.         ACTier1A.place(x=200, y=110)
  258.         ACTier2A = Button(results_frame, text="AC Tier 2", bg='red', fg='white', font=('yu gothic ui', 12, 'bold'), command=lambda: self.display_available_dates("AC Tier 2"), cursor='hand2')
  259.         ACTier2A.place(x=350, y=110)
  260.  
  261.         ACTier1A = Button(results_frame, text="AC Tier 1", bg='red', fg='white', font=('yu gothic ui', 12, 'bold'), command=lambda: self.display_available_dates("AC Tier 1"), cursor='hand2')
  262.         ACTier1A.place(x=200, y=190)
  263.         ACTier2A = Button(results_frame, text="AC Tier 2", bg='red', fg='white', font=('yu gothic ui', 12, 'bold'), command=lambda: self.display_available_dates("AC Tier 2"), cursor='hand2')
  264.         ACTier2A.place(x=350, y=190)
  265.  
  266.  
  267.     def fetch_available_dates(self, ac_tier):
  268.         available_dates = [f"2024-04-{day}" for day in range(1, 31)]  # Mocked available dates
  269.         return available_dates
  270.  
  271.     def fetch_available_seats(self, ac_tier, selected_date):
  272.         # Mocking the seat types based on selected_date and AC tier
  273.         seat_types = {
  274.             "2024-04-1": ["A-12", "B-32", "B-05", "C-34", "C-42"],
  275.             "2024-04-2": ["A-21", "B-42", "B-45", "C-44", "C-45"],
  276.             "2024-04-3": ["A-21", "B-42", "B-65", "C-44", "C-45"],
  277.             "2024-04-4": ["A-21", "B-42", "B-25", "C-44", "C-45"],
  278.             "2024-04-5": ["A-21", "B-42", "B-15", "C-44", "C-45"],
  279.             "2024-04-6": ["A-21", "B-42", "B-05", "C-44", "C-45"],
  280.             "2024-04-7": ["A-21", "B-42", "B-35", "C-44", "C-45", "A-32",],
  281.             "2024-04-8": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  282.             "2024-04-9": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  283.             "2024-04-10": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  284.             "2024-04-11": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  285.             "2024-04-12": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  286.             "2024-04-13": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  287.             "2024-04-14": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  288.             "2024-04-15": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  289.             "2024-04-16": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  290.             "2024-04-17": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  291.             "2024-04-18": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  292.             "2024-04-19": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  293.             "2024-04-20": ["A-21", "B-42", "B-35", "C-44", "C-45"],
  294.             "2024-04-21": ["A-26", "A-27", "B-28", "B-29"],
  295.             "2024-04-22": ["A-30", "A-31", "B-32", "D-11"],
  296.             "2024-04-23": ["A-33", "A-34", "B-35", "C-36", "C-37", "D-12"],
  297.             "2024-04-24": ["A-38", "A-39", "B-40", "C-42", "C-43", "C-44", "D-22"],
  298.             "2024-04-25": ["A-40", "A-41", "B-42", "C-42", "C-46", "C-44", "D-11"],
  299.             "2024-04-26": ["B-42", "B-43", "B-44", "C-11"],
  300.             "2024-04-27": ["A-11", "B-42", "C-42", "B-19"],
  301.             "2024-04-28": ["A-21", "B-42", "C-42", "B-20", "C-20", "D-12"],
  302.             "2024-04-29": ["A-32, B-26", "C-38", "B-7"],
  303.             "2024-04-30": ["A-33", "C-39", "B-40", "C-45,A-41"],
  304.             "2024-04-31": ["A-12", "B-42", "C-42", "B-34", "C-44", "C-44", "D-21"]
  305.         }
  306.  
  307.         return seat_types.get(selected_date, [])
  308.  
  309.     def seat_type_selected(self, seat_type):
  310.         # Function to display a confirmation message when a seat type button is clicked
  311.         confirmation_window = Toplevel(self.window)
  312.         confirmation_window.title("Ticket Booking Confirmation")
  313.         confirmation_window.geometry('400x200')
  314.  
  315.         confirmation_frame = Frame(confirmation_window, bg='#040405')
  316.         confirmation_frame.pack(fill='both', expand=True)
  317.  
  318.         confirmation_label = Label(confirmation_frame, text=f"Your ticket for {seat_type} is booked!", bg='#040405',
  319.                                    fg='white', font=('yu gothic ui', 16, 'bold'))
  320.         confirmation_label.pack(pady=50)
  321.  
  322.         close_button = Button(confirmation_frame, text="Close", command=confirmation_window.destroy, bg='red',
  323.                               fg='white',
  324.                               font=('yu gothic ui', 12, 'bold'), cursor='hand2')
  325.         close_button.pack()
  326.  
  327.  
  328.  
  329.     def display_available_dates(self, ac_tier):
  330.         available_dates = self.fetch_available_dates(ac_tier)
  331.  
  332.         dates_window = Toplevel(self.window)
  333.         dates_window.title(f"Available Dates for {ac_tier}")
  334.         dates_window.geometry('1500x400')
  335.  
  336.         dates_frame = Frame(dates_window, bg='#040405')
  337.         dates_frame.pack(fill='both', expand=True)
  338.  
  339.         dates_label = Label(dates_frame, text=f"Available Dates for {ac_tier}", bg='#040405', fg='white',
  340.                             font=('yu gothic ui', 16, 'bold'))
  341.         dates_label.pack(pady=10)
  342.  
  343.         # Create a horizontal scrollbar to display dates
  344.         dates_canvas = Canvas(dates_frame, bg='#040405', highlightthickness=0)
  345.         dates_canvas.pack(side=TOP, fill=BOTH, expand=True)
  346.  
  347.         scrollbar = Scrollbar(dates_frame, orient=HORIZONTAL, command=dates_canvas.xview)
  348.         scrollbar.pack(side=BOTTOM, fill=X)
  349.  
  350.         dates_canvas.configure(xscrollcommand=scrollbar.set)
  351.  
  352.         dates_frame_inner = Frame(dates_canvas, bg='#040405')
  353.         dates_canvas.create_window((0, 0), window=dates_frame_inner, anchor='nw')
  354.  
  355.         # Add date buttons to the frame
  356.         for i, date in enumerate(available_dates):
  357.             date_button = Button(dates_frame_inner, text=date, bg='red', fg='white', font=('yu gothic ui', 12, 'bold'),
  358.                                  command=lambda d=date: self.display_available_seats(ac_tier, d))
  359.             date_button.grid(row=0, column=i, padx=5, pady=5)
  360.  
  361.         # Update scroll region
  362.         dates_frame_inner.update_idletasks()
  363.         dates_canvas.config(scrollregion=dates_canvas.bbox("all"))
  364.  
  365.     def display_available_seats(self, ac_tier, selected_date):
  366.         available_seats = self.fetch_available_seats(ac_tier, selected_date)
  367.  
  368.         seats_window = Toplevel(self.window)
  369.         seats_window.title(f"Available Seats for {ac_tier} on {selected_date}")
  370.         seats_window.geometry('500x400')
  371.  
  372.         seats_frame = Frame(seats_window, bg='#040405')
  373.         seats_frame.pack(fill='both', expand=True)
  374.  
  375.         seats_label = Label(seats_frame, text=f"Available Seats for {ac_tier} ", bg='#040405',
  376.                             fg='white', font=('yu gothic ui', 16, 'bold'))
  377.         seats_label.pack(pady=10)
  378.  
  379.         # Inside display_available_seats method
  380.         if available_seats:
  381.             for i, seat_type in enumerate(available_seats):
  382.                 seat_button = Button(seats_frame, text=seat_type, bg='red', fg='white',
  383.                                      font=('yu gothic ui', 12, 'bold'))
  384.                 seat_button.config(command=lambda st=seat_type, : self.seat_type_selected(st))  # Bind seat type to method
  385.                 seat_button.pack(pady=5)
  386.         else:
  387.             no_seats_label = Label(seats_frame, text="No seats available for the selected date.", bg='#040405',
  388.                                    fg='white', font=('yu gothic ui', 12))
  389.             no_seats_label.pack(pady=10)
  390.  
  391.  
  392. def page():
  393.     window = Tk()
  394.     LoginForm(window)
  395.     window.mainloop()
  396.  
  397. if __name__ == '__main__':
  398.     page()
  399.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement