· 2 years ago · Jul 01, 2023, 05:45 PM
1import tkinter as tk
2from tkinter import ttk
3from tkinter import messagebox
4import consumer
5from PIL import ImageTk, Image
6
7connection = None
8while connection is None:
9 connection = consumer.connect_to_mysql()
10cur = connection.cursor(buffered=True)
11
12
13def get_usr_pwd():
14 usr = username.get()
15 pwd = password.get()
16 if consumer.login_check(usr, pwd, cur) == True:
17 success_label = tk.Label(login_frame, text="Success")
18 success_label.place(x=550, y=250)
19 login_frame.forget()
20 menu_frame.place(x=0, y=0)
21 menu_frame.config(height=400, width=800)
22 else:
23 fail_label = tk.Label(login_frame, text="Fail.Try Again")
24 messagebox.showinfo(title="Error", message="Wrong credentials, please try again")
25 fail_label.place(x=550, y=220)
26
27def flight_table():
28 flight_table = tk.Tk()
29 cur.execute("SELECT * FROM flight_details")
30 data = cur.fetchall()
31 table = ttk.Treeview(flight_table)
32 columns = ("Flight Name", "Flight Number", "Number of Seats", "Date", "Start", "Boarding Time", "Route", "Destination")
33 table['columns'] = columns
34 table['show'] = 'headings'
35 for col in columns:
36 table.heading(col, text=col)
37 for row in data:
38 table.insert('', 'end', values=row)
39 scrollbar = ttk.Scrollbar(flight_table, orient='vertical', command=table.yview)
40 table.configure(yscroll=scrollbar.set)
41 table.pack(side='left', fill='both', expand=True)
42 scrollbar.pack(side='right', fill='y')
43 flight_table.mainloop()
44
45def ticket_table():
46 cur.execute(f"SELECT * FROM booked_tickets where ticket_name = '{username.get()}'")
47 if cur.rowcount > 0:
48 ticket_table = tk.Tk()
49 data = cur.fetchall()
50 table = ttk.Treeview(ticket_table)
51 columns = ("Ticket No", "Ticket Name", "Boarding Point", "Destination", "Time")
52 table['columns'] = columns
53 table['show'] = 'headings'
54 for col in columns:
55 table.heading(col, text=col)
56 for row in data:
57 table.insert('', 'end', values=row)
58 scrollbar = ttk.Scrollbar(ticket_table, orient='vertical', command=table.yview)
59 table.configure(yscroll=scrollbar.set)
60 table.pack(side='left', fill='both', expand=True)
61 scrollbar.pack(side='right', fill='y')
62 ticket_table.mainloop()
63 else:
64 messagebox.showerror("Error","No Data")
65
66def ticket_booking_ui():
67 ticket_booking_ui = tk.Tk()
68 ticket_booking_ui.geometry("800x400")
69 ticket_booking_ui.title("Book Tickets")
70
71
72 d = tk.StringVar()
73 ttk.Label(ticket_booking_ui, text = "Select Boarding Point :",
74 font = ("Times New Roman", 10)).grid(column = 0,
75 row = 0, padx = 10, pady = 25)
76 startchosen = ttk.Combobox(ticket_booking_ui, width = 15,
77 textvariable = d)
78 cur.execute("SELECT DISTINCT Start from flight_details")
79 start_list = [row[0] for row in cur]
80 startchosen["values"] = start_list
81 startchosen.grid(row=1,column=0,padx = 10, pady = 5)
82
83 m = tk.StringVar()
84 ttk.Label(ticket_booking_ui, text = "Select Destination :",
85 font = ("Times New Roman", 10)).grid(column = 1,
86 row = 0, padx = 10, pady = 25)
87 endchosen = ttk.Combobox(ticket_booking_ui, width = 27,
88 textvariable = m)
89 cur.execute("SELECT DISTINCT Destination from flight_details")
90 end_list = [row[0] for row in cur]
91 endchosen['values'] = end_list
92 endchosen.grid(column = 1, row = 1)
93
94
95
96 ttk.Label(ticket_booking_ui, text=" Flight Number : ",font = ("Times New Roman", 10)).grid(row = 0,column=3)
97 fname = tk.StringVar()
98 flightchosen = ttk.Combobox(ticket_booking_ui,width = 15,textvariable=fname)
99 flightchosen.grid(row=1,column=3)
100
101 ttk.Label(ticket_booking_ui, text=" Flight Details ",font = ("Times New Roman", 10)).grid(row = 2,column=3)
102 flight_details = tk.Text(ticket_booking_ui,height=10,width=30)
103 flight_details.grid(row=3,column=3)
104
105
106
107 def get_flights():
108 start = startchosen.get()
109 end = endchosen.get()
110 query = f"SELECT Flight_Number FROM flight_details WHERE Start = '{start}' and Destination = '{end}'"
111 cur.execute(query)
112 #exists = cur.fetchone()
113 flight_details_data = cur.fetchall()
114 if cur.rowcount > 0:
115 flight_nos = []
116 for row in flight_details_data:
117 flight_nos.append(row[0])
118
119 flightchosen["values"]=flight_nos
120 messagebox.showinfo("Success","Flights Found, Please Check Available Flights")
121 else:
122 messagebox.showerror(title="Error",message="No Flights on Selected Date")
123
124 def get_flight_details():
125 flight_no = flightchosen.get()
126 if len(flight_no) > 0:
127 query = f'SELECT * FROM flight_details WHERE Flight_Number = "{flight_no}"'
128 cur.execute(query)
129 flight_details_data = cur.fetchone()
130 details = f'''Flight Name : {flight_details_data[0]}\nSeats Available : {flight_details_data[2]}\nDate : {flight_details_data[3]}\nStart : {flight_details_data[4]}\nBoarding Time : {flight_details_data[5]}\nRoute Type : {flight_details_data[6]}\nDestination : {flight_details_data[7]}\n'''
131 flight_details.insert(tk.END,details)
132
133 def book_ticket():
134 res = messagebox.askquestion("Confirmation","Would you like to book a ticket for this flight?")
135 if res == 'yes':
136 cur.execute('SELECT MAX(ticket_no) FROM booked_tickets') # for generating ticket numbers in an ordered manner
137 h = cur.fetchone()
138 if h[0]: # if there are 1 or more rows
139 h = h[0] + 1 # since h[0] would be the max ticket number, h+1 would be the new ticket number
140 else: # if there are no rows
141 h = 10000
142 cur.execute(f'INSERT INTO booked_tickets VALUES({h},"{username.get()}","{flight_details_data[4]}","{flight_details_data[7]}","{flight_details_data[5]}","{flight_details_data[1]}","{flight_details_data[3]}")')
143 cur.execute(f'UPDATE flight_details SET Number_Of_Seats = Number_Of_Seats-1 WHERE Flight_Number = "{flight_details_data[1]}" ')
144 messagebox.showinfo("Success","Ticket Booked Succesfully")
145 connection.commit()
146
147 book_button = tk.Button(ticket_booking_ui, text="Book Ticket",command=book_ticket,height=1,width=25)
148 book_button.grid(row=5,column=3)
149
150 else:
151 messagebox.showerror(title="Error",message="No Flights Selected")
152
153
154
155 date_button = tk.Button(ticket_booking_ui,text = "Get Flights",command = get_flights,height=1, width=25)
156 date_button.grid(row = 2, column=1)
157 flight_button = tk.Button(ticket_booking_ui,text = "Get Flight Details",command = get_flight_details,height=1, width=25)
158 flight_button.grid(row=4,column=3)
159
160def request_cancellation():
161 cur.execute(f"SELECT ticket_no FROM booked_tickets WHERE ticket_name = '{username.get()}' ")
162 if cur.rowcount > 0:
163 request_ui = tk.Tk()
164 request_ui.geometry("400x400")
165 request_ui.title("Request Cancellation")
166 ticket_list = [row[0] for row in cur]
167 ttk.Label(request_ui, text="Select Ticket ",font = ("Times New Roman", 10)).grid(row = 0,column=0,padx=5,pady=5)
168 ticketchosen = ttk.Combobox(request_ui,width = 15)
169 ticketchosen["values"] = ticket_list
170 ticketchosen.grid(row=1,column=0,padx=5,pady=5)
171 ticket_details = tk.Text(request_ui,height=10,width=30)
172 ticket_details.grid(row=2,column=0)
173 def confirm_ticket_no():
174 ticket_no = ticketchosen.get()
175 cur.execute(f"SELECT * FROM booked_tickets WHERE ticket_no = {ticket_no}")
176 data = cur.fetchone()
177 details = f'Ticket Name : {data[1]}\nBoarding Point : {data[2]}\nDestination : {data[3]}\nBoarding Time : {data[4]}\nFlight Number : {data[5]}\n'
178 ticket_details.insert(tk.END,details)
179 def cancel_ticket_no():
180 res = messagebox.askquestion("Confirmation","Would you like to request cancellation for this ticket?")
181 if res == 'yes':
182 cur.execute('SELECT MAX(request_id) FROM requests') # for generating ticket numbers in an ordered manner
183 h = cur.fetchone()
184 if h[0]: # if there are 1 or more rows
185 h = h[0] + 1 # since h[0] would be the max ticket number, h+1 would be the new ticket number
186 else: # if there are no rows
187 h = 100
188 cur.execute(f"INSERT INTO requests(request_id,ticket_no,ticket_name,flight_no) VALUES({h},{ticket_no},'{data[1]}','{data[5]}')")
189 connection.commit()
190 messagebox.showinfo("Success","Request Sent Succesfully")
191 cancel_ticket = tk.Button(request_ui,text="Cancel Ticket",height=1,width=25,command=cancel_ticket_no)
192 cancel_ticket.grid(row=4,column=0,padx=5,pady=5)
193 confirm_ticket = tk.Button(request_ui,text = 'Confirm Ticket',command =confirm_ticket_no,height=1,width=25)
194 confirm_ticket.grid(row=3,column=0,padx=5,pady=5)
195 else:
196 messagebox.showerror("Error","No Tickets Booked")
197
198def request_table():
199 cur.execute(f"SELECT * from requests where ticket_name ='{username.get()}'")
200 if cur.rowcount > 0:
201 request_table = tk.Tk()
202 request_table.title("Current Requests")
203 data = cur.fetchall()
204 table = ttk.Treeview(request_table)
205 columns = ("Request ID","Ticket No","Ticket Name","Flight Number","Status")
206 table['columns'] = columns
207 table['show'] = 'headings'
208 for col in columns:
209 table.heading(col, text=col)
210 for row in data:
211 table.insert('', 'end', values=row)
212 scrollbar = ttk.Scrollbar(request_table, orient='vertical', command=table.yview)
213 table.configure(yscroll=scrollbar.set)
214 table.pack(side='left', fill='both', expand=True)
215 scrollbar.pack(side='right', fill='y')
216 request_table.mainloop()
217 else:
218 messagebox.showerror("Error","No Data")
219
220def sign_up():
221 def create_account():
222 username = username_entry.get()
223 password = password_entry.get()
224 confirm_password = confirm_password_entry.get()
225
226 if password == confirm_password:
227 query = f"INSERT INTO consumer (username, password) VALUES ('{username}', '{password}')"
228 cur.execute(query)
229 connection.commit()
230 messagebox.showinfo("Success", "Account created successfully!")
231 sign_up_window.destroy()
232 else:
233 messagebox.showerror("Error", "Passwords do not match. Please try again.")
234
235 # Create a new window for the sign-up form
236 sign_up_window = tk.Toplevel()
237 sign_up_window.title("Sign Up")
238 sign_up_window.geometry("200x200")
239
240 # Username label and entry
241 username_label = tk.Label(sign_up_window, text="Username:")
242 username_label.pack()
243 username_entry = tk.Entry(sign_up_window)
244 username_entry.pack()
245
246 # Password label and entry
247 password_label = tk.Label(sign_up_window, text="Password:")
248 password_label.pack()
249 password_entry = tk.Entry(sign_up_window, show="*")
250 password_entry.pack()
251
252 # Confirm password label and entry
253 confirm_password_label = tk.Label(sign_up_window, text="Confirm Password:")
254 confirm_password_label.pack()
255 confirm_password_entry = tk.Entry(sign_up_window, show="*")
256 confirm_password_entry.pack()
257
258 sign_up_button = tk.Button(sign_up_window, text="Sign Up", command=create_account)
259 sign_up_button.pack()
260
261
262
263
264
265
266root = tk.Tk()
267root.geometry('800x400')
268root.title("Consumer")
269
270login_frame = tk.Frame(root)
271login_frame.place(x=0, y=0)
272login_frame.config(height=400, width=800)
273
274username_label = tk.Label(login_frame, text='Enter Username')
275username_label.place(x=500, y=50)
276username = tk.Entry(login_frame, width=30)
277username.place(x=500, y=75)
278password_label = tk.Label(login_frame, text="Enter Password")
279password_label.place(x=500, y=100)
280password = tk.Entry(login_frame, width=30)
281password.place(x=500, y=125)
282login_button = tk.Button(login_frame, text='Login', command=get_usr_pwd, height=1, width=25)
283login_button.place(x=500, y=175)
284sign_up_button=tk.Button(login_frame,text='Sign Up',command=sign_up,height=1,width=25)
285sign_up_button.place(x=500,y=250)
286login_frame.pack()
287
288
289img = ImageTk.PhotoImage(Image.open(r'C:\Users\stevi\Downloads\219970.png'))
290image_frame = tk.Frame(login_frame)
291image_frame.place(x=-100, y=0)
292image_frame.config(height=10, width=10)
293image_label = tk.Label(image_frame, image=img)
294image_label.pack()
295
296menu_frame = tk.Frame(root)
297check_flight_details = tk.Button(menu_frame, text="Check Flight Details", command=flight_table, height=1, width=25)
298check_flight_details.place(x=550, y=50)
299
300book_tickets_button = tk.Button(menu_frame, text="Book Tickets", command=ticket_booking_ui, height=1, width=25)
301book_tickets_button.place(x=550, y=100)
302
303ticket_check_button = tk.Button(menu_frame,text = "Check Booked Tickets",command = ticket_table,height=1,width=25)
304ticket_check_button.place(x = 550, y = 150)
305
306request_button = tk.Button(menu_frame,text = "Request Cancellation",command = request_cancellation,height=1,width=25)
307request_button.place(x = 550, y = 200)
308
309request_table_button = tk.Button(menu_frame,text = "Request Status Check",command = request_table,height=1,width=25)
310request_table_button.place(x = 550, y = 250)
311
312img1 = ImageTk.PhotoImage(Image.open(r"C:\Users\stevi\Downloads\ariplene3.jpg"))
313image_frame1 = tk.Frame(menu_frame)
314image_frame1.place(x=0, y=0)
315image_frame1.config(height=400, width=400)
316image_label1 = tk.Label(image_frame1, image=img1)
317image_label1.pack()
318
319root.mainloop()
320