-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlashCardStudyApp.py
More file actions
469 lines (397 loc) · 20.8 KB
/
Copy pathFlashCardStudyApp.py
File metadata and controls
469 lines (397 loc) · 20.8 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
import customtkinter as ctk
import random
import json
import os
DATA_FILE = "flashcards_data.json"
class Flashcard:
def __init__(self, subject, category, question, answer):
self.subject = subject
self.category = category
self.question = question
self.answer = answer
def to_dict(self):
return {
"subject": self.subject,
"category": self.category,
"question": self.question,
"answer": self.answer,
}
@staticmethod
def from_dict(d):
return Flashcard(d["subject"], d["category"], d["question"], d["answer"])
class FlashcardDeck:
def __init__(self):
self.subjects = {
"Programming": ["Python", "C++", "Java"],
"Calculus": ["Limits", "Derivatives", "Integrals"],
"Information Security": ["Cryptography", "Networks", "Malware"],
"Database Management": ["SQL", "Normalization", "Transactions"],
}
self.cards = [
# Programming - Python
Flashcard("Programming", "Python", "Keyword to define a function?", "def"),
Flashcard("Programming", "Python", "Data type for whole numbers?", "int"),
Flashcard("Programming", "Python", "Method to add an element to a list?", "append"),
Flashcard("Programming", "Python", "Keyword to create a class?", "class"),
Flashcard("Programming", "Python", "Mutable sequence of elements?", "list"),
Flashcard("Programming", "Python", "Immutable ordered sequence?", "tuple"),
Flashcard("Programming", "Python", "Keyword to handle exceptions?", "try"),
Flashcard("Programming", "Python", "Used to iterate over items?", "for"),
Flashcard("Programming", "Python", "Function to get length?", "len"),
Flashcard("Programming", "Python", "Keyword for conditional?", "if"),
# Programming - C++
Flashcard("Programming", "C++", "Keyword to define a constant?", "const"),
Flashcard("Programming", "C++", "Access specifier for private members?", "private"),
Flashcard("Programming", "C++", "Keyword for inheritance?", "public"),
Flashcard("Programming", "C++", "Function to allocate memory dynamically?", "new"),
Flashcard("Programming", "C++", "Operator for reference?", "&"),
# Programming - Java
Flashcard("Programming", "Java", "Keyword to inherit a class?", "extends"),
Flashcard("Programming", "Java", "Primitive type for true/false?", "boolean"),
Flashcard("Programming", "Java", "Used to define interface?", "interface"),
Flashcard("Programming", "Java", "Keyword for constant?", "final"),
Flashcard("Programming", "Java", "Default access specifier?", "package-private"),
# Calculus - Limits
Flashcard("Calculus", "Limits", "Limit of sin(x)/x as x→0?", "1"),
Flashcard("Calculus", "Limits", "Limit of (1+x)^(1/x) as x→0?", "e"),
Flashcard("Calculus", "Limits", "Limit of x^2 as x→∞?", "∞"),
Flashcard("Calculus", "Limits", "Limit of 1/x as x→∞?", "0"),
# Calculus - Derivatives
Flashcard("Calculus", "Derivatives", "Derivative of x^2?", "2x"),
Flashcard("Calculus", "Derivatives", "Derivative of sin(x)?", "cos(x)"),
Flashcard("Calculus", "Derivatives", "Derivative of cos(x)?", "-sin(x)"),
Flashcard("Calculus", "Derivatives", "Derivative of e^x?", "e^x"),
# Calculus - Integrals
Flashcard("Calculus", "Integrals", "Integral of 1/x dx?", "ln|x| + C"),
Flashcard("Calculus", "Integrals", "Integral of e^x dx?", "e^x + C"),
Flashcard("Calculus", "Integrals", "Integral of sin(x) dx?", "-cos(x) + C"),
Flashcard("Calculus", "Integrals", "Integral of cos(x) dx?", "sin(x) + C"),
# Information Security - Cryptography
Flashcard("Information Security", "Cryptography", "AES stands for?", "Advanced Encryption Standard"),
Flashcard("Information Security", "Cryptography", "RSA is a type of?", "Asymmetric encryption"),
Flashcard("Information Security", "Cryptography", "SHA-256 is used for?", "Hashing"),
# Information Security - Networks
Flashcard("Information Security", "Networks", "Port number for HTTP?", "80"),
Flashcard("Information Security", "Networks", "Port number for HTTPS?", "443"),
Flashcard("Information Security", "Networks", "IP address version 4 has how many bits?", "32"),
# Information Security - Malware
Flashcard("Information Security", "Malware", "Software that replicates itself?", "Virus"),
Flashcard("Information Security", "Malware", "Malware that demands ransom?", "Ransomware"),
Flashcard("Information Security", "Malware", "Malware that records keystrokes?", "Keylogger"),
# Database Management - SQL
Flashcard("Database Management", "SQL", "SQL command to retrieve data?", "SELECT"),
Flashcard("Database Management", "SQL", "SQL command to remove data?", "DELETE"),
Flashcard("Database Management", "SQL", "SQL command to update data?", "UPDATE"),
# Database Management - Normalization
Flashcard("Database Management", "Normalization", "First normal form eliminates?", "Repeating groups"),
Flashcard("Database Management", "Normalization", "Second normal form removes?", "Partial dependency"),
Flashcard("Database Management", "Normalization", "Third normal form removes?", "Transitive dependency"),
# Database Management - Transactions
Flashcard("Database Management", "Transactions", "A transaction must be?", "Atomic"),
Flashcard("Database Management", "Transactions", "ACID property for consistency?", "Consistency"),
Flashcard("Database Management", "Transactions", "ACID property for durability?", "Durability"),
]
self.mastered = set()
self.load_from_file()
def get_random_card(self, subject=None, category=None):
filtered = self.cards
if subject and subject != "Mixed":
filtered = [c for c in filtered if c.subject == subject]
if category and category != "Mixed":
filtered = [c for c in filtered if c.category == category]
return random.choice(filtered) if filtered else None
def add_subject(self, subject):
if subject and subject not in self.subjects:
self.subjects[subject] = []
def add_category(self, subject, category):
if subject and category:
if subject not in self.subjects:
self.subjects[subject] = [category]
elif category not in self.subjects[subject]:
self.subjects[subject].append(category)
def add_card(self, subject, category, question, answer):
if subject and category and question and answer:
self.cards.append(Flashcard(subject, category, question, answer))
def save_to_file(self, path=DATA_FILE):
data = {
"subjects": self.subjects,
"cards": [c.to_dict() for c in self.cards],
"mastered": list(self.mastered)
}
try:
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
except Exception as e:
print("Error saving data:", e)
def load_from_file(self, path=DATA_FILE):
if not os.path.exists(path):
return
try:
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
if "subjects" in data:
self.subjects = data["subjects"]
if "cards" in data:
self.cards = [Flashcard.from_dict(d) for d in data["cards"]]
if "mastered" in data:
self.mastered = set(data["mastered"])
except Exception as e:
print("Error loading data:", e)
class FlashcardApp:
def __init__(self, root):
self.root = root
self.root.title("Flashback Study App")
self.root.geometry("900x630")
self.root.configure(bg="#729ba5")
ctk.set_appearance_mode("light")
ctk.set_default_color_theme("blue")
self.deck = FlashcardDeck()
self.current_card = None
self.flipping = False
self.subject_var = ctk.StringVar(value="Mixed")
self.category_var = ctk.StringVar(value="Mixed")
self.show_main_menu()
def clear_screen(self):
for widget in self.root.winfo_children():
widget.destroy()
def styled_button(self, text, command, height=50):
return ctk.CTkButton(
self.root, text=text,
fg_color="#4d7a86", hover_color="#3d6670",
text_color="white", corner_radius=10,
height=height, font=("Cooper Black", 18),
command=command
)
def styled_combobox(self, variable, values, width=400, height=45):
return ctk.CTkComboBox(
self.root, values=values, variable=variable, font=("Apotos Black",18),
state="readonly", width=width, height=height, corner_radius=10,
fg_color="#2E4053", button_color="#1ABC9C", button_hover_color="#16A085",
border_color="#1ABC9C", dropdown_fg_color="#2C3E50", dropdown_hover_color="#34495E",
dropdown_text_color="white", text_color="white", dropdown_font=("Helvetica", 15)
)
def styled_label(self, text, size=20, bold=False, color="#3d6670"):
font_style = ("Cooper Black", size, "bold") if bold else ("Cooper Black", size)
return ctk.CTkLabel(
self.root, text=text, font=font_style, text_color=color
)
def styled_entry(self, placeholder=""):
return ctk.CTkEntry(
self.root, placeholder_text=placeholder,
fg_color="#2E4053", text_color="white",
placeholder_text_color="#bdc3c7", border_color="#1ABC9C",
corner_radius=10, height=40, font=("Arial", 16),width=400
)
def show_main_menu(self):
self.clear_screen()
title = self.styled_label("Flashcard Study App", size=40, bold=True, color="#3d6670")
title.pack(pady=(55,35))
menu_options = [
("Practice Flashcards", self.practice_menu),
("Add new Flashcard", self.add_flashcard_screen),
("Review Mastered Cards", self.review_mastered),
("Add Subject/Category", self.add_subject_category_screen)
]
for text, cmd in menu_options:
self.styled_button(text, cmd, height=70).pack(pady=12, ipadx=10, ipady=5)
def practice_menu(self):
self.clear_screen()
tk_label = self.styled_label("Select Subject", size=35, bold=True, color="#3d6670")
tk_label.pack(pady=(100,10))
subjects = list(self.deck.subjects.keys()) + ["Mixed"]
self.subject_var.set("Mixed")
subj_combo = self.styled_combobox(self.subject_var, subjects)
subj_combo.pack(pady=(2,3))
tk_label2 = self.styled_label("Select Category", size=28, bold=True, color="#3d6670")
tk_label2.pack(pady=8)
self.category_var.set("Mixed")
cat_combo = self.styled_combobox(self.category_var, ["Mixed"])
cat_combo.pack(pady=5)
def update_categories(*args):
subj = self.subject_var.get()
if subj != "Mixed":
cat_combo.configure(values=self.deck.subjects.get(subj, []) + ["Mixed"])
else:
cat_combo.configure(values=["Mixed"])
self.category_var.set("Mixed")
update_progress()
self.subject_var.trace_add("write", update_categories)
self.category_var.trace_add("write", lambda *a: update_progress())
progress_frame = ctk.CTkFrame(self.root, fg_color="transparent")
progress_frame.pack(pady=(10,15))
self.progress_label = self.styled_label("", size=16)
self.progress_label.pack(in_=progress_frame, side="left", padx=10)
self.progress_bar = ctk.CTkProgressBar(progress_frame, width=250)
self.progress_bar.pack(in_=progress_frame, side="left", padx=10)
def update_progress():
subj = self.subject_var.get()
cat = self.category_var.get()
filtered = self.deck.cards
if subj and subj != "Mixed":
filtered = [c for c in filtered if c.subject == subj]
if cat and cat != "Mixed":
filtered = [c for c in filtered if c.category == cat]
total = len(filtered)
mastered = 0
for c in filtered:
if c.question in self.deck.mastered:
mastered += 1
pct = (mastered / total) if total else 0
self.progress_label.configure(text=f"Mastered: {mastered} / {total}",font=("Cooper Black",18))
self.progress_bar.set(pct)
update_progress()
self.styled_button("Start Practice", self.show_flashcard).pack(pady=(20,8))
self.styled_button("Back to Menu", self.show_main_menu).pack(pady=8)
def show_flashcard(self):
self.clear_screen()
subject = self.subject_var.get()
category = self.category_var.get()
self.current_card = self.deck.get_random_card(subject, category)
if not self.current_card:
msg = self.styled_label("No cards available for this selection.\nAdd new cards.\nThen practice", size=35)
msg.pack(pady=(150,60))
self.styled_button("Back", self.practice_menu).pack(pady=20)
return
fc_title = self.styled_label("Flashcard", size=30, bold=True, color="#3d6670")
fc_title.pack(pady=(30,20))
self.card_frame = ctk.CTkFrame(self.root, width=600, height=200, corner_radius=15, fg_color="#4d7a86")
self.card_frame.pack(pady=(10,20))
self.card_label = ctk.CTkLabel(self.card_frame, text=self.current_card.question,
font=("Aptos Black", 20,"bold"), text_color="white", wraplength=550)
self.card_label.place(relx=0.5, rely=0.5, anchor="center")
btn_frame = ctk.CTkFrame(self.root, fg_color="transparent")
btn_frame.pack()
flip_btn = ctk.CTkButton(btn_frame, text="Flip Card", fg_color="#1f424c", hover_color="#3d6670",
text_color="white", corner_radius=10, height= 50,
font=("Cooper Black", 18),command=self.flip_card)
flip_btn.grid(row=0, column=0, padx=8, pady=8)
mark_btn = ctk.CTkButton(btn_frame, text="Mark Mastered",fg_color="#1f424c", hover_color="#3d6670",
text_color="white", corner_radius=10,
height=50, font=("Cooper Black", 18),command=self.mark_mastered)
mark_btn.grid(row=0, column=1, padx=8, pady=8)
next_btn = self.styled_button("Next Card", self.show_flashcard)
next_btn.pack(pady=5)
self.styled_button("Back", self.practice_menu).pack(pady=10)
def mark_mastered(self):
if self.current_card:
self.deck.mastered.add(self.current_card.question)
self.deck.save_to_file()
self.styled_label("Marked as mastered!", size=24).pack(pady=5)
def flip_card(self):
if not self.flipping:
self.flipping = True
current_text = self.card_label.cget("text")
new_text = self.current_card.answer if current_text == self.current_card.question else self.current_card.question
self.animate_flip(current_text, new_text, step=0)
def animate_flip(self, old_text, new_text, step):
if step < 10:
self.card_frame.configure(width=600 - step*40)
self.root.after(30, lambda: self.animate_flip(old_text, new_text, step + 1))
elif step == 10:
if new_text == self.current_card.answer:
self.card_frame.configure(fg_color="white")
self.card_label.configure(text_color="#4d7a86")
else:
self.card_frame.configure(fg_color="#4d7a86")
self.card_label.configure(text_color="white")
self.card_label.configure(text=new_text)
self.root.after(30, lambda: self.animate_flip(old_text, new_text, step + 1))
elif step <= 20:
self.card_frame.configure(width=200 + (step-10)*40)
self.root.after(30, lambda: self.animate_flip(old_text, new_text, step + 1))
else:
self.card_frame.configure(width=600)
self.flipping = False
def add_flashcard_screen(self):
self.clear_screen()
title = self.styled_label("Add New Flashcard", size=30, bold=True)
title.pack(pady=(45,12))
self.styled_label("Subject:",size = 30).pack(pady=5)
subj_combo_var = ctk.StringVar()
subj_combo = self.styled_combobox(subj_combo_var, list(self.deck.subjects.keys()))
subj_combo.pack(pady=5)
self.styled_label("Category:",size = 30).pack(pady=5)
cat_combo_var = ctk.StringVar()
cat_combo = self.styled_combobox(cat_combo_var, ["Select Subject"])
cat_combo.pack(pady=5)
def update_categories(*args):
subject = subj_combo_var.get()
if subject:
cat_combo.configure(values=self.deck.subjects.get(subject, []))
subj_combo_var.trace_add("write", update_categories)
q_entry = self.styled_entry("Enter Question")
q_entry.pack(pady=5)
a_entry = self.styled_entry("Enter Answer")
a_entry.pack(pady=5)
def save_card():
s = subj_combo_var.get()
c = cat_combo_var.get()
q = q_entry.get().strip()
a = a_entry.get().strip()
if not s:
self.styled_label("Please select a Subject.", size=20).pack(pady=5)
return
if not c or c == "Select Subject":
self.styled_label("Please select a Category.", size=20).pack(pady=5)
return
if not q or not a:
self.styled_label("Question and Answer cannot be empty.", size=20).pack(pady=5)
return
self.deck.add_card(s, c, q, a)
self.deck.save_to_file()
q_entry.delete(0, 'end')
a_entry.delete(0, 'end')
self.styled_label("Flashcard Added!", size=20).pack(pady=10)
self.styled_button("Save Flashcard", save_card).pack(pady=10)
self.styled_button("Back to Menu", self.show_main_menu).pack(pady=5)
def review_mastered(self):
self.clear_screen()
title = self.styled_label("Mastered Cards", size=40, bold=True)
title.pack(pady=(70,25))
total_cards = len(self.deck.cards)
mastered_count = len(self.deck.mastered)
self.styled_label(f"Progress: {mastered_count} / {total_cards} mastered", size=20).pack(pady=5)
if not self.deck.mastered:
self.styled_label("No mastered cards yet.\nCome on!\nMaster some cards", size=20).pack(pady=20)
else:
for q in self.deck.mastered:
self.styled_label(f"- {q}", size=14).pack(pady=2)
self.styled_button("Back", self.show_main_menu).pack(pady=40)
def add_subject_category_screen(self):
self.clear_screen()
title = self.styled_label("Add Subject / Category", size=30, bold=True)
title.pack(pady=(40,20))
self.styled_label("New Subject:",size = 25).pack()
subj_entry = self.styled_entry()
subj_entry.pack(pady=10)
self.styled_label("Or New Category Under Subject:",size= 25).pack()
subj_combo_var = ctk.StringVar()
subj_combo = self.styled_combobox(subj_combo_var, list(self.deck.subjects.keys()))
subj_combo.pack(pady=10)
cat_entry = self.styled_entry("New Category Name")
cat_entry.pack(pady=(10,20))
def save():
new_sub = subj_entry.get().strip()
chosen_sub = subj_combo_var.get().strip()
new_cat = cat_entry.get().strip()
if new_sub:
self.deck.add_subject(new_sub)
if new_cat:
self.deck.add_category(new_sub, new_cat)
elif new_cat and chosen_sub:
self.deck.add_category(chosen_sub, new_cat)
else:
self.styled_label("Please enter a new subject\nor choose a subject and add a category.", size=20).pack(pady=15)
return
self.deck.save_to_file()
self.show_main_menu()
self.styled_button("Save", save).pack(pady=15)
self.styled_button("Back", self.show_main_menu).pack()
if __name__ == "__main__":
root = ctk.CTk()
app = FlashcardApp(root)
def on_closing():
app.deck.save_to_file()
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()