forked from hermonochy/QuizMasterMini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz
More file actions
executable file
·110 lines (88 loc) · 3.67 KB
/
Copy pathquiz
File metadata and controls
executable file
·110 lines (88 loc) · 3.67 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
#!/usr/bin/env python3
import PySimpleGUI as sg
import random
import json
import subprocess
from dataclasses import dataclass, field
from typing import List, Tuple
@dataclass
class QuizQuestion:
question: str
correctAnswer: str
wrongAnswers: List[str]
timeout: int = field(default=10)
def __repr__(self):
return self.question
def load_quiz(filename: str) -> Tuple[List[QuizQuestion], str]:
with open(filename, 'r') as file:
quizDicts = json.load(file)
questionList = []
for q in quizDicts["listOfQuestions"]:
qq = QuizQuestion(**q)
questionList.append(qq)
titleofquiz = quizDicts["title"]
return questionList, titleofquiz
def Home_layout():
return [
[sg.Text('Welcome to QuizMasterMini', font=("Calibri", 24))],
[sg.Text('_'*70)],
[sg.Button('Make a Quiz', size = (30,2)), sg.Button('Play a Quiz', size = (30,2))],
[sg.Text('_'*65)],
[sg.Button("Quit")],
]
def quiz_layout():
return [
[sg.Text('Select the correct answer:', font=("Calibri", 15), size=(100, 1))],
[sg.Text('', font=("Calibri", 15), size=(100, 2), key='question')],
[sg.Listbox([], bind_return_key=True, enable_events=True, font=("Calibri", 12), size=(150, 10), key='answers', select_mode='single')],
[sg.Button('Quit')],
[sg.Text('', size=(30, 1), key='score')]
]
def main():
home_window = sg.Window('Quiz', Home_layout(), finalize=True)
while True:
event, values = home_window.read()
if event == sg.WIN_CLOSED or event == "Quit":
break
if event == 'Make a Quiz':
subprocess.Popen(["python3", "quizcreator"])
if event == 'Play a Quiz':
filename = sg.popup_get_file("Please select a quiz:", initial_folder="Quizzes",
no_window=True, file_types=(('Quiz files', '.json'),))
if not filename:
continue
questions, title = load_quiz(filename)
question_index = 0
score = 0
start_quiz = False
quiz_window = sg.Window('Quiz', quiz_layout(), finalize=True)
while question_index < len(questions):
current_question = questions[question_index]
correct_answer = current_question.correctAnswer
wrong_answers = current_question.wrongAnswers
answers = random.sample([correct_answer] + wrong_answers, len(wrong_answers) + 1)
quiz_window['question'].update(current_question.question)
quiz_window['answers'].update(values=answers)
quiz_window['score'].update(f'Score: {score}', visible=start_quiz)
event, values = quiz_window.read()
if event == sg.WIN_CLOSED or event == 'Quit':
quiz_window.close()
break
if start_quiz:
if event in ('answers', 'Enter'):
if values['answers']:
if values['answers'][0] == correct_answer:
score += 1
else:
sg.popup('Please select an answer', keep_on_top=True)
continue
question_index += 1
if question_index >= len(questions):
sg.popup(f'Quiz completed! Your score: {score}/{len(questions)}')
break
else:
start_quiz = True
quiz_window.close()
home_window.close()
if __name__ == "__main__":
main()