Repository navigation
Expand file tree
/
Copy pathquiz_ui_both.py
More file actions
215 lines (161 loc) · 7.6 KB
/
Copy pathquiz_ui_both.py
File metadata and controls
215 lines (161 loc) · 7.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
from tkinter import Tk, Canvas, StringVar, Label, Radiobutton, Button, messagebox
from quiz_brain_both import QuizBrain
THEME_COLOR = "#375362"
class SettingsWindow:
def __init__(self):
self.window = Tk()
self.window.title("Start Window")
self.window.geometry("850x530")
self.number_of_players = None
self.current_team = None
# Add labels, buttons, and other widgets for settings
# ...
self.choose_solo_or_team_screen()
self.window.mainloop()
def choose_solo_or_team_screen(self):
"""To display choose mode screen"""
# Choose team label
choose_team_label = Label(self.window, text="Choose mode:", font=("ariel", 16, "bold"))
choose_team_label.place(x=350, y=150)
# Team buttons
solo_button = Button(self.window, text="One Player", width=10, font=("ariel", 14),
command=lambda: self.start_game(1, None))
solo_button.place(x=250, y=200)
team_button = Button(self.window, text="Two Players", width=10, font=("ariel", 14),
command=lambda: self.choose_team_screen())
team_button.place(x=450, y=200)
def choose_team_screen(self):
"""To display choose team screen"""
# Choose team label
choose_team_label = Label(self.window, text="Choose team:", font=("ariel", 16, "bold"))
choose_team_label.place(x=350, y=150)
# Team buttons
team1_button = Button(self.window, text="Team 1", width=10, font=("ariel", 14),
command=lambda: self.start_game(2, 1))
team1_button.place(x=250, y=200)
team2_button = Button(self.window, text="Team 2", width=10, font=("ariel", 14),
command=lambda: self.start_game(2, 0))
team2_button.place(x=450, y=200)
def start_game(self, number_of_players, current_team):
self.number_of_players = number_of_players
self.current_team = current_team
self.window.destroy()
class QuizInterface:
def __init__(self, quiz_brain: QuizBrain) -> None:
self.quiz = quiz_brain
self.window = Tk()
self.window.title("Quiz App")
self.window.geometry("850x530")
# Creating a canvas for question text, and display question
self.canvas = Canvas(width=800, height=250)
self.question_text = self.canvas.create_text(400, 125,
text="Question here",
width=680,
fill=THEME_COLOR,
font=(
'Ariel', 15, 'italic')
)
self.canvas.grid(row=2, column=0, columnspan=2, pady=50)
self.display_question()
# Declare a StringVar to store user's answer
self.user_answer = StringVar()
# Display four options(radio buttons)
self.opts = self.radio_buttons()
self.display_options()
# To show whether the answer is correct or wrong
self.feedback = Label(self.window, pady=10, font=("ariel", 15, "bold"))
self.feedback.place(x=300, y=380)
# Next and Quit Button
self.buttons()
# Team scores
self.team_scores = [0, 0]
self.current_team = 0
# Mainloop
self.window.mainloop()
def start_game(self, number_of_players, team):
"""To start the game with the chosen team"""
self.number_of_players = number_of_players
self.current_team = team
# Remove the choose team screen
self.window.destroy()
# Create the game screen
QuizInterface(quiz_brain=self.quiz)
def display_question(self):
"""To display the question"""
q_text = self.quiz.next_question()
self.canvas.itemconfig(self.question_text, text=q_text)
def radio_buttons(self):
"""To create four options (radio buttons)"""
# initialize the list with an empty list of options
choice_list = []
# position of the first option
y_pos = 220
# adding the options to the list
while len(choice_list) < 4:
# setting the radio button properties
radio_btn = Radiobutton(self.window, text="", variable=self.user_answer,
value='', font=("ariel", 14))
# adding the button to the list
choice_list.append(radio_btn)
# placing the button
radio_btn.place(x=200, y=y_pos)
# incrementing the y-axis position by 40
y_pos += 40
# return the radio buttons
return choice_list
def display_options(self):
"""To display four options"""
val = 0
# deselecting the options
self.user_answer.set(None)
# looping over the options to be displayed for the
# text of the radio buttons.
for option in self.quiz.current_question.choices:
self.opts[val]['text'] = option
self.opts[val]['value'] = option
val += 1
def next_btn(self):
"""To show feedback for each answer and keep checking for more questions"""
# Check if the answer is correct
if self.quiz.check_answer(self.user_answer.get()):
self.feedback["fg"] = "green"
self.feedback["text"] = 'Correct answer!'
else:
self.feedback['fg'] = 'red'
self.feedback['text'] = ('Oops! \n'
f'The right answer is: {self.quiz.current_question.correct_answer}')
if self.quiz.has_more_questions():
# Moves to next to display next question and its options
self.display_question()
self.display_options()
else:
# if no more questions, then it displays the score
self.display_result()
# destroys the self.window
self.window.destroy()
def buttons(self):
"""To show next button and quit button"""
# The first button is the Next button to move to the
# next Question
next_button = Button(self.window, text="Next", command=self.next_btn,
width=10, bg="green", fg="white", font=("ariel", 16, "bold"))
# palcing the button on the screen
next_button.place(x=350, y=460)
# This is the second button which is used to Quit the self.window
quit_button = Button(self.window, text="Quit", command=self.window.destroy,
width=5, bg="red", fg="white", font=("ariel", 16, " bold"))
# placing the Quit button on the screen
quit_button.place(x=700, y=50)
def display_result(self):
"""To display the result using messagebox"""
score, score_percent,score_percent_team_1, score_percent_team_2 = self.quiz.get_score()
# calculates the percentage of correct answers
if self.quiz.number_of_players == 1:
result = f"Score: {score_percent}%"
# Shows a message box to display the result
messagebox.showinfo("Result", f"{result}")
else:
result1 = f"Score: {score_percent_team_1}%"
result2 = f"Score: {score_percent_team_2}%"
# Shows a message box to display the result
messagebox.showinfo("Result", f"Team 1: {result1}\nTeam 2: {result2}")