Skip to content

Commit 7f202f3

Browse files
committed
feat: implement dynamic topic management in question generation
1 parent 8e2f7c2 commit 7f202f3

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

main.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
from app.ai.questionGenerator import QuestionGenerator
77

88

9+
class TopicFrame(ttk.Frame):
10+
def __init__(self, parent, remove_callback):
11+
super().__init__(parent)
12+
self.entry = ttk.Entry(self)
13+
self.entry.pack(side=tk.LEFT, padx=(0, 5))
14+
self.remove_btn = ttk.Button(self, text="X", width=2, command=lambda: remove_callback(self))
15+
self.remove_btn.pack(side=tk.LEFT)
16+
917
def generate_questions():
1018
try:
1119
number = int(num_questions.get())
@@ -21,77 +29,87 @@ def generate_questions():
2129
messagebox.showerror("Error", "Please enter a valid subject.")
2230
return
2331

24-
topic = topic_entry.get().strip()
32+
topics = [frame.entry.get().strip() for frame in topic_frames if frame.entry.get().strip()]
2533
age_group = age_group_entry.get()
2634

2735
if not age_group:
2836
messagebox.showerror("Error", "Age Group cannot be empty.")
2937
return
3038

31-
# Initialize the question generator
3239
result_text.set("Generating questions...\nIt may take a minute to generate quiz questions.")
3340
root.update_idletasks()
3441

42+
all_questions = []
3543
try:
36-
questions = genAi.generateQuestions(number, subject, age_group, topic if topic else None)
44+
if topics:
45+
for topic in topics:
46+
questions = genAi.generateQuestions(number, subject, age_group, topic)
47+
all_questions.extend(questions)
48+
else:
49+
questions = genAi.generateQuestions(number, subject, age_group)
50+
all_questions.extend(questions)
3751
result_text.set("Finished generating questions!")
3852
except Exception as e:
3953
messagebox.showerror("Error", f"An error occurred: {e}")
4054
return
4155

42-
# Save to JSON
4356
with open("output.json", "w") as file:
44-
file.write(json.dumps([question.__dict__ for question in questions], indent=4))
57+
file.write(json.dumps([question.__dict__ for question in all_questions], indent=4))
4558

4659
messagebox.showinfo("Success", "Questions generated and saved to output.json!")
4760

4861

62+
def add_topic_field():
63+
frame = TopicFrame(topics_container, remove_topic_field)
64+
frame.pack(pady=2)
65+
topic_frames.append(frame)
66+
67+
68+
def remove_topic_field(frame):
69+
frame.destroy()
70+
topic_frames.remove(frame)
71+
4972
if __name__ == "__main__":
50-
# Initialize the Question Generator
5173
genAi = QuestionGenerator()
74+
topic_frames = []
5275

53-
# Create main window
5476
root = tk.Tk()
5577
root.title("Quiz Question Generator")
56-
root.geometry("450x350")
78+
root.geometry("450x500")
5779
root.configure(bg="#f0f0f0")
5880

59-
# Styling
6081
style = ttk.Style()
6182
style.configure("TButton", padding=6, relief="flat", background="#4CAF50", font=("Arial", 10, "bold"))
6283
style.configure("TLabel", font=("Arial", 10))
6384
style.configure("TEntry", padding=5)
6485
style.configure("TCombobox", padding=5)
6586

66-
# Number of Questions
6787
ttk.Label(root, text="Number of questions (1-5):", background="#f0f0f0").pack(pady=5)
6888
num_questions = ttk.Entry(root)
6989
num_questions.pack()
7090

71-
# Subject
7291
ttk.Label(root, text="Subject:", background="#f0f0f0").pack(pady=5)
7392
subject_var = tk.StringVar()
7493
subject_menu = ttk.Combobox(root, textvariable=subject_var, values=[s.name for s in Subject])
7594
subject_menu.pack()
7695

77-
# topic (Optional)
78-
ttk.Label(root, text="Topic (Optional):", background="#f0f0f0").pack(pady=5)
79-
topic_entry = ttk.Entry(root)
80-
topic_entry.pack()
96+
# Topics section
97+
ttk.Label(root, text="Topics (Optional):", background="#f0f0f0").pack(pady=5)
98+
topics_container = ttk.Frame(root)
99+
topics_container.pack(pady=5)
100+
101+
add_topic_btn = ttk.Button(root, text="Add Topic", command=add_topic_field)
102+
add_topic_btn.pack(pady=5)
81103

82-
# Age Group
83104
ttk.Label(root, text="Age Group:", background="#f0f0f0").pack(pady=5)
84105
age_group_entry = ttk.Entry(root)
85106
age_group_entry.pack()
86107

87-
# Generate Button
88108
generate_button = ttk.Button(root, text="Generate Questions", command=generate_questions)
89109
generate_button.pack(pady=15)
90110

91-
# Status Label
92111
result_text = tk.StringVar()
93112
status_label = ttk.Label(root, textvariable=result_text, background="#f0f0f0", font=("Arial", 10, "italic"))
94113
status_label.pack()
95114

96-
# Run the application
97-
root.mainloop()
115+
root.mainloop()

0 commit comments

Comments
 (0)