Day 34 - Quizzler β True/False Trivia Quiz (Tkinter)
A simple GUI quiz app that pulls True/False questions from an online API and lets you answer them in a clean interface.
README GENERATED BY AI
π§ Overview
This project is a Python quiz application built with Tkinter and requests.
When you run it, the app:
- Fetches 10 random True/False questions from the Open Trivia DB API
- Displays one question at a time in a Tkinter window
- Lets you answer using β True / β False buttons
- Keeps track of your current score
- Shows a completion message when you finish the quiz
Itβs a great example of combining:
- API calls
- Object-oriented design (
Question,QuizBrain,QuizInterface) - A graphical UI with Tkinter
π Project Structure
```text . βββ main.py # Entry point: fetches questions and starts the quiz βββ ui.py # Tkinter UI: QuizInterface class, buttons, labels, canvas βββ question_model.py # Question class (models a single question) βββ quiz_brain.py # QuizBrain class (quiz logic, scoring, question flow) βββ images/ βββ true.png # True button image βββ false.png # False button image ````
Note: Only
main.pyandui.pyare shown here, butquestion_model.pyandquiz_brain.pyare small helper modules.
βοΈ Requirements
- Python 3.10+ (earlier versions likely fine too)
-
The following Python packages:
-
requests(for the API call) tkinter(comes with most Python installations)
Install requests with:
bash
pip install requests
Youβll also need:
images/true.pngimages/false.png
in an images folder next to your .py files.
π How to Run
- Make sure the project files are in the same folder:
text
main.py
ui.py
question_model.py
quiz_brain.py
/images
βββ true.png
βββ false.png
- Install dependencies:
bash
pip install requests
- Run the main script:
bash
python main.py
- A window titled βQuizzlerβ should appear.
π§© How It Works (High-Level)
main.py
- Calls the Open Trivia DB API:
python
question_data = requests.get(
url="https://opentdb.com/api.php?amount=10&type=boolean"
).json()["results"]
-
Builds a
question_banklist ofQuestionobjects. -
Creates a
QuizBraininstance to handle quiz logic. -
Passes
QuizBrainintoQuizInterfaceto start the GUI. -
(Thereβs also a CLI-style
while quiz.still_has_questions()loop at the bottom, but the GUI handles most of the interaction now.)
ui.py β QuizInterface
-
Sets up the Tkinter window:
-
Title:
"Quizzler" - Background color:
THEME_COLOR = "#375362" - Score label in the top-right
- Canvas in the center to display the question text
-
Two image buttons:
- β
True (
true.png) - β False (
false.png)
- β
True (
-
Calls
self.get_next_question()to load the first question. -
When you click a button, it calls:
python
self.check_answer(True) # or False
-
Uses methods on the
QuizBraininstance (likenext_question()andcheck_answer(...)) to: -
Move to the next question
- Update the score
- Detect when the quiz is finished
β Features
- Fetches live questions from an online trivia API
- Simple, intuitive True/False interface
- Score tracking in the UI
- End-of-quiz message when you finish all 10 questions
-
Clean separation of:
-
Data model (
Question) - Logic (
QuizBrain) - UI (
QuizInterface)
π‘ Possible Improvements
Some ideas if you want to expand this project later:
- Add a difficulty selector (easy/medium/hard) using the APIβs parameters
- Let the user pick number of questions
- Show feedback colors (e.g., green for correct, red for wrong) for a second before moving on
- Add categories (e.g., film, science, history)
- Store high scores in a local file (e.g., JSON or text)
- Add a βPlay Againβ button when the quiz ends
π§Ύ Notes
- This project was written in Python and uses Tkinter for the GUI.
- Trivia data is provided by the Open Trivia Database (
https://opentdb.com/api_config.php). - README GENERATED BY AI
- This was actually a modified version of 'The Quiz' Project from a while back. The most notable differences are the tkinter UI, and the custom data from the Open Trivia Database.