Last active
February 20, 2022 02:25
-
-
Save Jithender5913/56a4d150f02120dcd209823831c1d288 to your computer and use it in GitHub Desktop.
Quizzer App using Python Application Programming Interface(API), Tkinter GUI and OOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from data import question_data | |
from question_model import Question | |
from quiz_brain import QuizBrain | |
from ui import QuizInterface | |
question_bank = [] | |
for question in question_data: | |
question_text = question["question"] | |
question_answer = question["correct_answer"] | |
new_question = Question(question_text, question_answer) | |
question_bank.append(new_question) | |
quiz = QuizBrain(question_bank) | |
quiz_ui = QuizInterface(quiz) | |
# class QuizInterface | |
from tkinter import * | |
from quiz_brain import QuizBrain | |
THEME_COLOR = "#375362" | |
class QuizInterface: # this is pascal case | |
def __init__(self, quiz_brain: QuizBrain): | |
self.quiz = quiz_brain # we created a property called self quiz that we receive when we initialise this new quiz interface | |
self.window = Tk() # we used self to make it as a property of this class | |
self.window.title("Quizzer") | |
self.window.config(padx=20, pady=20, bg=THEME_COLOR) | |
self.score_label = Label(text="Score: 0", fg="white", bg=THEME_COLOR) | |
self.score_label.grid(row=0, column=1) | |
self.canvas = Canvas(height=250, width=300, bg="white") | |
self.question_text = self.canvas.create_text(150, 125, text="some question text", fill=THEME_COLOR, | |
width=280, | |
font=("Arial", 20, "italic")) | |
self.canvas.grid(row=1, column=0, columnspan=2, pady=50) | |
right_button_image = PhotoImage(file="images/true.png") # for right and wrong buttons, we did not use self | |
# keyword because we are not going to use them anywhere else apart from here | |
self.right_button = Button(image=right_button_image, highlightthickness=0, command=self.right_button_pressed) | |
self.right_button.grid(row=2, column=0) | |
wrong_button_image = PhotoImage(file="images/false.png") | |
self.wrong_button = Button(image=wrong_button_image, highlightthickness=0, command=self.wrong_button_pressed) | |
self.wrong_button.grid(row=2, column=1) | |
self.get_next_question() # everything has to go right before mainloop in tkinter | |
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="You have reached the end of the quiz. Thanks for playing!") | |
self.right_button.config(state="disabled") | |
self.wrong_button.config(state="disabled") | |
def right_button_pressed(self): | |
is_right = self.quiz.check_answer("true") | |
self.give_feedback(is_right) | |
def wrong_button_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="green") | |
else: | |
self.canvas.config(bg="red") | |
self.window.after(1000, self.get_next_question) | |
# class QuizBrain | |
import html | |
class QuizBrain: | |
def __init__(self, q_list): | |
self.question_number = 0 | |
self.score = 0 | |
self.question_list = q_list | |
self.current_question = None | |
def still_has_questions(self): | |
return self.question_number < len(self.question_list) | |
def next_question(self): | |
self.current_question = self.question_list[self.question_number] | |
self.question_number += 1 | |
q_text = html.unescape(self.current_question.text) | |
return f"Q.{self.question_number}:{q_text}" | |
# user_answer = input(f"Q.{self.question_number}: {q_text} (True/False): ") | |
# self.check_answer(user_answer) | |
def check_answer(self, user_answer): | |
correct_answer = self.current_question.answer | |
if user_answer.lower() == correct_answer.lower(): | |
self.score += 1 | |
return True | |
else: | |
return False | |
# class QuestionModel | |
class Question: | |
def __init__(self, q_text, q_answer): | |
self.text = q_text | |
self.answer = q_answer | |
# data API | |
import requests | |
parameters = { | |
"amount": 20, | |
"type": "boolean", | |
"category": 18, | |
} | |
response = requests.get("https://opentdb.com/api.php", params=parameters) | |
response.raise_for_status() | |
data = response.json() | |
question_data = data["results"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment