-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathticket.py
233 lines (183 loc) · 8.76 KB
/
ticket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import tkinter as tk
from tkinter import ttk
import sqlite3
from tkinter import messagebox
PRIMARY_COLOR = "#2C3E50" # Charcoal Blue
SECONDARY_COLOR = "#34495E" # Dark Slate Blue
ACCENT_COLOR = "#ECF0F1" # Light Cloud Gray
BUTTON_COLOR = "#2980B9" # Calm Blue
TEXT_COLOR = "#2C3E50" # Dark Charcoal
class TicketViewer:
def __init__(self, root):
self.root = root
self.root.title("Samaaye Events - Tickets")
self.root.geometry("800x600")
# Initialize database
self.setup_database()
# Create GUI
self.create_header()
self.create_main_content()
# Load initial tickets
self.refresh_tickets()
def setup_database(self):
"""Setup SQLite database if it doesn't exist"""
conn = sqlite3.connect('samaaye_events.db')
cursor = conn.cursor()
# Create bookings table if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS bookings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
mobile TEXT NOT NULL,
event_title TEXT NOT NULL,
event_date TEXT NOT NULL,
event_time TEXT NOT NULL,
event_location TEXT NOT NULL,
ticket_quantity INTEGER NOT NULL,
total_price REAL NOT NULL,
booking_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
conn.close()
def create_header(self):
"""Create the header with navigation"""
header = tk.Frame(self.root, bg=PRIMARY_COLOR)
header.pack(fill="x", padx=0, pady=0)
# Company logo and name
company_name = tk.Label(header, text="Samaaye events",
font=("Arial", 16), bg=PRIMARY_COLOR, fg=ACCENT_COLOR)
company_name.pack(side="left", padx=20, pady=10)
# Navigation buttons
nav_buttons = [
("Events", self.quit_window),
("Tickets", self.quit_window),
("My Account", None) # Add appropriate command for "My Account" if needed
]
for button_text, command in nav_buttons:
btn = tk.Button(header, text=button_text, bg=PRIMARY_COLOR,
fg=ACCENT_COLOR, bd=0, command=command)
btn.pack(side="left", padx=20, pady=10)
def quit_window(self):
"""Quit the application window"""
self.root.quit()
def create_main_content(self):
"""Create the main content area"""
content_frame = tk.Frame(self.root, bg=ACCENT_COLOR)
content_frame.pack(fill="both", expand=True, padx=20, pady=20)
# Title
title = tk.Label(content_frame, text="Booked Tickets",
font=("Arial", 24, "bold"), bg=ACCENT_COLOR, fg=TEXT_COLOR)
title.grid(row=0, column=0, columnspan=4, pady=10)
# Search frame
search_frame = tk.Frame(content_frame, bg=ACCENT_COLOR)
search_frame.grid(row=1, column=0, columnspan=4, pady=10, sticky="ew")
tk.Label(search_frame, text="Search:", bg=ACCENT_COLOR, fg=TEXT_COLOR).pack(side="left", padx=5)
self.search_var = tk.StringVar()
search_entry = tk.Entry(search_frame, textvariable=self.search_var)
search_entry.pack(side="left", padx=5)
tk.Button(search_frame, text="Search", bg=BUTTON_COLOR, fg=ACCENT_COLOR,
command=self.search_tickets).pack(side="left", padx=5)
tk.Button(search_frame, text="Reset", bg=BUTTON_COLOR, fg=ACCENT_COLOR,
command=self.refresh_tickets).pack(side="left", padx=5)
# Create Treeview for tickets without the "Delete" column
columns = ("ID", "Name", "Mobile", "Event", "Date", "Time", "Location",
"Quantity", "Total Price", "Booking Date")
self.tree = ttk.Treeview(content_frame, columns=columns, show="headings", height=20)
# Configure column headings and widths
for col in columns:
self.tree.heading(col, text=col, command=lambda c=col: self.sort_tickets(c))
width = 100 if col not in ["Name", "Event", "Location"] else 150
self.tree.column(col, width=width)
# Add scrollbar
scrollbar = ttk.Scrollbar(content_frame, orient="vertical", command=self.tree.yview)
self.tree.configure(yscrollcommand=scrollbar.set)
# Pack the treeview and scrollbar
self.tree.grid(row=2, column=0, columnspan=4, sticky="nsew")
scrollbar.grid(row=2, column=4, sticky="ns")
# Frame for delete button at the bottom of the content area
button_frame = tk.Frame(content_frame, bg=ACCENT_COLOR)
button_frame.grid(row=3, column=0, columnspan=5, pady=10, sticky="ew")
# Center the Delete Ticket button inside the frame
self.delete_button = tk.Button(button_frame, text="Delete", bg="#E74C3C", fg=ACCENT_COLOR,
command=self.delete_ticket, font=("Arial", 8, "bold"), width=6, height=1)
self.delete_button.grid(row=0, column=4, padx=10, pady=5)
# Configure row/column weights to adjust layout correctly
content_frame.grid_rowconfigure(2, weight=1) # Allow treeview to expand
content_frame.grid_columnconfigure(0, weight=1) # Allow treeview to expand
button_frame.grid_columnconfigure(2, weight=1) # Center the button
def refresh_tickets(self):
"""Refresh the tickets display"""
# Clear existing items
for item in self.tree.get_children():
self.tree.delete(item)
try:
conn = sqlite3.connect('samaaye_events.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM bookings ORDER BY booking_date DESC')
for row in cursor.fetchall():
self.tree.insert("", "end", values=row) # Add all columns, including booking_date
conn.close()
except sqlite3.Error as e:
messagebox.showerror("Database Error", f"Error accessing database: {str(e)}")
def delete_ticket(self):
"""Delete the selected ticket(s) from the database and refresh the list"""
selected_items = self.tree.selection()
if not selected_items:
messagebox.showwarning("No Selection", "Please select ticket to delete.")
return
# Ask if the user wants to delete the selected ticket(s)
num_tickets = len(selected_items)
confirm_msg = f"Do you want to delete the selected ticket?"
confirm = messagebox.askyesno("Delete Tickets", confirm_msg)
if confirm:
try:
conn = sqlite3.connect('samaaye_events.db')
cursor = conn.cursor()
# Delete selected tickets from the database
for item in selected_items:
ticket_id = self.tree.item(item)['values'][0] # First column is ID
cursor.execute('DELETE FROM bookings WHERE id = ?', (ticket_id,))
conn.commit()
conn.close()
messagebox.showinfo("Success", f" ticket deleted successfully.")
self.refresh_tickets() # Refresh the list after deletion
except sqlite3.Error as e:
messagebox.showerror("Database Error", f"Error deleting ticket(s): {str(e)}")
def search_tickets(self):
"""Search tickets based on name, mobile number, or event title"""
search_term = self.search_var.get().strip()
# Clear existing items
for item in self.tree.get_children():
self.tree.delete(item)
if not search_term:
self.refresh_tickets()
return
try:
conn = sqlite3.connect('samaaye_events.db')
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM bookings
WHERE name LIKE ? OR mobile LIKE ? OR event_title LIKE ?
ORDER BY booking_date DESC
''', (f'%{search_term}%', f'%{search_term}%', f'%{search_term}%'))
for row in cursor.fetchall():
self.tree.insert("", "end", values=row)
conn.close()
except sqlite3.Error as e:
messagebox.showerror("Database Error", f"Error searching database: {str(e)}")
def sort_tickets(self, column):
"""Sort tickets by clicking on column headers"""
# Get all items
items = [(self.tree.set(item, column), item) for item in self.tree.get_children("")]
# Sort items
items.sort()
# Rearrange items in sorted positions
for index, (_, item) in enumerate(items):
self.tree.move(item, "", index)
def main():
root = tk.Tk()
app = TicketViewer(root)
root.mainloop()
if __name__ == "__main__":
main()