71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
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=40, pady=40, bg=THEME_COLOR)
|
|
|
|
self.score_label = Label(text="Score: 0",
|
|
fg="white",
|
|
bg=THEME_COLOR,
|
|
font=("Times", 18, "bold")
|
|
)
|
|
self.score_label.grid(row=0, column=1)
|
|
|
|
self.canvas = Canvas(width=300, height=250, bg="white")
|
|
self.question_text = self.canvas.create_text(
|
|
150,
|
|
125,
|
|
width=280,
|
|
text="Some Question Text",
|
|
fill='black',
|
|
font=("Times", 20, "bold")
|
|
|
|
)
|
|
self.canvas.grid(row=1, column=0, columnspan=2, pady=50, padx=30)
|
|
|
|
true_image = PhotoImage(file="images/true.png")
|
|
false_image = PhotoImage(file="images/false.png")
|
|
self.true_button = Button(image=true_image, highlightthickness=0, bg=THEME_COLOR, fg=THEME_COLOR, command=self.true_pressed)
|
|
self.false_button = Button(image=false_image, highlightthickness=0, bg=THEME_COLOR, fg=THEME_COLOR, command=self.false_pressed)
|
|
self.true_button.grid(row=2, column=1)
|
|
self.false_button.grid(row=2, column=0)
|
|
|
|
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_label.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="Hey looks like you were enjoying the quiz but I am sorry"
|
|
" to inform you that we have ran out of questions")
|
|
self.true_button.config(state="disabled")
|
|
self.false_button.config(state="disabled")
|
|
|
|
def true_pressed(self):
|
|
self.give_feedback(self.quiz.check_answer("True"))
|
|
|
|
def false_pressed(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="lime green")
|
|
else:
|
|
self.canvas.config(bg="red")
|
|
|
|
self.window.after(1000, self.get_next_question)
|