-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom Combination Quiz App.py
More file actions
326 lines (269 loc) · 13.3 KB
/
Copy pathRandom Combination Quiz App.py
File metadata and controls
326 lines (269 loc) · 13.3 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
"""
Random Combination Quiz App
Single-file tkinter desktop application.
Run: save this file as random_combination_quiz.py and run `python random_combination_quiz.py`.
"""
import tkinter as tk
from tkinter import ttk, messagebox
import random
class RandomCombinationQuizApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("Random Combination Quiz")
# a reasonable default size; window is resizable
self.geometry("900x600")
self.minsize(700, 480)
self.resizable(True, True)
# State
self.items = []
self.k = 2
self.num_questions = 5
self.current_question = 0
self.score = 0
self.questions = [] # list of sets
self.reveal_time_ms = 2500 # milliseconds to reveal the combination
self._build_ui()
def _build_ui(self):
# Use grid on root so resizing works nicely
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1) # make question row expand
# Top frame - configuration
cfg_frame = ttk.LabelFrame(self, text="Configuration", padding=8)
cfg_frame.grid(row=0, column=0, sticky="nsew", padx=8, pady=6)
# let the cfg_frame expand horizontally but not take too much vertical space
cfg_frame.columnconfigure(0, weight=1)
cfg_frame.columnconfigure(1, weight=0)
cfg_frame.columnconfigure(2, weight=0)
cfg_frame.columnconfigure(3, weight=0)
cfg_frame.rowconfigure(1, weight=1)
ttk.Label(cfg_frame, text="Items (comma separated):").grid(row=0, column=0, columnspan=4, sticky="w")
self.items_text = tk.Text(cfg_frame, height=4, wrap="word")
self.items_text.grid(row=1, column=0, columnspan=4, sticky="nsew", pady=6)
ttk.Label(cfg_frame, text="Combination size (k):").grid(row=2, column=0, sticky="w", pady=(4,0))
self.k_spin = ttk.Spinbox(cfg_frame, from_=1, to=100, width=6, command=self._on_k_change)
self.k_spin.set(self.k)
self.k_spin.grid(row=2, column=1, sticky="w", pady=(4,0), padx=(6,0))
ttk.Label(cfg_frame, text="Number of questions:").grid(row=2, column=2, sticky="w", pady=(4,0))
self.num_q_spin = ttk.Spinbox(cfg_frame, from_=1, to=2000, width=6)
self.num_q_spin.set(self.num_questions)
self.num_q_spin.grid(row=2, column=3, sticky="w", pady=(4,0), padx=(6,0))
ttk.Label(cfg_frame, text="Reveal time (seconds):").grid(row=3, column=0, sticky="w", pady=(6,0))
self.reveal_spin = ttk.Spinbox(cfg_frame, from_=1, to=60, width=6)
self.reveal_spin.set(int(self.reveal_time_ms/1000))
self.reveal_spin.grid(row=3, column=1, sticky="w", pady=(6,0), padx=(6,0))
start_btn = ttk.Button(cfg_frame, text="Start Quiz", command=self.start_quiz)
start_btn.grid(row=3, column=3, sticky="e", padx=(0,6), pady=(6,0))
# Middle frame - question display (this expands)
q_frame = ttk.LabelFrame(self, text="Question", padding=8)
q_frame.grid(row=1, column=0, sticky="nsew", padx=8, pady=6)
q_frame.columnconfigure(0, weight=1)
q_frame.rowconfigure(0, weight=1)
self.question_label = ttk.Label(q_frame, text="Press 'Start Quiz' to begin.",
font=(None, 14), wraplength=760, justify="center")
self.question_label.grid(row=0, column=0, sticky="nsew", padx=6, pady=6)
# update wraplength when question frame resizes
def update_wrap(event):
# subtract some padding so text doesn't touch edges
new_wrap = max(200, event.width - 40)
self.question_label.configure(wraplength=new_wrap)
q_frame.bind("<Configure>", update_wrap)
# Bottom frame - answer area and controls
ans_frame = ttk.LabelFrame(self, text="Answer / Controls", padding=8)
ans_frame.grid(row=2, column=0, sticky="nsew", padx=8, pady=6)
ans_frame.columnconfigure(0, weight=1)
ans_frame.columnconfigure(1, weight=0)
# Scrollable checkbox area on the left
checkbox_outer = ttk.Frame(ans_frame)
checkbox_outer.grid(row=0, column=0, sticky="nsew")
checkbox_outer.rowconfigure(0, weight=1)
checkbox_outer.columnconfigure(0, weight=1)
# Canvas + scrollbar
self.checkbox_canvas = tk.Canvas(checkbox_outer, highlightthickness=0)
self.checkbox_canvas.grid(row=0, column=0, sticky="nsew")
vsb = ttk.Scrollbar(checkbox_outer, orient="vertical", command=self.checkbox_canvas.yview)
vsb.grid(row=0, column=1, sticky="ns")
self.checkbox_canvas.configure(yscrollcommand=vsb.set)
# Inner frame that will hold checkbuttons
self.checkbox_container = ttk.Frame(self.checkbox_canvas)
self.checkbox_window = self.checkbox_canvas.create_window((0,0), window=self.checkbox_container, anchor="nw")
def _on_checkbox_config(event):
# update canvas scrollregion
self.checkbox_canvas.configure(scrollregion=self.checkbox_canvas.bbox("all"))
self.checkbox_container.bind("<Configure>", _on_checkbox_config)
def _on_canvas_config(event):
# keep the inner frame width in sync with canvas width
canvas_width = event.width
try:
self.checkbox_canvas.itemconfigure(self.checkbox_window, width=canvas_width)
except Exception:
pass
self.checkbox_canvas.bind("<Configure>", _on_canvas_config)
# Controls on the right
control_frame = ttk.Frame(ans_frame)
control_frame.grid(row=0, column=1, sticky="ne", padx=(8,0))
self.submit_btn = ttk.Button(control_frame, text="Submit Answer", command=self.submit_answer, state="disabled")
self.submit_btn.pack(padx=4, pady=4, fill="x")
self.next_btn = ttk.Button(control_frame, text="Next", command=self.next_question, state="disabled")
self.next_btn.pack(padx=4, pady=4, fill="x")
self.score_label = ttk.Label(control_frame, text=f"Score: {self.score}/{self.num_questions}")
self.score_label.pack(padx=4, pady=8)
reset_btn = ttk.Button(control_frame, text="Reset", command=self.reset_quiz)
reset_btn.pack(padx=4, pady=4, fill="x")
# some padding for a nicer look
for child in cfg_frame.winfo_children():
try:
child.grid_configure(padx=6, pady=3)
except Exception:
pass
def _on_k_change(self):
try:
self.k = int(self.k_spin.get())
except Exception:
self.k = 2
def parse_items(self):
raw = self.items_text.get("1.0", "end").strip()
if not raw:
return []
# split by comma or newline, strip whitespace, remove empty
parts = [p.strip() for p in raw.replace('\n', ',').split(',')]
parts = [p for p in parts if p]
# remove duplicates while preserving order
seen = set()
items = []
for p in parts:
if p not in seen:
seen.add(p)
items.append(p)
return items
def start_quiz(self):
self.items = self.parse_items()
if len(self.items) < 1:
messagebox.showwarning("No items", "Please enter at least one item to quiz on.")
return
try:
self.k = int(self.k_spin.get())
self.num_questions = int(self.num_q_spin.get())
self.reveal_time_ms = int(self.reveal_spin.get()) * 1000
except Exception:
messagebox.showerror("Invalid input", "Please make sure numeric fields are valid integers.")
return
if self.k < 1 or self.k > len(self.items):
messagebox.showwarning("Invalid combination size", f"Combination size k must be between 1 and {len(self.items)}.")
return
# Generate questions as random combinations (allow repeated questions if combinations fewer than num_questions)
from itertools import combinations
all_combs = list(combinations(self.items, self.k))
if not all_combs:
messagebox.showerror("Error", "Unable to create any combinations with the given items and k.")
return
# Shuffle and pick
random.shuffle(all_combs)
if len(all_combs) >= self.num_questions:
chosen = all_combs[:self.num_questions]
else:
# allow repeats if not enough unique combinations
chosen = [random.choice(all_combs) for _ in range(self.num_questions)]
# Convert to set for easier checking
self.questions = [set(c) for c in chosen]
# Reset counters
self.current_question = 0
self.score = 0
self.update_score_label()
# Disable config controls while quiz is running
self.submit_btn.config(state="disabled")
self.next_btn.config(state="disabled")
self._show_current_question()
def _show_current_question(self):
qset = self.questions[self.current_question]
# Display the combination as a bullet list
display_text = "\n".join(f"• {item}" for item in qset)
header = f"Question {self.current_question+1} of {self.num_questions}\n\n"
self.question_label.config(text=header + display_text)
# After reveal_time_ms hide the text and show the checkboxes
self.after(self.reveal_time_ms, self._hide_and_show_checkboxes)
# Ensure checkboxes area is cleared
for child in self.checkbox_container.winfo_children():
child.destroy()
# disable submit/next until reveal period is over
self.submit_btn.config(state="disabled")
self.next_btn.config(state="disabled")
# Reset scroll to top
try:
self.checkbox_canvas.yview_moveto(0)
except Exception:
pass
def _hide_and_show_checkboxes(self):
# Hide question text
self.question_label.config(text="Select the items that were shown in the combination.")
# Create checkboxes for all items inside the inner frame
self.checkbox_vars = {}
for child in self.checkbox_container.winfo_children():
child.destroy()
# We'll arrange checkboxes in a single column for clarity (scrollable)
for i, item in enumerate(self.items):
var = tk.BooleanVar(value=False)
cb = ttk.Checkbutton(self.checkbox_container, text=item, variable=var)
cb.grid(row=i, column=0, sticky="w", padx=6, pady=2)
self.checkbox_vars[item] = var
# Ensure scrollbar region updated
self.checkbox_canvas.configure(scrollregion=self.checkbox_canvas.bbox("all"))
# Enable submit button
self.submit_btn.config(state="normal")
# Next remains disabled until after submission
self.next_btn.config(state="disabled")
def submit_answer(self):
# Collect selected items
selected = {item for item, var in self.checkbox_vars.items() if var.get()}
correct = self.questions[self.current_question]
# Compute score for this question: full points only if exact match
if selected == correct:
self.score += 1
message = "Correct!"
else:
# give feedback: which were missing and which were extra
missing = correct - selected
extra = selected - correct
parts = []
if missing:
parts.append("Missing: " + ", ".join(sorted(missing)))
if extra:
parts.append("Incorrect selections: " + ", ".join(sorted(extra)))
message = "Not quite. " + "; ".join(parts)
messagebox.showinfo("Result", message)
self.update_score_label()
# Disable submit, enable next (or finish)
self.submit_btn.config(state="disabled")
if self.current_question + 1 < self.num_questions:
self.next_btn.config(state="normal")
else:
# Quiz finished
self._finish_quiz()
def next_question(self):
self.current_question += 1
# Clear checkboxes
for child in self.checkbox_container.winfo_children():
child.destroy()
self._show_current_question()
def update_score_label(self):
self.score_label.config(text=f"Score: {self.score}/{self.num_questions}")
def _finish_quiz(self):
# Clear checkboxes
for child in self.checkbox_container.winfo_children():
child.destroy()
self.question_label.config(text=f"Quiz finished. Your score: {self.score}/{self.num_questions}")
self.next_btn.config(state="disabled")
self.submit_btn.config(state="disabled")
def reset_quiz(self):
self.items_text.delete("1.0", "end")
self.k_spin.set(2)
self.num_q_spin.set(5)
self.reveal_spin.set(3)
self.question_label.config(text="Press 'Start Quiz' to begin.")
for child in self.checkbox_container.winfo_children():
child.destroy()
self.score = 0
self.num_questions = 5
self.update_score_label()
if __name__ == "__main__":
app = RandomCombinationQuizApp()
app.mainloop()