-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodern_timetable_gui.py
More file actions
1790 lines (1457 loc) · 77.7 KB
/
modern_timetable_gui.py
File metadata and controls
1790 lines (1457 loc) · 77.7 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import tkinter as tk
from tkinter import ttk, messagebox, scrolledtext, filedialog
import sys
import io
import threading
import sqlite3
import os
import datetime
import pandas as pd
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
from openpyxl.utils import get_column_letter
from db_operations import initialize_database, fetch_data_from_db, get_course_classes, set_course_classes
from db_operations import add_professor, add_course, add_branch, add_branch_teacher_course_mapping
from db_operations import delete_course, delete_course_teacher_mapping
from db_operations import get_all_professors, get_professor_courses, get_available_courses_for_professor
from db_operations import add_course_to_professor, remove_course_from_professor
from simplified_genetic_algorithm import genetic_algorithm, print_timetable, print_professor_schedules
from main import debug_database, verify_database_integrity
# Color scheme
COLORS = {
"primary": "#3498db", # Blue
"secondary": "#2ecc71", # Green
"accent": "#e74c3c", # Red
"background": "#f5f5f5", # Light gray
"text": "#2c3e50", # Dark blue/gray
"light_text": "#7f8c8d", # Light gray text
"success": "#27ae60", # Dark green
"warning": "#f39c12", # Orange
"error": "#c0392b" # Dark red
}
# Redirect stdout to capture print statements
class StdoutRedirector:
def __init__(self, text_widget):
self.text_widget = text_widget
self.buffer = io.StringIO()
def write(self, string):
self.buffer.write(string)
self.text_widget.insert(tk.END, string)
self.text_widget.see(tk.END) # Auto-scroll to the end
def flush(self):
pass
class ModernTimetableApp:
def __init__(self, root):
self.root = root
self.root.title("Fast Timetable Generator")
self.root.geometry("1200x800")
self.root.minsize(1000, 700)
# Configure the style
self.configure_style()
# Store the last generated timetable and professor schedules
self.last_timetable = None
self.last_professor_schedules = None
# Create main frame
self.main_frame = ttk.Frame(root)
self.main_frame.pack(fill=tk.BOTH, expand=True)
# Create sidebar and content area
self.create_sidebar()
self.create_content_area()
# Initialize the database
self.initialize_db()
# Show the home screen by default
self.show_home()
def configure_style(self):
"""Configure the ttk styles for a modern look"""
style = ttk.Style()
style.theme_use('clam') # Use the 'clam' theme as a base
# Configure colors
style.configure('TFrame', background=COLORS["background"])
style.configure('TLabel', background=COLORS["background"], foreground=COLORS["text"])
style.configure('TButton', background=COLORS["primary"], foreground="white", font=('Arial', 10))
style.map('TButton', background=[('active', COLORS["secondary"])])
# Sidebar button style
style.configure('Sidebar.TButton', font=('Arial', 11), padding=10)
# Heading style
style.configure('Heading.TLabel', font=('Arial', 16, 'bold'), foreground=COLORS["primary"])
style.configure('SubHeading.TLabel', font=('Arial', 12, 'italic'), foreground=COLORS["light_text"])
# Card style
style.configure('Card.TFrame', background="white", relief="raised", borderwidth=1)
# Success and error styles
style.configure('Success.TLabel', foreground=COLORS["success"])
style.configure('Error.TLabel', foreground=COLORS["error"])
def create_sidebar(self):
"""Create the sidebar with navigation buttons"""
self.sidebar = ttk.Frame(self.main_frame, width=200, style='TFrame')
self.sidebar.pack(side=tk.LEFT, fill=tk.Y, padx=0, pady=0)
# Make the sidebar fixed width
self.sidebar.pack_propagate(False)
# App title
title_frame = ttk.Frame(self.sidebar, style='TFrame')
title_frame.pack(fill=tk.X, padx=10, pady=20)
ttk.Label(title_frame, text="Timetable", font=('Arial', 18, 'bold'),
foreground=COLORS["primary"], style='TLabel').pack(anchor=tk.W)
ttk.Label(title_frame, text="Generator", font=('Arial', 14),
foreground=COLORS["secondary"], style='TLabel').pack(anchor=tk.W)
# Navigation buttons
self.nav_buttons = []
nav_items = [
("Home", self.show_home),
("Database", self.show_database),
("Courses", self.show_courses),
("Classes", self.show_classes),
("Professor Courses", self.show_professor_courses),
("Generate", self.show_generate),
("View Timetable", self.show_timetable),
("Professor Schedules", self.show_professors)
]
for text, command in nav_items:
btn = ttk.Button(self.sidebar, text=text, command=command, style='Sidebar.TButton')
btn.pack(fill=tk.X, padx=10, pady=5)
self.nav_buttons.append(btn)
# Status section at bottom of sidebar
status_frame = ttk.Frame(self.sidebar, style='TFrame')
status_frame.pack(side=tk.BOTTOM, fill=tk.X, padx=10, pady=20)
ttk.Label(status_frame, text="Status:", style='TLabel').pack(anchor=tk.W)
self.status_var = tk.StringVar(value="Ready")
ttk.Label(status_frame, textvariable=self.status_var, style='TLabel').pack(anchor=tk.W, pady=5)
def create_content_area(self):
"""Create the main content area"""
self.content = ttk.Frame(self.main_frame, style='TFrame')
self.content.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=20, pady=20)
# Create frames for each section (only one will be visible at a time)
self.home_frame = ttk.Frame(self.content, style='TFrame')
self.database_frame = ttk.Frame(self.content, style='TFrame')
self.courses_frame = ttk.Frame(self.content, style='TFrame')
self.classes_frame = ttk.Frame(self.content, style='TFrame') # New frame for classes management
self.professor_courses_frame = ttk.Frame(self.content, style='TFrame') # New frame for professor courses
self.generate_frame = ttk.Frame(self.content, style='TFrame')
self.timetable_frame = ttk.Frame(self.content, style='TFrame')
self.professors_frame = ttk.Frame(self.content, style='TFrame')
# Set up each frame
self.setup_home_frame()
self.setup_database_frame()
self.setup_courses_frame()
self.setup_classes_frame() # Setup the new classes frame
self.setup_professor_courses_frame() # Setup the professor courses frame
self.setup_generate_frame()
self.setup_timetable_frame()
self.setup_professors_frame()
def hide_all_frames(self):
"""Hide all content frames"""
for frame in [self.home_frame, self.database_frame, self.courses_frame, self.classes_frame,
self.professor_courses_frame, self.generate_frame, self.timetable_frame, self.professors_frame]:
frame.pack_forget()
def show_home(self):
self.hide_all_frames()
self.home_frame.pack(fill=tk.BOTH, expand=True)
def show_database(self):
self.hide_all_frames()
self.database_frame.pack(fill=tk.BOTH, expand=True)
def show_courses(self):
self.hide_all_frames()
self.courses_frame.pack(fill=tk.BOTH, expand=True)
self.refresh_courses()
self.refresh_branches()
self.refresh_mapping_lists()
def show_classes(self):
self.hide_all_frames()
self.classes_frame.pack(fill=tk.BOTH, expand=True)
self.refresh_courses_for_classes()
def show_professor_courses(self):
self.hide_all_frames()
self.professor_courses_frame.pack(fill=tk.BOTH, expand=True)
self.refresh_professors_list()
def show_generate(self):
self.hide_all_frames()
self.generate_frame.pack(fill=tk.BOTH, expand=True)
def show_timetable(self):
self.hide_all_frames()
self.timetable_frame.pack(fill=tk.BOTH, expand=True)
def show_professors(self):
self.hide_all_frames()
self.professors_frame.pack(fill=tk.BOTH, expand=True)
self.refresh_professors()
def setup_home_frame(self):
"""Set up the home screen with a modern, clean design"""
# Title with improved styling
title_frame = ttk.Frame(self.home_frame, style='TFrame')
title_frame.pack(fill=tk.X, pady=(0, 20))
ttk.Label(title_frame, text="Timetable Generator",
font=('Arial', 24, 'bold'), foreground=COLORS["primary"],
style='TLabel').pack(pady=(0, 5))
ttk.Label(title_frame, text="Create optimal timetables using genetic algorithms",
style='SubHeading.TLabel').pack(pady=(0, 10))
# Main content in a two-column layout
content_frame = ttk.Frame(self.home_frame, style='TFrame')
content_frame.pack(fill=tk.BOTH, expand=True, pady=10)
content_frame.columnconfigure(0, weight=1)
content_frame.columnconfigure(1, weight=1)
# Left column: Stats and actions
left_col = ttk.Frame(content_frame, style='TFrame')
left_col.grid(row=0, column=0, padx=10, pady=10, sticky=tk.N+tk.S+tk.W+tk.E)
# Stats cards
stats_frame = ttk.LabelFrame(left_col, text="Database Statistics", style='TFrame')
stats_frame.pack(fill=tk.X, pady=10)
stats_grid = ttk.Frame(stats_frame, style='TFrame')
stats_grid.pack(fill=tk.X, padx=10, pady=10)
stats_grid.columnconfigure(0, weight=1)
stats_grid.columnconfigure(1, weight=1)
stats_grid.columnconfigure(2, weight=1)
# Create stat cards in a grid
self.create_stat_card(stats_grid, "Branches", "branch_count", 0, 0)
self.create_stat_card(stats_grid, "Courses", "course_count", 0, 1)
self.create_stat_card(stats_grid, "Teachers", "teacher_count", 0, 2)
# Quick actions
actions_frame = ttk.LabelFrame(left_col, text="Quick Actions", style='TFrame')
actions_frame.pack(fill=tk.X, pady=10)
# Create a grid for buttons with 2 columns
button_grid = ttk.Frame(actions_frame, style='TFrame')
button_grid.pack(fill=tk.X, padx=10, pady=10)
button_grid.columnconfigure(0, weight=1)
button_grid.columnconfigure(1, weight=1)
# Add buttons with icons (using text for now)
ttk.Button(button_grid, text="Initialize Database",
command=self.initialize_db).grid(row=0, column=0, padx=5, pady=5, sticky=tk.W+tk.E)
ttk.Button(button_grid, text="Generate Timetable",
command=self.generate_timetable).grid(row=0, column=1, padx=5, pady=5, sticky=tk.W+tk.E)
ttk.Button(button_grid, text="View Timetable",
command=self.show_timetable).grid(row=1, column=0, padx=5, pady=5, sticky=tk.W+tk.E)
ttk.Button(button_grid, text="View Professor Schedules",
command=self.show_professors).grid(row=1, column=1, padx=5, pady=5, sticky=tk.W+tk.E)
# Right column: Console output
right_col = ttk.Frame(content_frame, style='TFrame')
right_col.grid(row=0, column=1, padx=10, pady=10, sticky=tk.N+tk.S+tk.W+tk.E)
console_frame = ttk.LabelFrame(right_col, text="Console Output", style='TFrame')
console_frame.pack(fill=tk.BOTH, expand=True)
self.console = scrolledtext.ScrolledText(console_frame, wrap=tk.WORD, height=15,
font=('Consolas', 9))
self.console.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# Add a welcome message to the console
welcome_msg = (
"Welcome to Timetable Generator!\n\n"
"This application helps you create optimal timetables using genetic algorithms.\n\n"
"To get started:\n"
"1. Initialize the database\n"
"2. Add branches, professors, and courses\n"
"3. Generate a timetable\n\n"
"System ready and waiting for commands...\n"
)
self.console.insert(tk.END, welcome_msg)
# Redirect stdout to the console
sys.stdout = StdoutRedirector(self.console)
def create_stat_card(self, parent, title, var_name, row, col):
"""Create a statistics card"""
card = ttk.Frame(parent, style='Card.TFrame')
card.grid(row=row, column=col, padx=10, pady=10, sticky=tk.W+tk.E)
ttk.Label(card, text=title, font=('Arial', 12), background="white").pack(pady=(10, 5))
# Create a variable to hold the count
setattr(self, var_name, tk.StringVar(value="0"))
ttk.Label(card, textvariable=getattr(self, var_name),
font=('Arial', 24, 'bold'), foreground=COLORS["primary"],
background="white").pack(pady=(0, 10))
def setup_database_frame(self):
"""Set up the database management frame"""
# Title
ttk.Label(self.database_frame, text="Database Management",
style='Heading.TLabel').pack(pady=(0, 20))
# Create a notebook for tabs
notebook = ttk.Notebook(self.database_frame)
notebook.pack(fill=tk.BOTH, expand=True)
# Create tabs
actions_tab = ttk.Frame(notebook, style='TFrame')
branch_tab = ttk.Frame(notebook, style='TFrame')
professor_tab = ttk.Frame(notebook, style='TFrame')
notebook.add(actions_tab, text="Database Actions")
notebook.add(branch_tab, text="Add Branch")
notebook.add(professor_tab, text="Add Professor")
# Database actions tab
actions_frame = ttk.Frame(actions_tab, style='TFrame')
actions_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
ttk.Button(actions_frame, text="Initialize Database",
command=self.initialize_db).pack(fill=tk.X, pady=10)
ttk.Button(actions_frame, text="Debug Database",
command=self.debug_database).pack(fill=tk.X, pady=10)
ttk.Button(actions_frame, text="Verify Database Integrity",
command=self.verify_database).pack(fill=tk.X, pady=10)
# Console output for actions
console_frame = ttk.LabelFrame(actions_frame, text="Console Output", style='TFrame')
console_frame.pack(fill=tk.BOTH, expand=True, pady=20)
self.db_console = scrolledtext.ScrolledText(console_frame, wrap=tk.WORD, height=10)
self.db_console.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# Branch tab
branch_frame = ttk.Frame(branch_tab, style='TFrame')
branch_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
ttk.Label(branch_frame, text="Branch Name:").pack(anchor=tk.W, pady=(0, 5))
self.branch_name_var = tk.StringVar()
ttk.Entry(branch_frame, textvariable=self.branch_name_var, width=40).pack(fill=tk.X, pady=(0, 10))
ttk.Button(branch_frame, text="Add Branch",
command=self.add_branch).pack(anchor=tk.W, pady=10)
# Professor tab
prof_frame = ttk.Frame(professor_tab, style='TFrame')
prof_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
ttk.Label(prof_frame, text="Professor Name:").pack(anchor=tk.W, pady=(0, 5))
self.prof_name_var = tk.StringVar()
ttk.Entry(prof_frame, textvariable=self.prof_name_var, width=40).pack(fill=tk.X, pady=(0, 10))
ttk.Label(prof_frame, text="Branch:").pack(anchor=tk.W, pady=(10, 5))
self.prof_branch_var = tk.StringVar()
self.prof_branch_combo = ttk.Combobox(prof_frame, textvariable=self.prof_branch_var, width=40)
self.prof_branch_combo.pack(fill=tk.X, pady=(0, 10))
button_frame = ttk.Frame(prof_frame, style='TFrame')
button_frame.pack(fill=tk.X, pady=10)
ttk.Button(button_frame, text="Add Professor",
command=self.add_professor).pack(side=tk.LEFT, padx=(0, 10))
ttk.Button(button_frame, text="Refresh Branches",
command=self.refresh_branches).pack(side=tk.LEFT)
def setup_courses_frame(self):
"""Set up the course management frame"""
# Title
ttk.Label(self.courses_frame, text="Course Management",
style='Heading.TLabel').pack(pady=(0, 20))
# Create a notebook for tabs
notebook = ttk.Notebook(self.courses_frame)
notebook.pack(fill=tk.BOTH, expand=True)
# Create tabs
add_course_tab = ttk.Frame(notebook, style='TFrame')
mapping_tab = ttk.Frame(notebook, style='TFrame')
delete_tab = ttk.Frame(notebook, style='TFrame')
notebook.add(add_course_tab, text="Add Course")
notebook.add(mapping_tab, text="Add Mapping")
notebook.add(delete_tab, text="Delete Course")
# Add course tab
course_frame = ttk.Frame(add_course_tab, style='TFrame')
course_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
ttk.Label(course_frame, text="Course Code:").pack(anchor=tk.W, pady=(0, 5))
self.course_code_var = tk.StringVar()
ttk.Entry(course_frame, textvariable=self.course_code_var, width=40).pack(fill=tk.X, pady=(0, 10))
ttk.Label(course_frame, text="Course Name:").pack(anchor=tk.W, pady=(10, 5))
self.course_name_var = tk.StringVar()
ttk.Entry(course_frame, textvariable=self.course_name_var, width=40).pack(fill=tk.X, pady=(0, 10))
ttk.Label(course_frame, text="Branch:").pack(anchor=tk.W, pady=(10, 5))
self.course_branch_var = tk.StringVar()
self.course_branch_combo = ttk.Combobox(course_frame, textvariable=self.course_branch_var, width=40)
self.course_branch_combo.pack(fill=tk.X, pady=(0, 10))
button_frame = ttk.Frame(course_frame, style='TFrame')
button_frame.pack(fill=tk.X, pady=10)
ttk.Button(button_frame, text="Add Course",
command=self.add_course).pack(side=tk.LEFT, padx=(0, 10))
ttk.Button(button_frame, text="Refresh Branches",
command=self.refresh_branches).pack(side=tk.LEFT)
# Mapping tab
mapping_frame = ttk.Frame(mapping_tab, style='TFrame')
mapping_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
ttk.Label(mapping_frame, text="Branch:").pack(anchor=tk.W, pady=(0, 5))
self.mapping_branch_var = tk.StringVar()
self.mapping_branch_combo = ttk.Combobox(mapping_frame, textvariable=self.mapping_branch_var, width=40)
self.mapping_branch_combo.pack(fill=tk.X, pady=(0, 10))
ttk.Label(mapping_frame, text="Teacher:").pack(anchor=tk.W, pady=(10, 5))
self.mapping_teacher_var = tk.StringVar()
self.mapping_teacher_combo = ttk.Combobox(mapping_frame, textvariable=self.mapping_teacher_var, width=40)
self.mapping_teacher_combo.pack(fill=tk.X, pady=(0, 10))
ttk.Label(mapping_frame, text="Course:").pack(anchor=tk.W, pady=(10, 5))
self.mapping_course_var = tk.StringVar()
self.mapping_course_combo = ttk.Combobox(mapping_frame, textvariable=self.mapping_course_var, width=40)
self.mapping_course_combo.pack(fill=tk.X, pady=(0, 10))
button_frame = ttk.Frame(mapping_frame, style='TFrame')
button_frame.pack(fill=tk.X, pady=10)
ttk.Button(button_frame, text="Add Mapping",
command=self.add_mapping).pack(side=tk.LEFT, padx=(0, 10))
ttk.Button(button_frame, text="Refresh Lists",
command=self.refresh_mapping_lists).pack(side=tk.LEFT)
# Delete course tab
delete_frame = ttk.Frame(delete_tab, style='TFrame')
delete_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
ttk.Label(delete_frame, text="Course:").pack(anchor=tk.W, pady=(0, 5))
self.delete_course_var = tk.StringVar()
self.delete_course_combo = ttk.Combobox(delete_frame, textvariable=self.delete_course_var, width=40)
self.delete_course_combo.pack(fill=tk.X, pady=(0, 10))
button_frame = ttk.Frame(delete_frame, style='TFrame')
button_frame.pack(fill=tk.X, pady=10)
ttk.Button(button_frame, text="Delete Course",
command=self.delete_course).pack(side=tk.LEFT, padx=(0, 10))
ttk.Button(button_frame, text="Refresh Courses",
command=self.refresh_courses).pack(side=tk.LEFT)
# Console output for all tabs
console_frame = ttk.LabelFrame(self.courses_frame, text="Console Output", style='TFrame')
console_frame.pack(fill=tk.BOTH, expand=True, pady=20)
self.course_console = scrolledtext.ScrolledText(console_frame, wrap=tk.WORD, height=10)
self.course_console.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
def setup_generate_frame(self):
"""Set up the timetable generation frame"""
# Title
ttk.Label(self.generate_frame, text="Generate Timetable",
style='Heading.TLabel').pack(pady=(0, 20))
ttk.Label(self.generate_frame, text="Using genetic algorithm for optimal timetable generation",
style='SubHeading.TLabel').pack(pady=(0, 10))
# Generation options
options_frame = ttk.LabelFrame(self.generate_frame, text="Generation Options", style='TFrame')
options_frame.pack(fill=tk.X, pady=10)
# Create a grid layout with 2 columns
options_frame.columnconfigure(0, weight=0)
options_frame.columnconfigure(1, weight=1)
# Generations option
ttk.Label(options_frame, text="Generations:").grid(row=0, column=0, padx=10, pady=10, sticky=tk.W)
self.generations_var = tk.StringVar(value="100")
ttk.Spinbox(options_frame, from_=30, to=300, increment=10,
textvariable=self.generations_var, width=10).grid(row=0, column=1, padx=10, pady=10, sticky=tk.W)
# Generate button
generate_btn = ttk.Button(options_frame, text="Generate Timetable",
command=self.generate_timetable)
generate_btn.grid(row=1, column=0, columnspan=2, padx=10, pady=10, sticky=tk.W+tk.E)
# Status indicator
status_frame = ttk.Frame(self.generate_frame, style='TFrame')
status_frame.pack(fill=tk.X, pady=10)
self.progress_var = tk.StringVar(value="Not started")
ttk.Label(status_frame, text="Status:").pack(side=tk.LEFT, padx=(0, 10))
ttk.Label(status_frame, textvariable=self.progress_var,
font=('Arial', 10, 'bold')).pack(side=tk.LEFT)
# Information panel
info_frame = ttk.LabelFrame(self.generate_frame, text="Information", style='TFrame')
info_frame.pack(fill=tk.BOTH, expand=True, pady=10)
info_text = (
"The timetable generation process will run in the background.\n\n"
"Once complete, you will be automatically redirected to the timetable view.\n\n"
"For large datasets, this process may take several minutes.\n"
"Please be patient while the genetic algorithm optimizes your timetable."
)
ttk.Label(info_frame, text=info_text, wraplength=500, justify="center").pack(
fill=tk.BOTH, expand=True, padx=20, pady=20
)
def setup_timetable_frame(self):
"""Set up the timetable display frame"""
# Title
ttk.Label(self.timetable_frame, text="Timetable View",
style='Heading.TLabel').pack(pady=(0, 20))
# Actions toolbar
actions_frame = ttk.Frame(self.timetable_frame, style='TFrame')
actions_frame.pack(fill=tk.X, pady=10)
# Create a more organized button layout
button_frame = ttk.Frame(actions_frame, style='TFrame')
button_frame.pack(fill=tk.X)
# Configure columns for even spacing
button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
button_frame.columnconfigure(2, weight=1)
# Add buttons in a grid layout
ttk.Button(button_frame, text="Generate New Timetable",
command=self.show_generate).grid(row=0, column=0, padx=5, pady=5, sticky=tk.W+tk.E)
ttk.Button(button_frame, text="View Professor Schedules",
command=self.show_professors).grid(row=0, column=1, padx=5, pady=5, sticky=tk.W+tk.E)
ttk.Button(button_frame, text="Export to Excel",
command=self.export_timetable_to_excel).grid(row=0, column=2, padx=5, pady=5, sticky=tk.W+tk.E)
# Timetable display with improved styling
timetable_frame = ttk.LabelFrame(self.timetable_frame, text="Current Timetable", style='TFrame')
timetable_frame.pack(fill=tk.BOTH, expand=True, pady=10)
# Use a monospaced font for better timetable display
self.timetable_text = scrolledtext.ScrolledText(
timetable_frame,
wrap=tk.WORD,
font=('Courier New', 10)
)
self.timetable_text.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# Initial message with better formatting
self.timetable_text.insert(tk.END,
"No timetable has been generated yet.\n\n"
"Please go to the Generate tab to create a new timetable."
)
def setup_classes_frame(self):
"""Set up the course classes management frame"""
# Title
ttk.Label(self.classes_frame, text="Course Classes Management",
style='Heading.TLabel').pack(pady=(0, 20))
ttk.Label(self.classes_frame, text="Set the number of classes per week for each course",
style='SubHeading.TLabel').pack(pady=(0, 20))
# Create a notebook for tabs
notebook = ttk.Notebook(self.classes_frame)
notebook.pack(fill=tk.BOTH, expand=True)
# Create tabs
view_tab = ttk.Frame(notebook, style='TFrame')
update_tab = ttk.Frame(notebook, style='TFrame')
update_all_tab = ttk.Frame(notebook, style='TFrame')
notebook.add(view_tab, text="View Classes")
notebook.add(update_tab, text="Update Course")
notebook.add(update_all_tab, text="Update All Courses")
# View classes tab
view_frame = ttk.Frame(view_tab, style='TFrame')
view_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
ttk.Button(view_frame, text="View Course Classes Configuration",
command=self.view_course_classes).pack(fill=tk.X, pady=10)
# Update single course tab
update_frame = ttk.Frame(update_tab, style='TFrame')
update_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
ttk.Label(update_frame, text="Course:").pack(anchor=tk.W, pady=(0, 5))
self.update_course_var = tk.StringVar()
self.update_course_combo = ttk.Combobox(update_frame, textvariable=self.update_course_var, width=40)
self.update_course_combo.pack(fill=tk.X, pady=(0, 10))
ttk.Label(update_frame, text="Classes per Week:").pack(anchor=tk.W, pady=(10, 5))
self.update_classes_var = tk.StringVar(value="2")
ttk.Spinbox(update_frame, from_=1, to=5, textvariable=self.update_classes_var, width=10).pack(anchor=tk.W, pady=(0, 10))
button_frame = ttk.Frame(update_frame, style='TFrame')
button_frame.pack(fill=tk.X, pady=10)
ttk.Button(button_frame, text="Update Course",
command=self.update_course_classes).pack(side=tk.LEFT, padx=(0, 10))
ttk.Button(button_frame, text="Refresh Courses",
command=self.refresh_courses_for_classes).pack(side=tk.LEFT)
# Update all courses tab
update_all_frame = ttk.Frame(update_all_tab, style='TFrame')
update_all_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
ttk.Label(update_all_frame, text="Classes per Week for ALL courses:").pack(anchor=tk.W, pady=(0, 5))
self.update_all_classes_var = tk.StringVar(value="2")
ttk.Spinbox(update_all_frame, from_=1, to=5, textvariable=self.update_all_classes_var, width=10).pack(anchor=tk.W, pady=(0, 10))
ttk.Button(update_all_frame, text="Update All Courses",
command=self.update_all_course_classes).pack(anchor=tk.W, pady=10)
# Console output for all tabs
console_frame = ttk.LabelFrame(self.classes_frame, text="Console Output", style='TFrame')
console_frame.pack(fill=tk.BOTH, expand=True, pady=20)
self.classes_console = scrolledtext.ScrolledText(console_frame, wrap=tk.WORD, height=10)
self.classes_console.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
def setup_professor_courses_frame(self):
"""Set up the professor courses management frame"""
# Title
ttk.Label(self.professor_courses_frame, text="Professor Course Management",
style='Heading.TLabel').pack(pady=(0, 20))
# Create a split view with professors on the left and courses on the right
main_pane = ttk.PanedWindow(self.professor_courses_frame, orient=tk.HORIZONTAL)
main_pane.pack(fill=tk.BOTH, expand=True, pady=10)
# Left frame for professor selection
left_frame = ttk.Frame(main_pane, style='TFrame')
main_pane.add(left_frame, weight=1)
# Professor selection
ttk.Label(left_frame, text="Select Professor:", style='SubHeading.TLabel').pack(anchor=tk.W, pady=(0, 10))
# Create a frame for the professor list
prof_list_frame = ttk.Frame(left_frame, style='TFrame')
prof_list_frame.pack(fill=tk.BOTH, expand=True, pady=5)
# Create a treeview for the professors
self.professors_tree = ttk.Treeview(prof_list_frame, columns=("id", "name", "branch"), show="headings")
self.professors_tree.heading("id", text="ID")
self.professors_tree.heading("name", text="Professor Name")
self.professors_tree.heading("branch", text="Branch")
self.professors_tree.column("id", width=50)
self.professors_tree.column("name", width=200)
self.professors_tree.column("branch", width=150)
self.professors_tree.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# Add a scrollbar
scrollbar = ttk.Scrollbar(prof_list_frame, orient=tk.VERTICAL, command=self.professors_tree.yview)
self.professors_tree.configure(yscroll=scrollbar.set)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Bind selection event
self.professors_tree.bind("<<TreeviewSelect>>", self.on_professor_selected)
# Refresh button
ttk.Button(left_frame, text="Refresh Professors",
command=self.refresh_professors_list).pack(fill=tk.X, pady=10)
# Right frame for courses
right_frame = ttk.Frame(main_pane, style='TFrame')
main_pane.add(right_frame, weight=2)
# Create a notebook for tabs
self.prof_courses_notebook = ttk.Notebook(right_frame)
self.prof_courses_notebook.pack(fill=tk.BOTH, expand=True)
# Current courses tab
current_courses_tab = ttk.Frame(self.prof_courses_notebook, style='TFrame')
self.prof_courses_notebook.add(current_courses_tab, text="Current Courses")
# Create a treeview for current courses
self.current_courses_tree = ttk.Treeview(current_courses_tab, columns=("code", "name", "branch"), show="headings")
self.current_courses_tree.heading("code", text="Course Code")
self.current_courses_tree.heading("name", text="Course Name")
self.current_courses_tree.heading("branch", text="Branch")
self.current_courses_tree.column("code", width=100)
self.current_courses_tree.column("name", width=250)
self.current_courses_tree.column("branch", width=150)
self.current_courses_tree.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# Add a scrollbar
scrollbar = ttk.Scrollbar(current_courses_tab, orient=tk.VERTICAL, command=self.current_courses_tree.yview)
self.current_courses_tree.configure(yscroll=scrollbar.set)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Remove course button
ttk.Button(current_courses_tab, text="Remove Selected Course",
command=self.remove_course_from_selected_professor).pack(fill=tk.X, pady=10)
# Available courses tab
available_courses_tab = ttk.Frame(self.prof_courses_notebook, style='TFrame')
self.prof_courses_notebook.add(available_courses_tab, text="Available Courses")
# Create a treeview for available courses
self.available_courses_tree = ttk.Treeview(available_courses_tab, columns=("code", "name"), show="headings")
self.available_courses_tree.heading("code", text="Course Code")
self.available_courses_tree.heading("name", text="Course Name")
self.available_courses_tree.column("code", width=100)
self.available_courses_tree.column("name", width=300)
self.available_courses_tree.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# Add a scrollbar
scrollbar = ttk.Scrollbar(available_courses_tab, orient=tk.VERTICAL, command=self.available_courses_tree.yview)
self.available_courses_tree.configure(yscroll=scrollbar.set)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Add course button
ttk.Button(available_courses_tab, text="Add Selected Course",
command=self.add_course_to_selected_professor).pack(fill=tk.X, pady=10)
def setup_professors_frame(self):
"""Set up the professor schedules frame"""
# Title
ttk.Label(self.professors_frame, text="Professor Schedules",
style='Heading.TLabel').pack(pady=(0, 20))
# Professor selection with improved layout
select_frame = ttk.Frame(self.professors_frame, style='TFrame')
select_frame.pack(fill=tk.X, pady=10)
# Create a grid layout for better organization
select_frame.columnconfigure(0, weight=0) # Label
select_frame.columnconfigure(1, weight=1) # Combobox
select_frame.columnconfigure(2, weight=0) # Buttons
# Row 1: Professor selection
ttk.Label(select_frame, text="Select Professor:").grid(row=0, column=0, padx=5, pady=5, sticky=tk.W)
self.professor_var = tk.StringVar()
self.professor_combo = ttk.Combobox(select_frame, textvariable=self.professor_var, width=30)
self.professor_combo.grid(row=0, column=1, padx=5, pady=5, sticky=tk.W+tk.E)
# Button frame for row 1
button_frame = ttk.Frame(select_frame, style='TFrame')
button_frame.grid(row=0, column=2, padx=5, pady=5, sticky=tk.E)
ttk.Button(button_frame, text="View Schedule",
command=self.view_professor_schedule).pack(side=tk.LEFT, padx=2)
ttk.Button(button_frame, text="Refresh",
command=self.refresh_professors).pack(side=tk.LEFT, padx=2)
# Row 2: Export button
export_frame = ttk.Frame(self.professors_frame, style='TFrame')
export_frame.pack(fill=tk.X, pady=(0, 10))
ttk.Button(export_frame, text="Export All Schedules to Excel",
command=self.export_professor_schedule_to_excel).pack(anchor=tk.E, padx=5)
# Schedule display with improved styling
schedule_frame = ttk.LabelFrame(self.professors_frame, text="Professor Schedule", style='TFrame')
schedule_frame.pack(fill=tk.BOTH, expand=True, pady=10)
# Use a monospaced font for better schedule display
self.professor_text = scrolledtext.ScrolledText(
schedule_frame,
wrap=tk.WORD,
font=('Courier New', 10)
)
self.professor_text.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# Initial message with better formatting
self.professor_text.insert(tk.END,
"No professor schedules available. Please generate a timetable first using the Generate tab.\n\n"
"Once a timetable is generated, you can view the schedule for a selected professor."
)
# Database functions
def initialize_db(self):
"""Initialize the database with sample data"""
try:
initialize_database()
self.console.insert(tk.END, "Database initialized successfully!\n")
self.status_var.set("Database initialized")
self.refresh_branches()
self.refresh_courses()
self.refresh_mapping_lists()
self.refresh_courses_for_classes()
self.update_stats()
except Exception as e:
messagebox.showerror("Error", f"Failed to initialize database: {str(e)}")
self.console.insert(tk.END, f"Error initializing database: {str(e)}\n")
self.status_var.set("Database initialization failed")
def update_stats(self):
"""Update the statistics on the home screen"""
try:
with sqlite3.connect("timetable.db") as conn:
cursor = conn.cursor()
# Count branches
cursor.execute("SELECT COUNT(*) FROM branches")
branch_count = cursor.fetchone()[0]
self.branch_count.set(str(branch_count))
# Count courses
cursor.execute("SELECT COUNT(*) FROM courses")
course_count = cursor.fetchone()[0]
self.course_count.set(str(course_count))
# Count teachers
cursor.execute("SELECT COUNT(*) FROM teachers")
teacher_count = cursor.fetchone()[0]
self.teacher_count.set(str(teacher_count))
except Exception as e:
print(f"Error updating stats: {str(e)}")
def debug_database(self):
"""Debug the database contents"""
# Redirect stdout to the database console
old_stdout = sys.stdout
sys.stdout = StdoutRedirector(self.db_console)
try:
# Clear the console first
self.db_console.delete(1.0, tk.END)
debug_database()
self.status_var.set("Database debug completed")
except Exception as e:
messagebox.showerror("Error", f"Failed to debug database: {str(e)}")
self.db_console.insert(tk.END, f"Error debugging database: {str(e)}\n")
self.status_var.set("Database debug failed")
finally:
# Restore stdout
sys.stdout = old_stdout
def verify_database(self):
"""Verify the database integrity"""
# Redirect stdout to the database console
old_stdout = sys.stdout
sys.stdout = StdoutRedirector(self.db_console)
try:
# Clear the console first
self.db_console.delete(1.0, tk.END)
result = verify_database_integrity()
if result:
self.status_var.set("Database verification passed")
else:
self.status_var.set("Database verification failed")
except Exception as e:
messagebox.showerror("Error", f"Failed to verify database: {str(e)}")
self.db_console.insert(tk.END, f"Error verifying database: {str(e)}\n")
self.status_var.set("Database verification failed")
finally:
# Restore stdout
sys.stdout = old_stdout
def refresh_branches(self):
"""Refresh the branch comboboxes"""
try:
with sqlite3.connect("timetable.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT branch_name FROM branches ORDER BY branch_name")
branches = [branch[0] for branch in cursor.fetchall()]
# Update all branch comboboxes
self.prof_branch_combo['values'] = branches
self.course_branch_combo['values'] = branches
self.mapping_branch_combo['values'] = branches
if branches:
self.prof_branch_combo.current(0)
self.course_branch_combo.current(0)
self.mapping_branch_combo.current(0)
except Exception as e:
messagebox.showerror("Error", f"Failed to refresh branches: {str(e)}")
def add_branch(self):
"""Add a new branch to the database"""
branch_name = self.branch_name_var.get().strip()
if not branch_name:
messagebox.showerror("Error", "Branch name cannot be empty")
return
try:
# Redirect stdout to the database console
old_stdout = sys.stdout
sys.stdout = StdoutRedirector(self.db_console)
add_branch(branch_name)
self.branch_name_var.set("") # Clear the entry
self.refresh_branches()
self.update_stats()
self.status_var.set(f"Branch '{branch_name}' added")
except Exception as e:
messagebox.showerror("Error", f"Failed to add branch: {str(e)}")
self.db_console.insert(tk.END, f"Error adding branch: {str(e)}\n")
finally:
# Restore stdout
sys.stdout = old_stdout
def add_professor(self):
"""Add a new professor to the database"""
prof_name = self.prof_name_var.get().strip()
branch_name = self.prof_branch_var.get()
if not prof_name:
messagebox.showerror("Error", "Professor name cannot be empty")
return
if not branch_name:
messagebox.showerror("Error", "Please select a branch")
return
try:
# Redirect stdout to the database console
old_stdout = sys.stdout
sys.stdout = StdoutRedirector(self.db_console)
add_professor(prof_name, branch_name)
self.prof_name_var.set("") # Clear the entry
self.update_stats()
self.status_var.set(f"Professor '{prof_name}' added")
self.refresh_mapping_lists()
except Exception as e:
messagebox.showerror("Error", f"Failed to add professor: {str(e)}")
self.db_console.insert(tk.END, f"Error adding professor: {str(e)}\n")
finally:
# Restore stdout
sys.stdout = old_stdout
# Course management functions
def add_course(self):
"""Add a new course to the database"""
course_code = self.course_code_var.get().strip().upper()
course_name = self.course_name_var.get().strip()
branch_name = self.course_branch_var.get()
if not course_code or not course_name:
messagebox.showerror("Error", "Course code and name cannot be empty")
return
if not branch_name:
messagebox.showerror("Error", "Please select a branch")
return
try:
# Redirect stdout to the course console
old_stdout = sys.stdout
sys.stdout = StdoutRedirector(self.course_console)
# Add course with branch
with sqlite3.connect("timetable.db") as conn:
cursor = conn.cursor()
cursor.execute(
"INSERT INTO courses (course_code, course_name, branch_name) VALUES (?, ?, ?)",
(course_code, course_name, branch_name)
)
# Initialize course_classes with default value (2 classes per week)
cursor.execute(
"INSERT INTO course_classes (course_code, num_classes) VALUES (?, ?)",
(course_code, 2)
)
conn.commit()
self.course_console.insert(tk.END, f"✅ Course '{course_code}' added successfully!\n")
self.course_code_var.set("") # Clear the entries
self.course_name_var.set("")
self.update_stats()
self.status_var.set(f"Course '{course_code}' added")
self.refresh_courses()
self.refresh_mapping_lists()
except Exception as e:
messagebox.showerror("Error", f"Failed to add course: {str(e)}")
self.course_console.insert(tk.END, f"Error adding course: {str(e)}\n")
finally:
# Restore stdout
sys.stdout = old_stdout
def refresh_courses(self):