-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz_app.py
More file actions
444 lines (364 loc) · 16.6 KB
/
quiz_app.py
File metadata and controls
444 lines (364 loc) · 16.6 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
import sqlite3
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
# Function to get questions from database
def get_questions_from_database(course_name):
database_name = f"{course_name.replace(' ', '_')}.db"
conn = sqlite3.connect(database_name)
cursor = conn.cursor()
#gets answer choices
cursor.execute("SELECT question_text, option_A, option_B, option_C, option_D, correct_answer FROM questions")
questions = cursor.fetchall()
conn.close()
#lists question + answers
question_list = []
for q in questions:
question_list.append({
'question_text': q[0],
'option_A': q[1],
'option_B': q[2],
'option_C': q[3],
'option_D': q[4],
'correct_answer': q[5]
})
return question_list
# Admin Login
def check_password():
if password_entry.get() == "admin123":
login_window.destroy()
admin_interface()
else:
messagebox.showerror("Error", "Incorrect password")
# Admin Features
#lets admin add question
def add_question_gui():
#submits question into chosen database
def submit_question():
course = course_combobox.get()
if not course:
messagebox.showwarning("No Course Selected", "Please select a course.")
return
data = (question_entry.get(), option_a.get(), option_b.get(), option_c.get(), option_d.get(), correct_answer.get())
if not all(data):
messagebox.showwarning("Missing Info", "Please fill all fields.") #lets user know of error
return
db_name = f"{course.replace(' ', '_')}.db"
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS questions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
question_text TEXT,
option_A TEXT,
option_B TEXT,
option_C TEXT,
option_D TEXT,
correct_answer TEXT)''')
cursor.execute("""INSERT INTO questions
(question_text, option_A, option_B, option_C, option_D, correct_answer)
VALUES (?, ?, ?, ?, ?, ?)""", data)
conn.commit()
conn.close()
messagebox.showinfo("Success", "Question added!")
window.destroy()
window = Toplevel(root)
window.title("Add New Question")
#gives users choices of courses
courses = ["Principles of Managerial Finance", "Mgmt Organizational Behavior",
"Business Applications Develop", "Business Database Mgmt", "Principles of Marketing"]
Label(window, text="Select Course:").pack()
course_combobox = ttk.Combobox(window, values=courses, width=50, state="readonly")
course_combobox.pack()
#formats question
Label(window, text="Question:").pack()
question_entry = Entry(window, width=100)
question_entry.pack()
#Formats question
option_a = Entry(window, width=50)
option_b = Entry(window, width=50)
option_c = Entry(window, width=50)
option_d = Entry(window, width=50)
correct_answer = Entry(window, width=5)
for lbl, widget in zip(["Option A", "Option B", "Option C", "Option D", "Correct Answer (A/B/C/D)"], [option_a, option_b, option_c, option_d, correct_answer]):
Label(window, text=lbl).pack()
widget.pack()
Button(window, text="Submit Question", command=submit_question).pack(pady=10)
#defines view question to allow admin to veiw a courses questions.
def view_questions_gui():
def load_questions():
course = course_combobox.get()
# makes user choose course
if not course:
messagebox.showwarning("No Course Selected", "Please select a course.")
return
try:
questions = get_questions_from_database(course)
questions_listbox.delete(0, END)
for i, q in enumerate(questions, 1):
display_text = (
f"{i}. {q['question_text']}\n"
f" A. {q['option_A']} B. {q['option_B']} "
f"C. {q['option_C']} D. {q['option_D']} "
f"(Answer: {q['correct_answer']})\n"
)
questions_listbox.insert(END, display_text)
except Exception as e:
messagebox.showerror("Error", str(e))
window = Toplevel(root)
window.title("View Questions")
window.geometry("800x500")
Label(window, text="Select Course:", font=("Arial", 12)).pack(pady=10)
#gives users choices of courses
courses = ["Principles of Managerial Finance", "Mgmt Organizational Behavior",
"Business Applications Develop", "Business Database Mgmt", "Principles of Marketing"]
course_combobox = ttk.Combobox(window, values=courses, width=50, state="readonly")
course_combobox.pack()
Button(window, text="Load Questions", command=load_questions).pack(pady=10)
questions_listbox = Listbox(window, width=110, height=20, font=("Courier", 10))
questions_listbox.pack(pady=10)
#allows admin to change questions
def modify_question_gui():
def load_questions():
course = course_combobox.get()
if not course:
messagebox.showwarning("No Course Selected", "Please select a course.")
return
try:
nonlocal db_name
db_name = f"{course.replace(' ', '_')}.db"
nonlocal questions
questions = get_questions_from_database(course)
questions_listbox.delete(0, END)
for i, q in enumerate(questions, 1):
questions_listbox.insert(END, f"{i}. {q['question_text']}")
except Exception as e:
messagebox.showerror("Error", str(e))
def modify_selected():
idx = questions_listbox.curselection()
if not idx:
messagebox.showwarning("No Selection", "Please select a question to modify.")
return
selected_question = questions[idx[0]]
modify_window = Toplevel(root)
modify_window.title("Modify Question")
Label(modify_window, text="Question:").pack()
question_entry = Entry(modify_window, width=100)
question_entry.insert(0, selected_question['question_text'])
question_entry.pack()
#changes options in a question
option_a = Entry(modify_window, width=50)
option_b = Entry(modify_window, width=50)
option_c = Entry(modify_window, width=50)
option_d = Entry(modify_window, width=50)
correct_answer = Entry(modify_window, width=5)
for lbl, widget in zip(["Option A", "Option B", "Option C", "Option D", "Correct Answer (A/B/C/D)"],
[option_a, option_b, option_c, option_d, correct_answer]):
Label(modify_window, text=lbl).pack()
widget.pack()
#creates a screen to veiw the courses questions
def view_questions_gui():
def load_questions():
course = course_combobox.get()
if not course:
messagebox.showwarning("No Course Selected", "Please select a course.")
return
try:
questions = get_questions_from_database(course)
questions_listbox.delete(0, END)
for i, q in enumerate(questions, 1):
display_text = (
f"{i}. {q['question_text']}\n"
f" A. {q['option_A']} B. {q['option_B']} "
f"C. {q['option_C']} D. {q['option_D']} "
f"(Answer: {q['correct_answer']})\n"
)
questions_listbox.insert(END, display_text)
except Exception as e:
messagebox.showerror("Error", str(e))
window = Toplevel(root)
window.title("View Questions")
window.geometry("800x500")
Label(window, text="Select Course:", font=("Arial", 12)).pack(pady=10)
#gives users choices of courses
courses = ["Principles of Managerial Finance", "Mgmt Organizational Behavior",
"Business Applications Develop", "Business Database Mgmt", "Principles of Marketing"]
course_combobox = ttk.Combobox(window, values=courses, width=50, state="readonly")
course_combobox.pack()
Button(window, text="Load Questions", command=load_questions).pack(pady=10)
questions_listbox = Listbox(window, width=110, height=20, font=("Courier", 10))
questions_listbox.pack(pady=10)
#allows users to submit what the modified
def submit_modifications():
data = (question_entry.get(), option_a.get(), option_b.get(), option_c.get(), option_d.get(), correct_answer.get())
if not all(data):
messagebox.showwarning("Missing Info", "Please fill all fields.")
return
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute("""UPDATE questions SET question_text = ?, option_A = ?, option_B = ?, option_C = ?,
option_D = ?, correct_answer = ? WHERE question_text = ?""",
(*data, selected_question['question_text']))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Question modified!")
modify_window.destroy()
load_questions()
Button(modify_window, text="Submit Modifications", command=submit_modifications).pack(pady=10)
window = Toplevel(root)
window.title("Modify Questions")
window.geometry("700x500")
db_name = ""
questions = []
Label(window, text="Select Course:").pack()
courses = ["Principles of Managerial Finance", "Mgmt Organizational Behavior",
"Business Applications Develop", "Business Database Mgmt", "Principles of Marketing"]
course_combobox = ttk.Combobox(window, values=courses, width=50, state="readonly")
course_combobox.pack()
Button(window, text="Load Questions", command=load_questions).pack(pady=5)
questions_listbox = Listbox(window, width=100, height=20)
questions_listbox.pack(pady=10)
Button(window, text="Modify Selected Question", command=modify_selected).pack(pady=5)
#allows admin to delet questions
def delete_question_gui():
def load_questions():
course = course_combobox.get()
if not course:
messagebox.showwarning("No Course Selected", "Please select a course.")
return
try:
nonlocal db_name
db_name = f"{course.replace(' ', '_')}.db"
nonlocal questions
questions = get_questions_from_database(course)
questions_listbox.delete(0, END)
for i, q in enumerate(questions, 1):
questions_listbox.insert(END, f"{i}. {q['question_text']}")
except Exception as e:
messagebox.showerror("Error", str(e))
#deletes question
def delete_selected():
idx = questions_listbox.curselection()
if not idx:
return
question_text = questions[idx[0]]['question_text']
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute("DELETE FROM questions WHERE question_text = ?", (question_text,))
conn.commit()
conn.close()
load_questions()
messagebox.showinfo("Deleted", "Question deleted successfully.")
window = Toplevel(root)
window.title("Delete Questions")
window.geometry("700x500")
db_name = ""
questions = []
Label(window, text="Select Course:").pack()
courses = ["Principles of Managerial Finance", "Mgmt Organizational Behavior",
"Business Applications Develop", "Business Database Mgmt", "Principles of Marketing"]
course_combobox = ttk.Combobox(window, values=courses, width=50, state="readonly")
course_combobox.pack()
Button(window, text="Load Questions", command=load_questions).pack(pady=5)
questions_listbox = Listbox(window, width=100, height=20)
questions_listbox.pack(pady=10)
Button(window, text="Delete Selected Question", command=delete_selected).pack(pady=5)
# Admin Interface
def admin_interface():
window = Toplevel(root)
window.title("Admin Dashboard")
window.geometry("300x300")
#provides admin 4 choices
Button(window, text="Add Question", width=25, command=add_question_gui).pack(pady=10)
Button(window, text="View Questions", width=25, command=view_questions_gui).pack(pady=10)
Button(window, text="Modify Questions", width=25, command=modify_question_gui).pack(pady=10)
Button(window, text="Delete Questions", width=25, command=delete_question_gui).pack(pady=10)
#Sets up user's quiz
def start_quiz():
#takes user to next question
def next_question():
nonlocal index, score
if index < len(questions):
q = questions[index]
question_label.config(text=q['question_text'])
var.set(None)
for i, opt in enumerate(['A', 'B', 'C', 'D']):
options[i].config(text=f"{opt}. {q[f'option_{opt}']}")
#shows user final score
else:
messagebox.showinfo("Quiz Finished", f"Your score: {score}/{len(questions)}")
quiz_window.destroy()
#Controls what happens after user submits answer
def submit_answer():
nonlocal index, score
selected = var.get()
#Keeps track of score and provides feedback
if selected:
if selected == questions[index]['correct_answer']:
score += 1
feedback = "Correct!"
else:
feedback = f"Incorrect. The correct answer is {questions[index]['correct_answer']}."
# Show feedback after each question
messagebox.showinfo("Feedback", feedback)
index += 1
next_question()
else:
messagebox.showwarning("No Selection", "Please select an answer.")
#starts quiz
def load_questions_and_start():
course = course_combobox.get()
if not course:
messagebox.showwarning("No Course", "Select a course to start quiz.")
return
nonlocal questions
questions = get_questions_from_database(course)
if not questions:
messagebox.showinfo("No Questions", "No questions found for this course.")
return
# Show a welcome message after selecting the course
welcome_message = f"Welcome to the {course} quiz! Let's begin!"
messagebox.showinfo("Welcome", welcome_message)
quiz_selector.destroy()
next_question()
quiz_selector = Toplevel(root)
quiz_selector.title("Choose Course")
Label(quiz_selector, text="Select Course to Begin Quiz").pack(pady=10)
#gives users choices of courses
courses = ["Principles of Managerial Finance", "Mgmt Organizational Behavior",
"Business Applications Develop", "Business Database Mgmt", "Principles of Marketing"]
course_combobox = ttk.Combobox(quiz_selector, values=courses, width=50, state="readonly")
course_combobox.pack(pady=10)
Button(quiz_selector, text="Start Quiz", command=load_questions_and_start).pack(pady=10)
quiz_window = Toplevel(root)
quiz_window.title("Quiz")
quiz_window.geometry("700x400")
question_label = Label(quiz_window, text="", wraplength=600, font=("Arial", 12))
question_label.pack(pady=20)
var = StringVar()
options = [Radiobutton(quiz_window, text="", variable=var, value=opt) for opt in ["A", "B", "C", "D"]]
for opt in options:
opt.pack(anchor=W)
Button(quiz_window, text="Submit", command=submit_answer).pack(pady=20)
#resets list and scores
questions = []
index = 0
score = 0
# Root Window
root = Tk()
root.title("Quiz Application")
root.geometry("400x300")
Label(root, text="Select Mode", font=("Arial", 14)).pack(pady=30)
Button(root, text="Administrator", width=20, command=lambda: show_password_prompt()).pack(pady=10)
Button(root, text="Take a Quiz", width=20, command=start_quiz).pack(pady=10)
# Password Prompt
def show_password_prompt():
global login_window, password_entry
login_window = Toplevel(root)
login_window.title("Admin Login")
login_window.geometry("300x150")
#creates a place to put admin passcode
Label(login_window, text="Enter Admin Password:").pack(pady=10)
password_entry = Entry(login_window, show="*", width=30)
password_entry.pack(pady=5)
Button(login_window, text="Login", command=check_password).pack(pady=10)
root.mainloop()