6
6
from app .ai .questionGenerator import QuestionGenerator
7
7
8
8
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
+
9
17
def generate_questions ():
10
18
try :
11
19
number = int (num_questions .get ())
@@ -21,77 +29,87 @@ def generate_questions():
21
29
messagebox .showerror ("Error" , "Please enter a valid subject." )
22
30
return
23
31
24
- topic = topic_entry . get ().strip ()
32
+ topics = [ frame . entry . get ().strip () for frame in topic_frames if frame . entry . get (). strip ()]
25
33
age_group = age_group_entry .get ()
26
34
27
35
if not age_group :
28
36
messagebox .showerror ("Error" , "Age Group cannot be empty." )
29
37
return
30
38
31
- # Initialize the question generator
32
39
result_text .set ("Generating questions...\n It may take a minute to generate quiz questions." )
33
40
root .update_idletasks ()
34
41
42
+ all_questions = []
35
43
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 )
37
51
result_text .set ("Finished generating questions!" )
38
52
except Exception as e :
39
53
messagebox .showerror ("Error" , f"An error occurred: { e } " )
40
54
return
41
55
42
- # Save to JSON
43
56
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 ))
45
58
46
59
messagebox .showinfo ("Success" , "Questions generated and saved to output.json!" )
47
60
48
61
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
+
49
72
if __name__ == "__main__" :
50
- # Initialize the Question Generator
51
73
genAi = QuestionGenerator ()
74
+ topic_frames = []
52
75
53
- # Create main window
54
76
root = tk .Tk ()
55
77
root .title ("Quiz Question Generator" )
56
- root .geometry ("450x350 " )
78
+ root .geometry ("450x500 " )
57
79
root .configure (bg = "#f0f0f0" )
58
80
59
- # Styling
60
81
style = ttk .Style ()
61
82
style .configure ("TButton" , padding = 6 , relief = "flat" , background = "#4CAF50" , font = ("Arial" , 10 , "bold" ))
62
83
style .configure ("TLabel" , font = ("Arial" , 10 ))
63
84
style .configure ("TEntry" , padding = 5 )
64
85
style .configure ("TCombobox" , padding = 5 )
65
86
66
- # Number of Questions
67
87
ttk .Label (root , text = "Number of questions (1-5):" , background = "#f0f0f0" ).pack (pady = 5 )
68
88
num_questions = ttk .Entry (root )
69
89
num_questions .pack ()
70
90
71
- # Subject
72
91
ttk .Label (root , text = "Subject:" , background = "#f0f0f0" ).pack (pady = 5 )
73
92
subject_var = tk .StringVar ()
74
93
subject_menu = ttk .Combobox (root , textvariable = subject_var , values = [s .name for s in Subject ])
75
94
subject_menu .pack ()
76
95
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 )
81
103
82
- # Age Group
83
104
ttk .Label (root , text = "Age Group:" , background = "#f0f0f0" ).pack (pady = 5 )
84
105
age_group_entry = ttk .Entry (root )
85
106
age_group_entry .pack ()
86
107
87
- # Generate Button
88
108
generate_button = ttk .Button (root , text = "Generate Questions" , command = generate_questions )
89
109
generate_button .pack (pady = 15 )
90
110
91
- # Status Label
92
111
result_text = tk .StringVar ()
93
112
status_label = ttk .Label (root , textvariable = result_text , background = "#f0f0f0" , font = ("Arial" , 10 , "italic" ))
94
113
status_label .pack ()
95
114
96
- # Run the application
97
- root .mainloop ()
115
+ root .mainloop ()
0 commit comments