Open In App
Related Articles

Python – MCQ Quiz Game using Tkinter

Like Article
Save Article
Report issue

Prerequisite: Python GUI – tkinter

Python provides a standard GUI framework Tkinter which is used to develop fast and easy GUI applications. Here we will be developing a simple multiple-choice quiz in python with GUI. We will be creating a multiple choice quiz in Python with Tkinter. First, we will create a library named Quiz in the directory of your preference.


Steps Needed

1. We will create data.json for storing the data

The data for the quiz is defined in data.json with JSON data which are name/value pairs and contain an array of values. We have defined sample data for the quiz as shown below :

  "question": [
    "Q1. What Indian city is the capital of two states?",
    "Q2. Which city is the capital of India?",
    "Q3. Smallest State of India?",
    "Q4. Where is Taj Mahal Located?"
  "answer": [
  "options": [


2. Creating the GUI using Tkinter in

  1. Importing the module: tkinter and json
  2. Create the main window (container) of the app
  3. Add widgets to display data
  4. Add the functionalities to the button
  5. Using the data in the Quiz

Note: Both the data.json and the will be created in the same directory that we have defined above.

Now that we have created the data.json file for storing the data we are going to create file which will contain the program for the quiz.


# Python program to create a simple GUI
# Simple Quiz using Tkinter
#import everything from tkinter
from tkinter import *
# and import messagebox as mb from tkinter
from tkinter import messagebox as mb
#import json to use json file for data
import json
#class to define the components of the GUI
class Quiz:
    # This is the first method which is called when a
    # new object of the class is initialized. This method
    # sets the question count to 0. and initialize all the
    # other methoods to display the content and make all the
    # functionalities available
    def __init__(self):
        # set question number to 0
        # assigns ques to the display_question function to update later.
        # opt_selected holds an integer value which is used for
        # selected option in a question.
        # displaying radio button for the current question and used to
        # display options for the current question
        # display options for the current question
        # displays the button for next and exit.
        # no of questions
        # keep a counter of correct answers
    # This method is used to display the result
    # It counts the number of correct and wrong answers
    # and then display them at the end as a message Box
    def display_result(self):
        # calculates the wrong count
        wrong_count = self.data_size - self.correct
        correct = f"Correct: {self.correct}"
        wrong = f"Wrong: {wrong_count}"
        # calcultaes the percentage of correct answers
        score = int(self.correct / self.data_size * 100)
        result = f"Score: {score}%"
        # Shows a message box to display the result
        mb.showinfo("Result", f"{result}\n{correct}\n{wrong}")
    # This method checks the Answer after we click on Next.
    def check_ans(self, q_no):
        # checks for if the selected option is correct
        if self.opt_selected.get() == answer[q_no]:
            # if the option is correct it return true
            return True
    # This method is used to check the answer of the
    # current question by calling the check_ans and question no.
    # if the question is correct it increases the count by 1
    # and then increase the question number by 1. If it is last
    # question then it calls display result to show the message box.
    # otherwise shows next question.
    def next_btn(self):
        # Check if the answer is correct
        if self.check_ans(self.q_no):
            # if the answer is correct it increments the correct by 1
            self.correct += 1
        # Moves to next Question by incrementing the q_no counter
        self.q_no += 1
        # checks if the q_no size is equal to the data size
        if self.q_no==self.data_size:
            # if it is correct then it displays the score
            # destroys the GUI
            # shows the next question
    # This method shows the two buttons on the screen.
    # The first one is the next_button which moves to next question
    # It has properties like what text it shows the functionality,
    # size, color, and property of text displayed on button. Then it
    # mentions where to place the button on the screen. The second
    # button is the exit button which is used to close the GUI without
    # completing the quiz.
    def buttons(self):
        # The first button is the Next button to move to the
        # next Question
        next_button = Button(gui, text="Next",command=self.next_btn,
        # placing the button  on the screen,y=380)
        # This is the second button which is used to Quit the GUI
        quit_button = Button(gui, text="Quit", command=gui.destroy,
        width=5,bg="black", fg="white",font=("ariel",16," bold"))
        # placing the Quit button on the screen,y=50)
    # This method deselect the radio button on the screen
    # Then it is used to display the options available for the current
    # question which we obtain through the question number and Updates
    # each of the options for the current question of the radio button.
    def display_options(self):
        # deselecting the options
        # looping over the options to be displayed for the
        # text of the radio buttons.
        for option in options[self.q_no]:
    # This method shows the current Question on the screen
    def display_question(self):
        # setting the Question properties
        q_no = Label(gui, text=question[self.q_no], width=60,
        font=( 'ariel' ,16, 'bold' ), anchor= 'w' )
        #placing the option on the screen, y=100)
    # This method is used to Display Title
    def display_title(self):
        # The title to be shown
        title = Label(gui, text="GeeksforGeeks QUIZ",
        width=50, bg="green",fg="white", font=("ariel", 20, "bold"))
        # place of the title, y=2)
    # This method shows the radio buttons to select the Question
    # on the screen at the specified position. It also returns a
    # list of radio button which are later used to add the options to
    # them.
    def radio_buttons(self):
        # initialize the list with an empty list of options
        q_list = []
        # position of the first option
        y_pos = 150
        # adding the options to the list
        while len(q_list) < 4:
            # setting the radio button properties
            radio_btn = Radiobutton(gui,text=" ",variable=self.opt_selected,
            value = len(q_list)+1,font = ("ariel",14))
            # adding the button to the list
            # placing the button
   = 100, y = y_pos)
            # incrementing the y-axis position by 40
            y_pos += 40
        # return the radio buttons
        return q_list
# Create a GUI Window
gui = Tk()
# set the size of the GUI Window
# set the title of the Window
gui.title("GeeksforGeeks Quiz")
# get the data from the json file
with open('data.json') as f:
    data = json.load(f)
# set the question, options, and answer
question = (data['question'])
options = (data['options'])
answer = (data[ 'answer'])
# create an object of the Quiz Class.
quiz = Quiz()
# Start the GUI




Last Updated : 20 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads