-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui.py
293 lines (245 loc) · 10.9 KB
/
ui.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import tkinter as tk
from tkinter import messagebox, ttk
from PIL import Image, ImageTk
import traceback
import subprocess
import platform
import sqlite3
import sys
# Refined Color Palette
PRIMARY_COLOR = "#2C3E50"
SECONDARY_COLOR = "#34495E"
ACCENT_COLOR = "#ECF0F1"
BUTTON_COLOR = "#2980B9"
TEXT_COLOR = "#2C3E50"
# Function to display error messages
def show_error(error):
messagebox.showerror("Error", str(error))
traceback.print_exc()
# Function to handle ticket purchase by launching ui2.py with event data
def purchase_ticket(event):
current_dir = os.path.dirname(os.path.abspath(__file__))
ticket_booking_path = os.path.abspath(os.path.join(current_dir, "ui2.py"))
print("Launching:", ticket_booking_path)
try:
required_fields = ["title", "date", "time", "location", "ticket_price", "available_tickets"]
if not all(field in event for field in required_fields):
raise ValueError("Missing required event information")
ticket_price = str(event["ticket_price"])
available_tickets = str(event["available_tickets"])
print("Event data:", event)
subprocess.Popen([
sys.executable, ticket_booking_path,
event["title"],
event["date"],
event["time"],
event["location"],
ticket_price,
available_tickets,
])
except Exception as e:
show_error(f"Error launching ticket purchase: {str(e)}")
# Function to handle menu item clicks
def handle_menu_click(item):
current_dir = os.path.dirname(os.path.abspath(__file__))
if item == "Tickets":
tickets_path = os.path.abspath(os.path.join(current_dir, "ticket.py"))
try:
subprocess.Popen([sys.executable, tickets_path])
except Exception as e:
show_error(f"Error launching ticket.py: {str(e)}")
elif item == "My Account":
main_path = os.path.abspath(os.path.join(current_dir, "main.py"))
try:
subprocess.Popen([sys.executable, main_path])
except Exception as e:
show_error(f"Error launching main.py: {str(e)}")
else:
messagebox.showinfo("Info", f"{item} menu item clicked!")
# Function to calculate the number of columns based on the window width
def get_number_of_columns():
screen_width = root.winfo_width()
if screen_width >= 1200:
return 3
elif screen_width >= 800:
return 2
return 1
# Function to update event cards
def update_event_cards():
for widget in container.winfo_children():
widget.destroy()
num_columns = get_number_of_columns()
for i, event in enumerate(events):
row, column = divmod(i, num_columns)
card = create_event_card(container, event)
card.grid(row=row, column=column, padx=15, pady=20, sticky="nsew")
container.grid_columnconfigure(column, weight=1)
container.grid_rowconfigure(row, weight=1)
# Function to create an event card
def create_event_card(parent, event):
card = tk.Frame(parent, bg="white", relief=tk.FLAT, borderwidth=1,
highlightthickness=1, highlightbackground=SECONDARY_COLOR, padx=20, pady=20)
card.config(width=530, height=700)
card.grid_propagate(False)
content_frame = tk.Frame(card, bg="white")
content_frame.pack(fill=tk.BOTH, expand=True)
# Image handling
try:
img_path = event.get("image", "")
if img_path and os.path.exists(img_path):
img = Image.open(img_path).resize((530, 300), Image.LANCZOS)
img = ImageTk.PhotoImage(img)
image_label = tk.Label(content_frame, image=img, bg="white")
image_label.image = img
image_label.pack(pady=10)
else:
placeholder_label = tk.Label(content_frame, text="No Image Available",
bg="white", fg=TEXT_COLOR, height=10)
placeholder_label.pack(pady=10)
except Exception as e:
tk.Label(content_frame, text=f"Image Error: {str(e)}",
bg="white", fg="red").pack(pady=5)
# Info section
info_frame = tk.Frame(content_frame, bg="white")
info_frame.pack(fill=tk.X, padx=15, pady=10)
tk.Label(info_frame, text=event["date"], bg="white",
fg=BUTTON_COLOR, font=("Helvetica", 16)).pack(fill=tk.X)
tk.Label(info_frame, text=event["title"], bg="white",
fg=PRIMARY_COLOR, font=("Helvetica", 18)).pack(fill=tk.X, pady=(5, 0))
tk.Label(info_frame, text=event["time"], bg="white",
fg=TEXT_COLOR, font=("Helvetica", 12)).pack(fill=tk.X)
tk.Label(info_frame, text=event["location"], bg="white",
fg=TEXT_COLOR, font=("Helvetica", 12)).pack(fill=tk.X)
# Bottom section
bottom_frame = tk.Frame(content_frame, bg="white")
bottom_frame.pack(fill=tk.X, padx=15, pady=(10, 0))
# Format price with proper currency
price_text = f"NPR {float(event['ticket_price']):,.2f}"
tk.Label(bottom_frame, text=price_text, bg="white",
fg=BUTTON_COLOR, font=("Helvetica", 14)).pack(side=tk.LEFT)
# Add tickets available info
tickets_text = f"Available: {event['available_tickets']}"
tk.Label(bottom_frame, text=tickets_text, bg="white",
fg=TEXT_COLOR, font=("Helvetica", 12)).pack(side=tk.LEFT, padx=10)
# Buy button
buy_button = tk.Button(
bottom_frame,
text="Buy Tickets",
bg=BUTTON_COLOR,
fg=ACCENT_COLOR,
font=("Helvetica", 12),
command=lambda e=event: purchase_ticket(e)
)
buy_button.pack(side=tk.RIGHT)
return card
# Function to fetch events from database
def fetch_events_from_db():
db_events = []
try:
conn = sqlite3.connect('samaaye_events.db')
cursor = conn.cursor()
cursor.execute('''
SELECT title, date, time, location, ticket_price,
available_tickets, image_path
FROM events
''')
rows = cursor.fetchall()
conn.close()
for row in rows:
event = {
"title": row[0],
"date": row[1],
"time": row[2],
"location": row[3],
"ticket_price": float(row[4]) if row[4] else 0.0,
"available_tickets": int(row[5]) if row[5] else 0,
"image": row[6] if row[6] and os.path.exists(row[6]) else ""
}
db_events.append(event)
except sqlite3.Error as e:
show_error(f"Database Error: {str(e)}")
traceback.print_exc()
return db_events
# Main Application
try:
root = tk.Tk()
root.title("Samaaye Interactives")
# Set window state based on the operating system
if platform.system() == "Windows":
root.state('zoomed')
elif platform.system() == "Darwin": # macOS
root.attributes('-fullscreen', True)
elif platform.system() == "Linux":
root.attributes('-zoomed', True)
else:
root.geometry("1200x800")
root.configure(bg=ACCENT_COLOR)
# Create header
header = tk.Frame(root, bg=PRIMARY_COLOR, height=80)
header.grid(row=0, column=0, sticky="ew")
# Add logo image instead of text
try:
logo_path = "Samaaye Interactives.jpg"
if os.path.exists(logo_path):
logo_img = Image.open(logo_path).resize((150, 80), Image.LANCZOS)
logo_img = ImageTk.PhotoImage(logo_img)
logo_label = tk.Label(header, image=logo_img, bg=PRIMARY_COLOR)
logo_label.image = logo_img
logo_label.pack(side=tk.LEFT, padx=30, pady=20)
else:
tk.Label(header, text="Logo Not Found", bg=PRIMARY_COLOR, fg="red", font=("Helvetica", 16)).pack(side=tk.LEFT, padx=30)
except Exception as e:
show_error(f"Error loading logo: {e}")
menu = tk.Frame(header, bg=PRIMARY_COLOR)
menu.pack(side=tk.RIGHT, padx=30)
for menu_item in ["Events", "Tickets", "My Account"]:
menu_label = tk.Label(menu, text=menu_item, bg=PRIMARY_COLOR, fg=ACCENT_COLOR, font=("Helvetica", 14))
menu_label.pack(side=tk.LEFT, padx=20)
menu_label.bind("<Button-1>", lambda e, item=menu_item: handle_menu_click(item))
title = tk.Label(root, text="Upcoming Events", bg=ACCENT_COLOR, fg=PRIMARY_COLOR, font=("Helvetica", 32))
title.grid(row=1, column=0, pady=30)
# Create scrollable canvas
canvas = tk.Canvas(root, bg=ACCENT_COLOR, width=1550)
scrollbar = ttk.Scrollbar(root, orient="vertical", command=canvas.yview)
scrollable_frame = ttk.Frame(canvas)
container = tk.Frame(scrollable_frame, bg=ACCENT_COLOR)
container.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(scrollregion=canvas.bbox("all"))
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.grid(row=2, column=0, sticky="nsew")
scrollbar.grid(row=2, column=1, sticky="ns")
root.grid_rowconfigure(2, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=0)
# Initialize events
events = []
# Add hardcoded events
hardcoded_events = [
{"title": "The Big 3 (2nd Show)", "date": "5 Dec", "time": "6:00 PM onwards", "location": "LOD, Thamel",
"image": "abc.png", "ticket_price": 500, "available_tickets": 100},
{"title": "Nepal Premier League", "date": "19 Dec", "time": "9:15 AM onwards", "location": "TU Cricket Ground",
"image": "npl.png", "ticket_price": 750, "available_tickets": 250},
{"title": "Grasslands Carnival", "date": "5 Dec", "time": "4:30 PM onwards", "location": "Patan Durbar Square",
"image": "grass.jpg", "ticket_price": 350, "available_tickets": 75},
{"title": "Tech Conference 2025", "date": "23 Dec", "time": "9:00 AM onwards", "location": "Kathmandu",
"image": "maxresdefault.png", "ticket_price": 800, "available_tickets": 200},
{"title": "New Year's Eve Party", "date": "31 Dec", "time": "10:00 PM onwards", "location": "City Hall",
"image": "TentCardversion.png", "ticket_price": 1200, "available_tickets": 500},
]
events.extend(hardcoded_events)
seen_titles = set(event["title"] for event in events)
db_events = fetch_events_from_db()
for event in db_events:
if event["title"] not in seen_titles:
events.append(event)
seen_titles.add(event["title"])
update_event_cards() # Initial render of events
root.bind("<Configure>", lambda e: update_event_cards() if e.widget == root else None)
root.mainloop()
except Exception as e:
show_error(e)