from tkinter import *
from quiz_brain import QuizBrain
THEME_COLOR = "#375362"
class QuizInterface:
def __init__(self, quiz_brain: QuizBrain):
self.quiz = quiz_brain
self.window = Tk()
self.window.title("Quizzler")
self.window.config(padx=20, pady=20, bg=THEME_COLOR)
self.canvas = Canvas(width=300, height=250, bg="white")
self.question_text = self.canvas.create_text(
150,
125,
width=280,
text="some question",
fill=THEME_COLOR,
font=("Ariel", 20, "italic"))
self.canvas.grid(column=0, row=1, columnspan=2, pady=20)
self.score = Label(text="Score=0", fg="white", bg=THEME_COLOR, font=("Ariel", 20))
self.score.grid(column=1, row=0)
self.score.config(pady=20)
# Almost everything that I've created inside my init, I've given it the 'self.'
# And remember from our lessons on OOP, this turns this into a property which can be accessed anywhere in the class.
# Now, some other things like the true image and the false image,
# I've not done that for because we're not going to use it anywhere else other than to set up our button right here.
true_image = PhotoImage(file="images/true.png")
false_image = PhotoImage(file="images/false.png")
self.true_button = Button(image=true_image, highlightthickness=0, command=self.click_true)
self.false_button = Button(image=false_image, highlightthickness=0, command=self.click_false)
self.true_button.grid(column=0, row=2, pady=20)
self.false_button.grid(column=1, row=2, pady=20)
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.config(text=f"Score={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.true_button.config(state="disabled")
self.false_button.config(state="disabled")
def click_true(self) :
# is_right = self.quiz.check_answer("True")
self.give_feedback(self.quiz.check_answer("True"))
def click_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)