-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
69 lines (53 loc) · 2.61 KB
/
ui.py
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
from tkinter import *
from quiz_brain import QuizBrain
THEME_COLOR = "#375362"
class QuizInterface:
def __init__(self, quiz_brain: QuizBrain): # declaring that the parameter quiz_brain is an object of the QuizBrain class
self.quiz = quiz_brain
# creating GUI window
self.window = Tk()
self.window.title("Quizzler")
self.window.config(padx=20, pady=20, bg=THEME_COLOR)
self.score_lb = Label(text='Score: 0', fg='white', bg=THEME_COLOR)
self.score_lb.grid(row=0, column=1)
# creating quetion canvas
self.canvas = Canvas(width=300, height=250, bg='white')
# creating question text
self.question_text = self.canvas.create_text(150, 125,
text='Question Goes HERE',
fill=THEME_COLOR,
font=('Arial', 20, 'italic'),
width=280
)
self.canvas.grid(row=1, column=0, columnspan=2, pady=50)
# creating true and false buttons
true_img = PhotoImage(file="./true.png")
false_img = PhotoImage(file="./false.png")
self.right_btn = Button(image=true_img, highlightthickness=0, command=self.check_answer_true) # add command here
self.right_btn.grid(row=2, column=0)
self.wrong_btn = Button(image=false_img, highlightthickness=0, command=self.check_answer_false) # add command here
self.wrong_btn.grid(row=2, column=1)
self.get_next_question()
self.window.mainloop()
def get_next_question(self):
self.canvas.config(bg='white')
if self.quiz.still_has_questions():
self.score_lb.config(text="Score: " + str(self.quiz.score))
q_text = self.quiz.next_question()
self.canvas.itemconfig(self.question_text, text=q_text)
else:
self.canvas.itemconfig(self.question_text, text="You've reached the end of the quiz! ")
self.right_btn.config(state='disabled')
self.left_btn.config(state='disabled')
def check_answer_true(self):
is_right = self.quiz.check_answer(True)
self.give_feedback(is_right)
def check_answer_false(self):
is_right = self.quiz.check_answer(False)
self.give_feedback(is_right)
def give_feedback(self, is_right):
if is_right:
self.canvas.config(bg='green')
else:
self.canvas.config(bg='red')
self.window.after(1000, self.get_next_question())