-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQWhizz Math - V2.7.0.py
More file actions
1571 lines (1292 loc) · 124 KB
/
Copy pathQWhizz Math - V2.7.0.py
File metadata and controls
1571 lines (1292 loc) · 124 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
# Date Created: 05/05/2025
# Author: Jack Compton
# Purpose: GUI application for Flow Computing that helps students with their mathematics.
# BEFORE USE:
# Windows: Open Command Prompt and run:
# pip install customtkinter pillow fonttools
#
# macOS: Open Terminal and run:
# brew install python-tk && pip install customtkinter pillow fonttools
#
# Linux: Open Terminal and run:
# sudo apt install python3-tk && pip install customtkinter pillow fonttools
#
# Once these packages are installed, the program is ready to use.
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import customtkinter as CTk
from AppData.CTkScrollableDropdown import *
from PIL import Image, ImageTk
from AppData.fpdf import FPDF
from AppData.fpdf.enums import TableCellFillMode
from AppData.fpdf.fonts import FontFace
from datetime import datetime
import json, time, random, os, platform, subprocess
class PDF(FPDF):
def __init__(self):
# Initialise the parent FPDF class and its attributes for page orientation and size.
super().__init__(orientation="portrait", format="A4") # "Super()" allows a subclass, in this case "PDF", to inherit methods and attributes from the parent class (superclass) "FPDF".
self.set_auto_page_break(auto=True, margin=20) # Automatically add a new page if content overflows.
def header(self):
# Add logo on the top left
self.image("AppData/Images/qw_logo.png", 15, 7, 25)
# Centred title image
img_width = 70
x_center = (self.w - img_width) / 2 # Calculate centre x position.
self.image("AppData/Images/scoreboard_logo.png", x=x_center, y=9, w=img_width)
# Line break to move below header elements
self.ln(20)
def footer(self):
self.set_y(-15) # Move 1.5 cm from the bottom.
self.set_font("helvetica", style="I", size=10)
self.set_text_color("#75adf7") # Set text colour to blue.
# Print current date and time.
current_datetime = datetime.now().strftime("%d %B %Y, %I:%M %p") # Get current date and time.
self.set_x(15) # Align the left cell with the table's left-side X position, moved right by 15 mm.
self.cell(0, 10, f"Generated on: {current_datetime}", align="L") # Print current date and time on the left.
# Print current page number and total pages.
self.set_x(-20) # Align the right cell with the table's right-side X position, moved left by 20 mm.
self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", align="R") # Print page number on the right. "{nb}" is a placeholder that gets replaced with the total page count by "alias_nb_pages()".
def scoreboard_table(self, data, headings):
# Position cursor before starting the table content
self.set_y(32)
self.set_font("helvetica", size=10)
self.set_text_color("#000000") # Set text colour to black
self.set_draw_color("#6aa5db") # Set draw (table border) colour to light blue
self.set_line_width(0.25)
# Style for heading row
heading_style = FontFace(emphasis="BOLD", color=255, fill_color="#87bcf4")
# Create a styled table using fpdf2's context manager
with self.table(
borders_layout="NO_HORIZONTAL_LINES",
cell_fill_color=(224, 235, 255), # Alternate cell colour for colour banding.
cell_fill_mode=TableCellFillMode.ROWS, # Fill alternate cells for colour banding.
col_widths=(75, 290, 125, 120, 100, 100), # Set column widths
headings_style=heading_style, # Apply heading style
line_height=6, # Set line height
text_align=("CENTER", "LEFT", "CENTER", "CENTER", "CENTER", "CENTER"), # Set text alignment for each column
width=180, # Set table width
) as table:
# Create header cells
heading_row = table.row()
for i, heading in enumerate(headings):
heading_row.cell(" " + heading if i == 1 else heading) # Add left-side padding only to the "Username" column
# Create each row of data from the scoreboard file
for data_row in data:
row = table.row()
for i, datum in enumerate(data_row):
row.cell(" " + str(datum) if i == 1 else str(datum)) # Add left-side padding only to the "Username" column
class Tools:
# Constructor for the "Tools" class, which takes an instance of the class names as a parameter and stores it in their unique attributes.
# This allows attributes and methods defined in the "Home" class, for example, to be accessed from within the "Tools" class.
def __init__(self, about_instance, scoreboard_instance, completion_instance, quiz_instance, homepage_instance):
self.about = about_instance # Store a reference to the "About" class instance.
self.scoreboard = scoreboard_instance # Store a reference to the "Scoreboard" class instance.
self.completion = completion_instance # Store a reference to the "Completion" class instance.
self.quiz = quiz_instance # Store a reference to the "Quiz" class instance.
self.home = homepage_instance # Store a reference to the "Home" class instance.
# Method for clearing all widgets or clearing specified widgets (column, row).
def clear_widget(self, procedure, all_widgets, column, row, command):
global username, difficulty_num, questions
if command != None: command() # Go to the specified procedure from passed command if it is specified.
if all_widgets == True:
# Clear all page content
for widget in main_window.winfo_children():
widget.destroy()
if procedure != None: procedure() # Go to the specified procedure from button command if it is specified.
elif all_widgets == False:
# Find all widgets in the specified row and column.
for widget in main_window.grid_slaves(column=column, row=row):
widget.destroy() # Destroy the widgets occupying the specified space.
# Method for handling mouse button 1 click events.
def on_mbtn1_click(self, origin, element):
if origin == "Scoreboard":
if element == "Scrollbar":
self.scoreboard.scrollbar.configure(button_color=BUTTON_CLICKED, button_hover_color=BUTTON_CLICKED) # Change the scrollbar button colour to a darker blue when clicked and/or held.
# Method for handling mouse button 1 release events.
def on_mbtn1_release(self, origin, element):
if origin == "Scoreboard":
if element == "Scrollbar":
self.scoreboard.scrollbar.configure(button_color=BUTTON_FG, button_hover_color=BUTTON_HOVER) # Change the scrollbar button colour back to a light blue when released.
# Method for handling errors and preventing repeated code.
def error_control(self, file_name, file_dir, file_data, control):
global users, settings, timer, deletion_history_states
# Temporary storage mode
if control == "Temporary":
if file_data == "users": users = [] # If the "file_data" variable is set to "users", make "users" as an empty list.
elif file_data == "settings":
settings = default_settings # If the "file_data" variable is set to "settings", make "settings" store the default settings.
timer.set(settings.get("enable_timer")) # Set the timer to the value stored in the "default_settings" dictionary.
deletion_history_states.set(settings.get("deletion_history_states")) # Set the deletion history states to the value stored in the "default_settings" dictionary.
return # Exit the function after handling the error control for temporary storage mode.
# Function for loading the "users" and "settings" lists from the JSON files.
def load_details(self, file_name, file_dir, file_data):
global data_loaded, users, settings, timer, deletion_history_states
# Check if the JSON file exists. If not, create it.
if not os.path.exists(file_dir):
response1 = messagebox.askyesno("File Not Found", f"The {file_name} file cannot be found. Do you want to create a new one?")
if response1 == True: # If the user chooses to create a new file, proceed.
try:
# Create a new JSON file with an empty list.
with open(file_dir, "w") as file: # Create a new JSON file with an empty list if the file doesn't already exist.
if file_data == "users": json.dump([], file) # Write an empty list to the new JSON file.
elif file_data == "settings": json.dump(default_settings, file, indent=4) # Write an empty list to the new JSON file.
file.close() # Close the file after writing to it.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = True # Set the "data_loaded" variable to True, so that the program doesn't reload data again from the JSON file before it is accessed.
# Error control for instances such as the file being inaccessible or lacking the permission to read/write it.
except IOError as io_error:
messagebox.showerror("File Error", f"An error occurred while creating the {file_name} file, program will run in temporary storage mode.\n\n{io_error}\n\n{full_directory}") # Show an error message if the file cannot be created.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
# Error control for any other errors.
except Exception as e:
messagebox.showerror("Error", f"An error occurred while creating the {file_name} file, program will run in temporary storage mode.\n\n{e}\n\n{full_directory}") # Show an error message if the file cannot be created due to an unexpected error.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
return
# If the user chooses not to create a new file, run the program in temporary storage mode.
else:
messagebox.showwarning("Temporary Storage Mode", f"The program will run in temporary storage mode until the {file_name} file is created or replaced.\n\n{full_directory}") # Show a warning message if the user does not want to create a new file.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
return
# If the JSON file exists, try to load the data from it, and if the file isn't in JSON format, it will raise a JSONDecodeError. If there are any other errors, such as the file being inaccessible, it will raise an IOError.
try:
with open(file_dir, "r") as file: # Open the JSON file in read mode ("r").
data = json.load(file) # Load the details from the JSON file into the "users" list.
if file_data == "users":
if not isinstance(data, list): # Check if the loaded data is a list.
raise json.JSONDecodeError("Expected a list", doc=str(data), pos=0) # Raise an error if the loaded scoreboard data is not a list, simulating a JSON decode error.
users.clear() # Clear the list to prevent duplicate entries.
users = data # Assign the loaded data to the "users" list.
# Check all entries to make sure they each contain 6 elements.
for i, details in enumerate(users):
if len(details) < 6:
response2 = messagebox.askyesno("Invalid Data", f"The {file_name} file contains invalid data. Entry #{i+1} is incomplete (expected 6 elements, got {len(details)}).\nWould you like to remove this entry?")
if response2:
# Remove invalid entries and update the scoreboard file.
valid_users = [details for details in users if len(details) == 6]
with open(file_dir, "w") as file: # Open the JSON file in write mode ("w").
json.dump(valid_users, file, indent=4) # Write the valid users to the JSON file.
file.close() # Close the file after writing to it.
break
else:
messagebox.showwarning("Invalid Data", f"The program will run in temporary storage mode until the {file_name} file is fixed.\n\n{full_directory}")
users = [details for details in users if len(details) == 6] # Keep only the valid entries in memory.
break
elif file_data == "settings":
if not isinstance(data, dict): # Check if the loaded data is a dictionary.
raise json.JSONDecodeError("Expected a dict", doc=str(data), pos=0) # Raise an error if the loaded settings data is not a dictionary, simulating a JSON decode error.
if "enable_timer" not in data or "deletion_history_states" not in data:
raise json.JSONDecodeError("Missing required keys in settings", doc=str(data), pos=0)
settings.clear() # Clear the list to prevent duplicate entries.
settings = data # Modify the "settings" dictionary in place.
timer.set(settings.get("enable_timer", default_settings["enable_timer"])) # Set the timer to the value stored in the "settings" dictionary, or the default value if not found.
deletion_history_states.set(settings.get("deletion_history_states", default_settings["deletion_history_states"])) # Set the deletion history states to the value stored in the "settings" dictionary, or the default value if not found.
data_loaded = True # Set the "data_loaded" variable to True, so that the program doesn't reload data again from the JSON file before it is accessed.
# Error control for instances such as the JSON file having invalid data, having incorrect formatting, or being corrupted.
except json.JSONDecodeError as JSONDecodeError:
response3 = messagebox.askyesno("File Error", f"Failed to decode JSON data. The {file_name} file may be corrupted or improperly formatted. Do you want to replace it?\n\n{JSONDecodeError}\n\n{full_directory}") # Show an error message if the JSON file cannot be decoded, asking the user if they want to replace the file.
if response3 == True:
try:
with open(file_dir, "w") as file: # Open the shortened_directory file in write mode ("w").
if file_data == "users": json.dump([], file) # Write an empty list to the new JSON file.
elif file_data == "settings": json.dump(default_settings, file, indent=4) # Write an empty list to the new JSON file.
file.close() # Close the file after writing to it.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = True # Set the "data_loaded" variable to True, so that the program doesn't reload data again from the JSON file before it is accessed.
messagebox.showinfo("File Replaced", f"The JSON {file_name} file has been successfully replaced and restored to defaults.\n\n{full_directory}")
# Error control for instances such as the file being inaccessible or lacking the permission to read/write it.
except IOError as io_error:
messagebox.showerror("File Error", f"An error occurred while replacing the {file_name} file, program will run in temporary storage mode.\n\n{io_error}\n\n{full_directory}") # Show an error message if the file cannot be replaced.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
# Error control for any other exceptions that may occur.
except Exception as e:
messagebox.showerror("Unexpected Error", f"An unexpected error occurred while replacing the {file_name} file, program will run in temporary storage mode.\n\n{e}\n\n{full_directory}") # Show an error message if there is an unexpected error.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
return
else:
messagebox.showwarning("Temporary Storage Mode", f"The program will run in temporary storage mode until the {file_name} file is created or replaced.\n\n{full_directory}") # Show a warning message if the user does not want to create a new file.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
return
# Error control for instances such as the file being inaccessible or lacking the permission to read it.
except IOError as io_error:
messagebox.showwarning("File Error", f"An error occurred while reading the {file_name} file, program will run in temporary storage mode.\n\n{io_error}\n\n{full_directory}") # Show an error message if the file cannot be read.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
# Error control for any other exceptions that may occur.
except Exception as e:
response3 = messagebox.askyesno("Unexpected Error", f"An unexpected error occurred while reading the {file_name} file. Do you want to replace it?") # Show an error message if there is an unexpected error.
if response3 == True:
try:
with open(file_dir, "w") as file: # Open the shortened_directory file in write mode ("w").
if file_data == "users": json.dump([], file) # Write an empty list to the new JSON file.
elif file_data == "settings": json.dump(default_settings, file, indent=4) # Write an empty list to the new JSON file.
file.close() # Close the file after writing to it.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = True # Set the "data_loaded" variable to True, so that the program doesn't reload data again from the JSON file before it is accessed.
messagebox.showinfo("File Replaced", f"The JSON {file_name} file has been successfully replaced and restored to defaults.\n\n{full_directory}")
# Error control for instances such as the file being inaccessible or lacking the permission to read/write it.
except IOError as io_error:
messagebox.showerror("File Error", f"An error occurred while replacing the {file_name} file, program will run in temporary storage mode.\n\n{io_error}\n\n{full_directory}") # Show an error message if the file cannot be replaced.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
# Error control for any other exceptions that may occur.
except Exception as e:
messagebox.showerror("Unexpected Error", f"An unexpected error occurred while replacing the {file_name} file, program will run in temporary storage mode.\n\n{e}\n\n{full_directory}") # Show an error message if there is an unexpected error.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
return
else:
messagebox.showwarning("Temporary Storage Mode", f"The program will run in temporary storage mode until the {file_name} file is created or replaced.\n\n{full_directory}") # Show a warning message if the user does not want to create a new file.
self.error_control(file_name, file_dir, file_data, "Temporary") # Call the error control function to handle temporary storage mode, which will clear the "users" list and add the default values to the "settings" dictionary.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
return
# Method for saving details specific to the specified window.
def save_details(self, procedure, origin, scenario, file_dir):
global ref_number, username, difficulty_num, questions, data_loaded
if origin == "Home":
if scenario == "Temporary" or scenario == "Permanent":
username = self.home.username_entry.get() # Get the username entry widget value.
difficulty_num = self.home.difficulty_slider.get() # Get the difficulty slider value.
questions = int(self.home.questions_slider.get()) # Get the questions slider value.
if scenario == "Permanent":
if users != []: # Check if the "users" list is not empty.
existing_ref_numbers = [user[0] for user in users] # Create a list of existing reference numbers from the "users" list.
else:
existing_ref_numbers = []
# Check if the maximum number of unique reference numbers has been reached.
if len(existing_ref_numbers) >= 9000: # Check if 9000 possible unique 4-digit ref numbers from 1000 to 9999 have been generated.
messagebox.showwarning("Maximum Scores Reached", "No more unique reference numbers can be generated.\nPlease delete old user scores to add new ones.")
self.clear_widget(self.scoreboard.setup_scoreboard, True, None, None, None) # Clear all current widgets (passing "True" clears all widgets), then go back to the home page.
return
else:
while True:
ref_number = random.randint(1000, 9999) # Generate a random number from 1000 to 9999 and put this value into the "ref_number" variable.
if ref_number not in existing_ref_numbers: # Check that the generated reference number doesn't already exist.
break
if procedure == "Quiz":
self.clear_widget(self.quiz.setup_quiz, True, None, None, None)
elif procedure == "Scoreboard":
self.clear_widget(self.scoreboard.setup_scoreboard, True, None, None, None)
else:
return # If the procedure is not "Quiz" or "Scoreboard", do nothing and return.
elif origin == "Completion" or origin == "Scoreboard":
try:
with open(file_dir, "w") as file: # Open the file in write mode ("w"). If it doesn't exist, a new file will be created.
json.dump(users, file, indent=4) # Dump the entries from the "users" list into the JSON file.
file.close() # Close the file after writing to it.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
except IOError as io_error: # Error control for instances such as the file being inaccessible or lacking the permission to write to it.
messagebox.showerror("File Error", f"Failed to write to 'scoreboard.json'. Check file permissions, disk space, and ensure the file is not in use.\n\n{io_error}\n\n{full_directory}") # Show an error message if the file cannot be written to.
except Exception as e: # Error control for any other exceptions that may occur.
messagebox.showerror("Unexpected Error", f"An unexpected error occurred while writing to 'scoreboard.json'.\n\n{e}\n\n{full_directory}") # Show an error message if there is an unexpected error.
elif origin == "Menubar":
global settings
try:
settings = {"enable_timer": timer.get(), "deletion_history_states": deletion_history_states.get()}
with open(file_dir, "w") as file: # Open the file in write mode ("w"). If it doesn't exist, a new file will be created.
json.dump(settings, file, indent=4) # Dump the entries from the "users" list into the JSON file.
file.close() # Close the file after writing to it.
data_loaded = False # Set the "data_loaded" variable to false, so that the program will reload data from the JSON file when it next needs to be accessed.
except IOError as io_error: # Error control for instances such as the file being inaccessible or lacking the permission to write to it.
messagebox.showerror("File Error", f"Failed to write to 'settings.json'. Check file permissions, disk space, and ensure the file is not in use.\n\n{io_error}\n\n{full_directory}") # Show an error message if the file cannot be written to.
except Exception as e: # Error control for any other exceptions that may occur.
messagebox.showerror("Unexpected Error", f"An unexpected error occurred while writing to 'settings.json'.\n\n{e}\n\n{full_directory}") # Show an error message if there is an unexpected error.
# Method for printing details into a PDF.
def print_details(self, selections):
data = []
if selections == "all" and data_loaded == False: # Check if the data has been loaded from the JSON file only if all scores are being printed.
self.load_details("scoreboard", SCOREBOARD_FILE_PATH, "users")
if selections == "all":
if users == []: # Check if the "users" list is empty.
response1 = messagebox.askyesno("No Scores Recorded", "There are no recorded scores to print.\nWould you still like to print out a blank scoreboard table?")
if response1 == False:
return
else:
# Load all data from JSON file.
with open("AppData/scoreboard.json", "r") as file:
data = json.load(file) # Load the details from the JSON file into the "data" list.
else:
if selections != []: # Check if the "selections" list is not empty.
# Match the selected reference numbers in the treeview widget to the user reference numbers (user[0]) in the "users" list.
# This creates a new list of users that match the selected user reference numbers so that only the selected users are printed on the scoreboard PDF.
# "user[0]" is converted to a string to ensure that it matches the format used in the Treeview selection, which is passed as a string.
data = [user for user in users if str(user[0]) in selections] # If selections are provided, use them as the scoreboard data directly.
else:
messagebox.showwarning("No Scores Selected", "Please select at least one score to print.")
return
self.scoreboard.tree.selection_set("") # Clear the current selection in the Treeview widget.
self.reset_details("Scoreboard") # Reset the "sel_reference_numbers" list in the Scoreboard class so that the list is ready for new selections.
# Initialise PDF.
pdf = PDF()
pdf.alias_nb_pages() # Enable total page count placeholder.
pdf.add_page() # Start with first page.
# Define table headings.
headings = ["Ref #", "Username", "Difficulty", "Questions", "Time", "Score"]
# Generate the table on the PDF.
pdf.scoreboard_table(data, headings)
try:
# Ask the user where they would like to save the PDF file.
file_path = filedialog.asksaveasfilename(defaultextension=".pdf", initialdir=initial_pdf_directory, initialfile=INITIAL_PDF_NAME, filetypes=[("PDF files", "*.pdf")], title="Save Scoreboard As")
saved_file_name = os.path.basename(file_path) # Get the name of the saved file using "os.path.basename", which would be any characters after the last slash (/) in the file path.
# If the user chose a location, save the PDF to that location.
if file_path:
pdf.output(file_path) # Save the PDF to the specified file path.
words = "scoreboard has" if selections == "all" else "selected score has" if len(selections) == 1 else "selected scores have" # Determine whether to use "scoreboard has", "selected score has", or "selected scores have" in the message box based on the number of selected items.
messagebox.showinfo("Print Successful", f"The {words} been successfully printed to '{saved_file_name}'.\n\n{file_path}")
# Ask the user if they want to print the PDF.
response2 = messagebox.askyesno("Send PDF to Printer", "Would you like to send the PDF to a printer now?")
# If the user chooses to send the PDF to a printer, proceed with printing.
if response2 == True:
try:
if operating_system == "Windows": # Check if the operating system is Windows.
os.startfile(file_path, "print") # Send the PDF file to the default printer.
elif operating_system == "Linux": # Check if the operating system is Linux.
subprocess.run(["lp", file_path], check=True) # Use the "lp" command to send the PDF file to the default printer.
elif operating_system == "Darwin": # Check if the operating system is macOS.
subprocess.run(["lp", file_path], check=True) # Use the "lp" command to send the PDF file to the default printer.
else:
messagebox.showwarning("Unsupported OS", f"Your operating system ({operating_system}) is not supported for printing. Please print the PDF file manually.\n\n{file_path}") # Show a warning message if the operating system is not supported for printing.
except Exception as e:
messagebox.showerror("Printing Error", f"An error occurred while printing the PDF file.\n\n{e}\n\n{file_path}") # Show an error message if there is an error while printing the PDF file.
else:
return
# Error control for instances such as the file being inaccessible or lacking the permission to write to it.
except IOError as io_error:
messagebox.showerror("File Error", f"Failed to write to 'QWhizz Math Scoreboard.pdf'. Check file permissions, disk space, and ensure the file is not in use.\n\n{io_error}\n\n{file_path}") # Show an error message if the file cannot be written to.
# Error control for any other exceptions that may occur.
except Exception as e:
messagebox.showerror("Unexpected Error", f"An unexpected error occurred while writing to 'QWhizz Math Scoreboard.pdf'.\n\n{e}\n\n{file_path}") # Show an error message if there is an unexpected error.
# Method for deleting details from the "users" list.
def delete_details(self, selections):
global users
users_to_delete = [] # Create an empty list to store users to delete.
if users != []: # Check if the "users" list is not empty.
if selections == "all":
response = messagebox.askyesno("Delete All Scores", "Are you sure that you want to delete all recorded scores?")
if response == True:
if deletion_history_states.get() > 0: # Check if the "deletion_history_states" variable is greater than 0, which means that the deletion history is enabled.
# Store all users with their indices in the "history_stack" list.
history_stack.append([(user, i) for i, user in enumerate(users)])
# Trim the stack to the specified history limit in the "deletion_history_states" variable if the amount of stacked scores exceeds the set amount of history states.
if len(history_stack) > deletion_history_states.get():
history_stack.pop(0) # Remove the last entry from the stack (corresponding to the oldest score, which has an index of 0) if the stack exceeds the history limit.
users = []
redo_stack.clear() # Clear the "redo_stack" list to prevent any redo actions directly after deletion.
self.save_details(None, "Scoreboard", None, SCOREBOARD_FILE_PATH)
self.clear_widget(self.scoreboard.setup_scoreboard, True, None, None, None)
messagebox.showinfo("Scores Deleted", "All recorded scores have been deleted.")
else:
return
else:
if selections != []: # Check if the "selections" list is not empty.
words = ["scores", "have"] if len(selections) > 1 else ["score", "has"] # Determine whether to use "scores" and "have", or "score" and "has" in the message boxes based on the number of selected items.
response = messagebox.askyesno("Delete Selected Scores", f"Are you sure that you want to delete the selected {words[0]}?")
if response == True:
# Find the users to delete and record their original index positions
users_to_delete = []
for index, user in enumerate(users):
if str(user[0]) in selections:
users_to_delete.append((user, index)) # Store (user, index) in the "users_to_delete" list
# Store all selected users with their indices in the "history_stack" list.
if deletion_history_states.get() > 0: # Check if the "deletion_history_states" variable is greater than 0, which means that the deletion history is enabled.
history_stack.append(users_to_delete.copy()) # Use .copy() to append a snapshot of the current users_to_delete list, ensuring that future modifications to the original list (like clearing it) do not affect the stored history stack.
# Trim the stack to the specified history limit in the "deletion_history_states" variable if the amount of stacked scores exceeds the set amount of history states.
if len(history_stack) > deletion_history_states.get():
history_stack.pop(0) # Remove the last entry from the stack (corresponding to the oldest score, which has an index of 0) if the stack exceeds the history limit.
# Delete users in reverse index order to avoid shifting issues.
# "users_to_delete" is a list of tuples in the form (user, index), where "index" is the user's position in the "users" list.
# Sorting this list by index in descending order ensures that when items are deleted, the positions of earlier items in the list are not affected.
# If deletions were done in ascending order, deleting an item would shift the indices of the items that follow it, leading to incorrect deletions if more than one item is deleted at once.
for user, index in sorted(users_to_delete, key=lambda x: x[1], reverse=True): # Sort by the original index ("x[1]"), which refers to the second element ("1", being the index) in each (user, index) tuple, representing the user's original position.
del users[index] # Delete the user from the "users" list at the specified index.
redo_stack.clear() # Clear the "redo_stack" list to prevent any redo actions directly after deletion.
users_to_delete.clear() # Clear the list of users to delete.
self.save_details(None, "Scoreboard", None, SCOREBOARD_FILE_PATH)
self.clear_widget(self.scoreboard.setup_scoreboard, True, None, None, None)
messagebox.showinfo("Scores Deleted", f"The selected {words[0]} {words[1]} been deleted.")
else:
return
else:
messagebox.showwarning("No Scores Selected", "Please select at least one score to delete.")
return
self.reset_details("Scoreboard") # Reset the "sel_reference_numbers" list in the Scoreboard class so that the list is ready for new selections.
else:
messagebox.showwarning("No Scores Recorded", "There are no recorded scores to delete.")
return
# Method for undoing the deletion of scores.
def undo_delete(self):
global users, history_stack
# Check if the history stack is empty.
if not history_stack:
return # If the stack is empty, do nothing and return.
last_deleted = history_stack.pop() # Retrieve the last deleted users from the history stack.
redo_stack.append([(user, index) for user, index in last_deleted]) # Store the last deleted users in the redo stack for potential future redoing.
for user, index in last_deleted: # Reinsert each user at their original index.
users.insert(index, user)
self.save_details(None, "Scoreboard", None, SCOREBOARD_FILE_PATH)
self.clear_widget(self.scoreboard.setup_scoreboard, True, None, None, None)
# Method for redoing the deletion of scores that were previously undone.
def redo_delete(self):
global users, history_stack, redo_stack
# Check if the redo stack is empty.
if not redo_stack:
return # If the redo stack is empty, do nothing and return.
last_redo = redo_stack.pop() # Retrieve the last deleted users from the redo stack.
history_stack.append([(user, index) for user, index in last_redo]) # Store the last deleted users in the history stack for potential future undoing.
# Delete users in reverse index order to avoid shifting issues.
for i, index in sorted(last_redo, key=lambda x: x[1], reverse=True): # Sort by the original index ("x[1]"), which refers to the second element ("1", being the index) in each (user, index) tuple, representing the user's original position.
del users[index] # Delete the user from the "users" list at the specified index.
self.save_details(None, "Scoreboard", None, SCOREBOARD_FILE_PATH)
self.clear_widget(self.scoreboard.setup_scoreboard, True, None, None, None)
# Method for resetting details specific to the specified window.
def reset_details(self, origin):
global username, difficulty, difficulty_num, questions
if origin == "Completion":
username = None
difficulty = None
difficulty_num = None
questions = None
elif origin == "Scoreboard":
self.scoreboard.sel_reference_numbers = []
# Method for unbinding specified key events from the main window.
def unbind_keys(self, keys):
# Unbind all key events from the main window.
for key in keys:
main_window.unbind(key)
# Method for configuring the timer label state (enabled/disabled).
## Unique identifiers are passed in "origin" to differentiate between the "Quiz" and "Completion" classes to manage their relevant timer labels.
def timer_config(self, origin, command, procedure):
if origin == "Quiz":
if command == "Enable":
return (f"Time: {self.quiz.time_string}")
elif command == "Disable":
return "Timer Disabled"
elif origin == "Quiz Menubar":
if command == "Enable":
self.quiz.timer_lbl.configure(text=f"Time: {self.quiz.time_string}")
elif command == "Disable":
self.quiz.timer_lbl.configure(text="Timer Disabled")
if procedure != None: procedure()
elif origin == "Completion":
if command == "Enable":
return (f"Total Time: {self.quiz.total_time}")
elif command == "Disable":
return "Timer Disabled"
class About:
# Contstructor for the "About" class, which sets up the full window for the "About" page.
def __init__(self, tools_instance, scoreboard_instance, completion_instance, quiz_instance, homepage_instance):
self.tools = tools_instance # Store a reference to the "Tools" class instance.
self.scoreboard = scoreboard_instance # Store a reference to the "Scoreboard" class instance.
self.completion = completion_instance # Store a reference to the "Completion" class instance.
self.quiz = quiz_instance # Store a reference to the "Quiz" class instance.
self.home = homepage_instance # Store a reference to the "Home" class instance.
def setup_about(self, origin):
# Disable the main window to prevent interaction with it while the about window is open.
main_window.attributes("-disabled", True)
self.unpause_quiz = False # Set the flag to indicate that the quiz should not be unpaused when the "About" window is closed.
if origin == "Quiz" and quiz_paused == False:
self.quiz.pause_quiz() # Pause the quiz if the "About" window is opened from the "Quiz" window.
self.unpause_quiz = True # Set the flag to indicate that the quiz should be unpaused when the "About" window is closed.
# Create a top-level window (separate from the main window).
self.about_window = Toplevel(main_window, bg=MAIN_WINDOW_BG)
self.about_window.withdraw() # Withdraw the window so that it is not shown immediately.
if os.path.exists("AppData/Images/icon.png"): # Check if the icon file exists before setting it.
self.about_window.iconphoto(False, PhotoImage(file="AppData/Images/icon.png")) # Set the title bar icon for the "About" window.
self.about_window.title("About")
self.about_window.columnconfigure(0, weight=0, minsize=300)
self.about_window.resizable(False, False)
self.about_window.update_idletasks() # Process any pending events for the window to make sure the geometry info is up-to-date before calculating the centre position later.
# Centre the "About" window above the main window.
self.x = main_window.winfo_x() + main_window.winfo_width() // 2 - self.about_window.winfo_width() // 2 - 60
self.y = main_window.winfo_y() + main_window.winfo_height() // 2 - self.about_window.winfo_height() // 2 + 56
self.about_window.geometry(f"+{self.x}+{self.y}")
self.about_window.transient(main_window) # Keep on top of parent window (main_window)
self.about_window.focus() # Set focus to the "About" window so that it is ready for user interaction.
# Create a frame inside the "About" window to hold the "about" details label.
self.about_frame = CTk.CTkFrame(self.about_window, fg_color=FRAME_FG, corner_radius=10)
self.about_frame.grid(row=0, column=0, padx=10, pady=(10,5), sticky=EW)
self.about_frame.columnconfigure(0, weight=0, minsize=300)
# Add program details and a close button.
CTk.CTkLabel(self.about_frame, text=f"QWhizz Math\nVersion {APP_VERSION}\nMade by Jack Compton", font=(DEFAULT_FONT, 14, "bold"), text_color=FONT_COLOUR, justify="center").grid(row=0, column=0, sticky=EW, padx=10, pady=(20))
CTk.CTkButton(self.about_window, text="Close", command=lambda: self.close(), font=(DEFAULT_FONT, 14, "bold"), height=30, corner_radius=10, fg_color=BUTTON_FG, hover_color=BUTTON_HOVER).grid(row=1, column=0, sticky=EW, padx=10, pady=(5,10))
# Show the "About" window after setting its position and size and adding its contents. This prevents the window from flickering when it is created and shown.
self.about_window.deiconify()
# Override the window close (X) button behavior so that the main window is enabled again when the about window is closed using this button.
self.about_window.protocol("WM_DELETE_WINDOW", lambda: self.close())
# Bind the "esc" key to the "close" function so that the window can be closed by pressing "esc".
self.about_window.bind("<Escape>", lambda e: self.close()) # Prevent an argument error by using "e" after "lambda" to accept (but ignore) the event passed by bind, keeping close() parameterless.
def close(self): # Add "event" parameter to allow for "event" to be passed when the binded "esc" key is pressed (though the bind doesn't include an event).
self.about_window.unbind("<Escape>") # Unbind the "esc" key from the "close" function so that "esc" can be used for other purposes later.
main_window.attributes("-disabled", False) # Re-enable the main window so that it can be interacted.
if self.unpause_quiz == True:
self.quiz.unpause_quiz() # Unpause the quiz if the "About" window was opened from the "Quiz" window and the quiz was previously running (not paused).
self.unpause_quiz = False # Reset the flag to indicate that the quiz should not be unpaused when the "About" window is closed.
self.about_window.destroy()
class Scoreboard:
# Constructor for the "Scoreboard" class, which takes an instance of the class names as a parameter and stores it in their unique attributes.
# This allows attributes and methods defined in the "Home" class, for example, to be accessed from within the "Scoreboard" class.
def __init__(self, tools_instance, about_instance, completion_instance, quiz_instance, homepage_instance):
self.tools = tools_instance # Store a reference to the "Tools" class instance.
self.about = about_instance # Store a reference to the "About" class instance.
self.completion = completion_instance # Store a reference to the "Completion" class instance.
self.quiz = quiz_instance # Store a reference to the "Quiz" class instance.
self.home = homepage_instance # Store a reference to the "Home" class instance.
self.sel_reference_numbers = [] # Create a new list to store the selected reference numbers from the treeview widget.
# Function for handling the treeview items being selected or unselected ("<<TreeviewSelect>>" event is generated for both).
def on_item_selected(self, event):
selected_items = self.tree.selection()
self.sel_reference_numbers = [] # Clear the "sel_reference_numbers" list to ensure it only contains the currently selected items.
# If items are selected, i.e. at least one item is selected in the treeview, then proceed to get the reference numbers of the selected items.
if selected_items:
# Loop through each selected item in the treeview.
for item_id in selected_items:
sel_ref_number = str(self.tree.item(item_id, "values")[0]) # Get the reference number of the selected item.
self.sel_reference_numbers.append(sel_ref_number) # Append the selected reference number to the "sel_reference_numbers" list.
def setup_scoreboard(self):
# Set width for columns 0-1 (2 total) in the main window. Positive weight means the column will expand to fill the available space.
main_window.columnconfigure(0, weight=1, minsize=850)
main_window.columnconfigure(1, weight=1, minsize=0)
# Set up the menu bar.
scoreboard_menubar = Menu(main_window) # Create a new menu bar.
file_menu = Menu(scoreboard_menubar, tearoff=0, activebackground=MENU_HOVER, activeforeground=MENU_ACTIVE_FG)
scoreboard_menubar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Print Selected", accelerator="Ctrl+P", command=lambda: self.tools.print_details(self.sel_reference_numbers))
file_menu.add_command(label="Print All", accelerator="Ctrl+Shift+P", command=lambda: self.tools.print_details("all"))
file_menu.add_command(label="Delete Selected", accelerator="Del", command=lambda: self.tools.delete_details(self.sel_reference_numbers))
file_menu.add_command(label="Delete All", accelerator="Shift+Del", command=lambda: self.tools.delete_details("all"))
edit_menu = Menu(scoreboard_menubar, tearoff=0, activebackground=MENU_HOVER, activeforeground=MENU_ACTIVE_FG)
scoreboard_menubar.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Undo Delete", accelerator="Ctrl+Z", command=lambda: self.tools.undo_delete())
edit_menu.add_command(label="Redo Delete", accelerator="Ctrl+Shift+Z", command=lambda: self.tools.redo_delete())
settings_menu = Menu(scoreboard_menubar, tearoff=0, activebackground=MENU_HOVER, activeforeground=MENU_ACTIVE_FG)
scoreboard_menubar.add_cascade(label="Settings", menu=settings_menu)
timer_settings = Menu(scoreboard_menubar, tearoff=0, activebackground=MENU_HOVER, activeforeground=MENU_ACTIVE_FG)
settings_menu.add_cascade(menu=timer_settings, label="Timer")
timer_settings.add_radiobutton(label="Enabled", variable=timer, value=True, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH))
timer_settings.add_radiobutton(label="Disabled", variable=timer, value=False, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH))
history_settings = Menu(scoreboard_menubar, tearoff=0, activebackground=MENU_HOVER, activeforeground=MENU_ACTIVE_FG)
settings_menu.add_cascade(menu=history_settings, label="Score Deletion History States")
history_settings.add_radiobutton(label="Disabled", variable=deletion_history_states, value=0, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH))
history_settings.add_radiobutton(label="10", variable=deletion_history_states, value=10, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH))
history_settings.add_radiobutton(label="25", variable=deletion_history_states, value=25, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH))
history_settings.add_radiobutton(label="50", variable=deletion_history_states, value=50, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH))
help_menu = Menu(scoreboard_menubar, tearoff=0, activebackground=MENU_HOVER, activeforeground=MENU_ACTIVE_FG)
scoreboard_menubar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="Documentation")
help_menu.add_command(label="About", command=lambda: self.about.setup_about("Scoreboard"))
main_window.config(menu=scoreboard_menubar)
# Bind key shortcuts to perform actions.
main_window.bind("<Control-p>", lambda e: self.tools.print_details(self.sel_reference_numbers)) # Bind the "Ctrl+P" key to the "print_details" function so that the selected receipts can be printed.
main_window.bind("<Control-Shift-P>", lambda e: self.tools.print_details("all")) # Bind the "Ctrl+Shift+P" key to the "print_details" function so that all receipts can be printed.
main_window.bind("<Delete>", lambda e: self.tools.delete_details(self.sel_reference_numbers)) # Bind the "del" key to the "delete_details" function so that the selected receipts can be deleted.
main_window.bind("<Shift-Delete>", lambda e: self.tools.delete_details("all")) # Bind the "Shift+del" key to the "delete_details" function so that all receipts can be deleted.
main_window.bind("<Control-z>", lambda e: self.tools.undo_delete()) # Bind the "Ctrl+Z" key to the "undo_delete" function so that the last deletion can be undone.
main_window.bind("<Control-Shift-Z>", lambda e: self.tools.redo_delete()) # Bind the "Ctrl+Shift+Z" key to the "redo_delete" function so that the last deletion can be redone if it was previously undone.
self.binded_keys = ["<Control-p>", "<Control-Shift-P>", "<Delete>", "<Shift-Delete>", "<Control-z>", "<Control-Shift-Z>"] # Create a list of binded keys to be used later for unbinding them when the user goes back to the home page.
# Set up a content frame to place the main scoreboard top elements inside.
top_frame1 = CTk.CTkFrame(main_window, fg_color="transparent")
top_frame1.grid(column=0, row=0, sticky=EW, padx=20, pady=(20,5))
# Set width for columns 0-2 (3 total) in top frame 1. Total minimum column width is 810px.
top_frame1.columnconfigure(0, weight=0, minsize=400)
top_frame1.columnconfigure(1, weight=0, minsize=205)
top_frame1.columnconfigure(2, weight=0, minsize=205)
# Logo creation
total_height = 70 # Height for the canvas and vertical centre position is calculated by the height of two buttons (60px) + 10px padding.
self.logo_canvas = Canvas(top_frame1, bg=MAIN_WINDOW_BG, bd=0, highlightthickness=0, width=400, height=total_height) # Create a canvas for the banner image.
self.logo_canvas.grid(column=0, row=0, rowspan=2, sticky=EW)
self.logo = Image.open("AppData/Images/logo_small.png")
self.logo = ImageTk.PhotoImage(self.logo)
self.logo_canvas.create_image(0, total_height / 2, anchor=W, image=self.logo) # Add the image to the canvas by calculating the x and y coordinates for centre-left position.
self.logo_canvas.image = self.logo
# Create the buttons.
CTk.CTkButton(top_frame1, text="Delete", font=(DEFAULT_FONT, 14, "bold"), text_color=FONT_COLOUR, command=lambda: self.tools.delete_details(self.sel_reference_numbers),
width=200, height=30, corner_radius=10, fg_color=BUTTON_FG, hover_color=BUTTON_HOVER).grid(column=1, row=0, sticky=EW, padx=(0,5), pady=(0,5))
CTk.CTkButton(top_frame1, text="Home", font=(DEFAULT_FONT, 14, "bold"), text_color=FONT_COLOUR, command=lambda: self.tools.clear_widget(self.home.setup_homepage, True, None, None, self.tools.unbind_keys(self.binded_keys)),
width=200, height=30, corner_radius=10, fg_color=BUTTON_FG, hover_color=BUTTON_HOVER).grid(column=2, row=0, sticky=EW, padx=(5,0), pady=(0,5))
CTk.CTkButton(top_frame1, text="View Answers", font=(DEFAULT_FONT, 14, "bold"), text_color=FONT_COLOUR,
width=200, height=30, corner_radius=10, fg_color=BUTTON_FG, hover_color=BUTTON_HOVER).grid(column=1, row=1, sticky=EW, padx=(0,5), pady=(5,0))
CTk.CTkButton(top_frame1, text="Retry Quiz", font=(DEFAULT_FONT, 14, "bold"), text_color=FONT_COLOUR,
width=200, height=30, corner_radius=10, fg_color=BUTTON_FG, hover_color=BUTTON_HOVER).grid(column=2, row=1, sticky=EW, padx=(5,0), pady=(5,0))
# Reload the user scores from the scoreboard.json file.
self.tools.load_details("scoreboard", SCOREBOARD_FILE_PATH, "users")
# Create a frame to hold the Treeview and scrollbar.
tree_frame = CTk.CTkFrame(main_window, fg_color="transparent")
tree_frame.grid(column=0, row=1, sticky=EW, padx=20, pady=(5,20))
treestyle = ttk.Style()
treestyle.theme_use("default")
# Configure the Treeview style for the headings.
treestyle.configure("custom.Treeview.Heading",
background=BUTTON_FG, # Background colour of the treeview headings.
foreground="white", # Text colour of the treeview headings.
font=("Segoe UI", 10,"bold"), # Font style of the treeview heading text.
padding=(0, 5, 0 ,5), # Vertical padding of 5 px above and below the text in the treeview headings.
relief="flat") # Set the relief to "ridge" to give the header less of a button-look.
# Configure the Treeview style for the field section.
treestyle.configure("custom.Treeview",
background=FRAME_FG, # Background colour of the treeview field entries.
foreground="white", # Text colour of the treeview headings.
fieldbackground=FRAME_FG, # Main background colour of the treeview field.
rowheight=30, # Height of the treeview headings.
bordercolor=BUTTON_FG, # Border colour of the treeview field.
borderwidth=1, # Border width of the treeview field.
relief="flat", # Set the relief to "flat" to give the field a flat apperanance.
font=("Segoe UI", 10)) # Font style of the treeview field text.
# Change entry selection colour using ".map()" for dynamic styling of the "selected" state.
treestyle.map("Treeview",
background=[("selected", "#78b0f4")]) # Selection background colour of the treeview field entries.
# Change the highlight colour for the headers when the mouse hovers over them.
treestyle.map("custom.Treeview.Heading",
background=[("active", BUTTON_FG)], # Background colour of the treeview headings when hovered over.
foreground=[("active", "white")]) # Text colour of the treeview headings when hovered over.
# Create a Treeview widget to display the customer receipts.
columns = ("Ref #", "Username", "Difficulty", "Questions", "Time", "Score")
self.tree = ttk.Treeview(tree_frame, columns=columns, show="headings", style="custom.Treeview", height=8, selectmode="extended")
# Define the Treeview column headings.
for col in columns:
if col == "Username":
self.tree.heading(col, text=col, anchor=W)
else:
self.tree.heading(col, text=col, anchor=CENTER)
# Set individual Treeview column widths. Total width of the Treeview is 810 pixels.
column_widths = {
"Ref #": 75,
"Username": 290,
"Difficulty": 125,
"Questions": 120,
"Time": 100,
"Score": 100
}
# Configure the Treeview columns.
for col in columns:
if col == "Username":
self.tree.column(col, anchor=W, width=column_widths[col])
else:
self.tree.column(col, anchor=CENTER, width=column_widths[col])
try:
# Add each item in the list into the Treeview.
for index, details in enumerate(users):
index += 1 # Increment the index by 1 each time t
self.tree.insert("", "end", values=(details[0], details[1], details[2], details[3], details[4], details[5]))
except IndexError as index_error: # Error control for instances such as the "users" list being empty.
messagebox.showerror("Invalid Data", f"The saved JSON data is invalid or incomplete.\nPlease check the file for missing fields.\n\n{index_error}\n\n{full_directory}")
return # Return from the method if an IndexError occurs, preventing further execution.
self.tree.bind("<<TreeviewSelect>>", self.on_item_selected) # Bind the treeview item selection event to the "on_item_selected" method.
self.tree.bind("<Motion>", "break") # Prevent the treeview columns from being manually resized by the user by breaking the motion event.
# Create a vertical scrollbar for the Treeview if the list is higher than 8 entries.
if int(len(users)) > 8:
self.scrollbar = CTk.CTkScrollbar(tree_frame, orientation="vertical", command=self.tree.yview, height=10, button_color=BUTTON_FG, button_hover_color=BUTTON_HOVER)
self.tree.configure(yscrollcommand=self.scrollbar.set)
self.scrollbar.pack(side=RIGHT, fill=Y) # Position the scrollbar inside the frame by using ".pack()".
self.scrollbar.bind("<Button-1>", lambda e: self.tools.on_mbtn1_click("Scoreboard", "Scrollbar")) # Bind the left mouse button click event to the "on_mbtn1_click" method in the "Tools" class, so that the color stays dim while clicked.
self.scrollbar.bind("<ButtonRelease-1>", lambda e: self.tools.on_mbtn1_release("Scoreboard", "Scrollbar")) # Bind the left mouse button release event to the "on_mbtn1_release" method in the "Tools" class, so that the color returns to normal when released.
# Position the Treeview inside the frame by using ".pack()".
self.tree.pack(side=LEFT, fill=BOTH, expand=True)
# Make sure the frame resizes properly by setting the weight to 1.
tree_frame.grid_columnconfigure(0, weight=1)
tree_frame.grid_rowconfigure(0, weight=1)
class Completion:
# Constructor for the "Completion" class, which takes an instance of the class names as a parameter and stores it in their unique attributes.
# This allows attributes and methods defined in the "Home" class, for example, to be accessed from within the "Completion" class.
def __init__(self, tools_instance, about_instance, scoreboard_instance, quiz_instance, homepage_instance):
self.tools = tools_instance # Store a reference to the "Tools" class instance.
self.about = about_instance # Store a reference to the "About" class instance.
self.scoreboard = scoreboard_instance # Store a reference to the "Scoreboard" class instance.
self.quiz = quiz_instance # Store a reference to the "Quiz" class instance.
self.home = homepage_instance # Store a reference to the "Home" class instance.
# Method for submitting the quiz details to the "users" list and saving them to the JSON file (saving is done in the "save_details" method within the "Tools" class).
def submit_details(self):
global ref_number, username, difficulty, difficulty_num, questions, users
self.quiz.final_score = f"{self.quiz.score}/{questions}"
if timer.get() == True:
self.time = self.quiz.total_time
else:
self.time = "Disabled"
users.append([ref_number, username, difficulty, questions, self.time, self.quiz.final_score]) # Add the next user and their quiz details to the "users" list.
self.tools.save_details(None, "Completion", None, SCOREBOARD_FILE_PATH) # Save the details to the JSON file.
self.setup_completion()
def setup_completion(self):
# Set width for columns 0-1 (2 total) in the main window. Positive weight means the column will expand to fill the available space.
main_window.columnconfigure(0, weight=1, minsize=0)
main_window.columnconfigure(1, weight=1, minsize=450)
# Set up the menu bar.
completion_menubar = Menu(main_window) # Create a new menu bar.
settings_menu = Menu(completion_menubar, tearoff=0, activebackground=MENU_HOVER, activeforeground=MENU_ACTIVE_FG)
completion_menubar.add_cascade(label="Settings", menu=settings_menu)
timer_settings = Menu(completion_menubar, tearoff=0, activebackground=MENU_HOVER, activeforeground=MENU_ACTIVE_FG)
settings_menu.add_cascade(menu=timer_settings, label="Timer")
timer_settings.add_radiobutton(label="Enabled", variable=timer, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH), value=True)
timer_settings.add_radiobutton(label="Disabled", variable=timer, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH), value=False)
history_settings = Menu(completion_menubar, tearoff=0, activebackground=MENU_HOVER, activeforeground=MENU_ACTIVE_FG)
settings_menu.add_cascade(menu=history_settings, label="Score Deletion History States")
history_settings.add_radiobutton(label="Disabled", variable=deletion_history_states, value=0, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH))
history_settings.add_radiobutton(label="10", variable=deletion_history_states, value=10, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH))
history_settings.add_radiobutton(label="25", variable=deletion_history_states, value=25, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH))
history_settings.add_radiobutton(label="50", variable=deletion_history_states, value=50, command=lambda: self.tools.save_details(None, "Menubar", None, SETTINGS_FILE_PATH))
help_menu = Menu(completion_menubar, tearoff=0, activebackground=MENU_HOVER, activeforeground=MENU_ACTIVE_FG)
completion_menubar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="Documentation")
help_menu.add_command(label="About", command=lambda: self.about.setup_about("Completion"))
main_window.config(menu=completion_menubar)
# Banner creation (left side)
lbanner_canvas = Canvas(main_window, bg=MAIN_WINDOW_BG, bd=0, highlightthickness=0) # Create a canvas for the banner image.
lbanner_canvas.grid(column=0, row=0, sticky=EW, padx=(20, 0))
lbanner = Image.open("AppData/Images/lbanner.png")
lbanner = ImageTk.PhotoImage(lbanner)
lbanner_canvas.configure(width=lbanner.width()+2, height=lbanner.height()) # Add 2 pixels to width to prevent image clipping on the right of image.
lbanner_canvas.create_image(lbanner.width() / 2, lbanner.height() / 2, anchor=CENTER, image=lbanner) # Add the image to the canvas by calculating the x and y coordinates for centre position.
lbanner_canvas.image = lbanner
# Banner creation (right side)
rbanner_canvas = Canvas(main_window, bg=MAIN_WINDOW_BG, bd=0, highlightthickness=0) # Create a canvas for the banner image.
rbanner_canvas.grid(column=2, row=0, sticky=EW, padx=(0, 20))
rbanner = Image.open("AppData/Images/rbanner.png")
rbanner = ImageTk.PhotoImage(rbanner)
rbanner_canvas.configure(width=rbanner.width()+2, height=rbanner.height()) # Add 2 pixels to width to prevent image clipping on the left of image.
rbanner_canvas.create_image(rbanner.width() / 2, rbanner.height() / 2, anchor=CENTER, image=rbanner) # Add the image to the canvas by calculating the x and y coordinates for centre position.
rbanner_canvas.image = rbanner
# Set up the main content frame to place the main completion frames and elements inside.
self.main_content_frame = CTk.CTkFrame(main_window, fg_color="transparent")
self.main_content_frame.grid(column=1, row=0, sticky=EW, padx=35, pady=(0,20))
# Logo creation
self.logo_canvas = Canvas(self.main_content_frame, bg=MAIN_WINDOW_BG, bd=0, highlightthickness=0) # Create a canvas for the banner image.
self.logo_canvas.grid(column=0, row=0, sticky=EW, padx=20, pady=(20,0))
self.logo = Image.open("AppData/Images/logo.png")
self.logo = ImageTk.PhotoImage(self.logo)
self.logo_canvas.configure(width=410, height=self.logo.height()+5) # Add 5 pixels to height to prevent image clipping on the bottom of image.
self.logo_canvas.create_image(410 / 2, self.logo.height() / 2, anchor=CENTER, image=self.logo) # Add the image to the canvas by calculating the x and y coordinates for centre position.
self.logo_canvas.image = self.logo
# Set up a content frame to place the main completion elements inside.
completion_frame1 = CTk.CTkFrame(self.main_content_frame, fg_color=FRAME_FG, corner_radius=10)
completion_frame1.grid(column=0, row=1, sticky=EW, padx=20, pady=(20,5))
# Set width for column 0 (1 total) in completion frame 1. Total minimum column width is 410px.
completion_frame1.columnconfigure(0, weight=1, minsize=410)
# Create the labels to be placed next to their relevant entry boxes.
CTk.CTkLabel(completion_frame1, text="Quiz Complete!", font=(DEFAULT_FONT, 18, "bold"), text_color=FONT_COLOUR).grid(column=0, row=0, sticky=EW, padx=5, pady=(20,8))
CTk.CTkLabel(completion_frame1, text=f"You scored a total of: {self.quiz.score}/{questions}", font=(DEFAULT_FONT, 15), text_color=FONT_COLOUR).grid(column=0, row=1, sticky=EW, padx=5)
CTk.CTkLabel(completion_frame1, text=f"Difficulty: {difficulty}", font=(DEFAULT_FONT, 15), text_color=FONT_COLOUR).grid(column=0, row=2, sticky=EW, padx=5)
self.total_time_lbl = CTk.CTkLabel(completion_frame1, text="", font=(DEFAULT_FONT, 15), text_color=FONT_COLOUR) # Make an empty label for the timer until the state of the timer is determined (enabled/disabled).
self.total_time_lbl.grid(column=0, row=3, sticky=EW, padx=5, pady=(0,20))
if timer.get() == True:
self.total_time_lbl.configure(text=self.tools.timer_config("Completion", "Enable", None)) # Use the "timer_config" function to update the label text relative to the state of the timer.
if timer.get() == False:
self.total_time_lbl.configure(text=self.tools.timer_config("Completion", "Disable", None)) # Use the "timer_config" function to update the label text relative to the state of the timer.
# Create a frame to place the buttons inside.
button_frame = CTk.CTkFrame(self.main_content_frame, fg_color="transparent")
button_frame.grid(column=0, row=2, sticky=EW, padx=20, pady=(5,0))
# Set width for columns 0-1 (2 total) in the answer frame. Total minimum column width is 410px.
button_frame.columnconfigure(0, weight=1, minsize=205)
button_frame.columnconfigure(1, weight=1, minsize=205)
# Create the buttons.
CTk.CTkButton(button_frame, text="View Answers",
width=200, height=30, corner_radius=10, fg_color=BUTTON_FG, hover_color=BUTTON_HOVER, font=(DEFAULT_FONT, 14, "bold"), text_color=FONT_COLOUR).grid(column=0, row=0, sticky=EW, padx=(0,5), pady=(0,5))
CTk.CTkButton(button_frame, text="Retry Quiz",
width=200, height=30, corner_radius=10, fg_color=BUTTON_FG, hover_color=BUTTON_HOVER, font=(DEFAULT_FONT, 14, "bold"), text_color=FONT_COLOUR).grid(column=1, row=0, sticky=EW, padx=(5,0), pady=(0,5))
CTk.CTkButton(button_frame, text="Scoreboard", command=lambda: self.quiz.reset_timer("Scoreboard", "Completion"),
width=200, height=30, corner_radius=10, fg_color=BUTTON_FG, hover_color=BUTTON_HOVER, font=(DEFAULT_FONT, 14, "bold"), text_color=FONT_COLOUR).grid(column=0, row=1, sticky=EW, padx=(0,5), pady=(5,0))
CTk.CTkButton(button_frame, text="Home", command=lambda: self.quiz.reset_timer("Home", "Completion"),
width=200, height=30, corner_radius=10, fg_color=BUTTON_FG, hover_color=BUTTON_HOVER, font=(DEFAULT_FONT, 14, "bold"), text_color=FONT_COLOUR).grid(column=1, row=1, sticky=EW, padx=(5,0), pady=(5,0))
class Quiz:
# Constructor for the "Quiz" class, which takes an instance of the class names as a parameter and stores it in their unique attributes.
# This allows attributes and methods defined in the "Home" class, for example, to be accessed from within the "Quiz" class.
def __init__(self, tools_instance, about_instance, scoreboard_instance, completion_instance, homepage_instance):
self.tools = tools_instance # Store a reference to the "Tools" class instance.
self.about = about_instance # Store a reference to the "About" class instance.
self.scoreboard = scoreboard_instance # Store a reference to the "Scoreboard" class instance.
self.completion = completion_instance # Store a reference to the "Completion" class instance.
self.home = homepage_instance # Store a reference to the "Home" class instance.
self.question_no = 1 # Variable for keeping track of which question the user is on, with the default value being 1.
self.timer_active = False # Variable to store the state of the timer, defaulting to False (off).
self.elapsed_time = 0 # Variable to store the elapsed time, defaulting to 0.
self.calculated_elapsed_time = 0 # Variable to store the calculated elapsed time, defaulting to 0.
self.quiz_start_time = None # Variable to store the start time of the quiz, defaulting to None.
self.pause_start_time = None # Variable to store the start time of the quiz pause, defaulting to None.
self.total_paused_time = 0 # Variable to store the total paused time, defaulting to 0.
self.time_string = "00:00:00" # Variable to store the formatted time string, defaulting to "00:00:00".
self.total_time = "00:00:00" # Variable to store the formatted total time, defaulting to "00:00:00".
self.user_answers = [] # Inalise a list to store the user's answers, defaulting to an empty list.
self.correct_answers = [] # Inalise a list to store the predefined correct answers, defaulting to an empty list.
self.final_score = "0/0" # Variable to store the final score, defaulting to "0/0".
self.score = 0 # Variable to store the active score during the quiz, defaulting to 0.
def exit_quiz(self, command, origin):
if origin == "Quiz" and command == "Home":
paused_prior = quiz_paused # Check if the quiz has been paused prior to pressing the exit button, meaning it shouldn't be paused twice and then unpaused.
if paused_prior == False: self.pause_quiz()
response1 = messagebox.askyesno("Exit Quiz", "Are you sure you want to exit the quiz?\nAll progress will be lost.", icon="warning")
if response1 == True:
self.stop_timer(command, origin)
else:
if paused_prior == False: self.unpause_quiz()
return
def start_timer(self):
self.timer_active = True
# Only set the quiz start time on the first run of the timer loop (not after unpausing)