Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 210b805

Browse files
committedFeb 9, 2021
Update Day-34
1 parent cd54cb2 commit 210b805

File tree

8 files changed

+149
-0
lines changed

8 files changed

+149
-0
lines changed
 

‎Day-34-QuizGUI/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Quiz GUI
2+
3+
### An implementation of Quiz-GUI.
4+
5+
<img src= 'https://user-images.githubusercontent.com/65078610/107326608-bc03b200-6ad1-11eb-88db-138d7375c56b.gif' width="400">

‎Day-34-QuizGUI/data.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import requests
2+
import json
3+
4+
parameters = {
5+
'amount' : 10,
6+
'category' : 20,
7+
# 'difficulty' : 'easy',
8+
'type' : 'boolean'
9+
}
10+
response = requests.get(url='https://opentdb.com/api.php', params=parameters)
11+
response.raise_for_status()
12+
data = response.json()
13+
14+
question_data = data['results']

‎Day-34-QuizGUI/images/false.png

2.8 KB
Loading

‎Day-34-QuizGUI/images/true.png

2.5 KB
Loading

‎Day-34-QuizGUI/main.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from data import question_data
2+
from question_model import Question
3+
from quiz_brain import QuizBrain
4+
from ui import QuizInterface
5+
6+
question_bank = []
7+
for question in question_data:
8+
question_object = Question(question['question'], question['correct_answer'])
9+
question_bank.append(question_object)
10+
11+
quiz = QuizBrain(question_bank)
12+
quiz_ui = QuizInterface(quiz)
13+
14+
# Declare datatypes for variables and output
15+
# def greeting(name: str) -> str:
16+
# return 'Hello ' + name

‎Day-34-QuizGUI/question_model.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Question:
2+
3+
def __init__(self, q_text, q_answer):
4+
self.text = q_text
5+
self.answer = q_answer

‎Day-34-QuizGUI/quiz_brain.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import html
2+
3+
class QuizBrain:
4+
5+
def __init__(self, q_list):
6+
self.question_number = 0
7+
self.question_list = q_list
8+
self.score = 0
9+
10+
def still_has_questions(self):
11+
return self.question_number < len(self.question_list)
12+
13+
def next_question(self):
14+
self.current_question = self.question_list[self.question_number]
15+
self.question_number += 1
16+
# Unescape used to remove html entities
17+
return (f"Q.{self.question_number}: {html.unescape(self.current_question.text)}")
18+
19+
def check_answer(self, user_answer):
20+
correct_answer = self.current_question.answer
21+
if user_answer.title() == correct_answer:
22+
self.score += 1
23+
return True
24+
else:
25+
return False

‎Day-34-QuizGUI/ui.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from tkinter import *
2+
THEME_COLOR = "#375362"
3+
FONT1 = ('Arial',14,'italic')
4+
FONT2 = ('Arial',12,'normal')
5+
6+
class QuizInterface():
7+
8+
# Optional, can pass quiz_brain datatype
9+
# from quiz_brain import QuizBrain
10+
# def __init__(self, quiz_brain: QuizBrain)
11+
12+
def __init__(self, quiz_brain):
13+
self.quiz = quiz_brain
14+
self.window = Tk()
15+
self.window.title("Quiz GUI")
16+
self.window.config(padx=20, pady=20, bg=THEME_COLOR)
17+
18+
# Label
19+
self.score_label = Label(text='Score: 0', fg='white', bg=THEME_COLOR, font=FONT2)
20+
self.score_label.grid(row=1, column=2)
21+
22+
# Canvas
23+
self.canvas = Canvas(width=300, height=250, bg='white')
24+
self.question_text = self.canvas.create_text(
25+
150, 125,
26+
text='Hola pepsi cola',
27+
width=200,
28+
fill=THEME_COLOR,
29+
font=FONT1
30+
)
31+
self.canvas.grid(row=2, column=1, columnspan=2, pady=50)
32+
33+
# Buttons
34+
check_img = PhotoImage(file='images/true.png')
35+
self.check_btn = Button(image=check_img, highlightthickness=0, command=self.true_pressed, bd=0)
36+
self.check_btn.grid(row=3, column=1)
37+
cross_img = PhotoImage(file='images/false.png')
38+
self.cross_btn = Button(image=cross_img, highlightthickness=0, command=self.false_pressed, bd=0)
39+
self.cross_btn.grid(row=3, column=2)
40+
41+
self.get__question()
42+
43+
self.window.mainloop()
44+
45+
def get__question(self):
46+
self.enable_btn()
47+
self.canvas.config(bg='white')
48+
if self.quiz.still_has_questions():
49+
self.score_label.config(text=f'Score: {self.quiz.score}')
50+
qtext = self.quiz.next_question()
51+
self.canvas.itemconfig(self.question_text, text=qtext)
52+
else:
53+
self.canvas.itemconfig(
54+
self.question_text,
55+
text=f"You've completed the quiz.\nYour final score: {self.quiz.score}/10"
56+
)
57+
self.check_btn.config(state='disabled')
58+
self.cross_btn.config(state='disabled')
59+
self.disable_btn()
60+
61+
def true_pressed(self):
62+
is_right = self.quiz.check_answer("True")
63+
self.give_feedback(is_right)
64+
65+
66+
def false_pressed(self):
67+
is_right = self.quiz.check_answer("False")
68+
self.give_feedback(is_right)
69+
70+
def give_feedback(self, is_right):
71+
self.disable_btn()
72+
if is_right:
73+
self.canvas.config(bg='green')
74+
else:
75+
self.canvas.config(bg='red')
76+
self.window.after(1000, self.get__question)
77+
78+
def disable_btn(self):
79+
self.check_btn.config(state='disabled')
80+
self.cross_btn.config(state='disabled')
81+
82+
def enable_btn(self):
83+
self.check_btn.config(state='normal')
84+
self.cross_btn.config(state='normal')

0 commit comments

Comments
 (0)
Please sign in to comment.