Python - Quiz Application Project
Last Updated :
28 Apr, 2025
Python provides us with many libraries to create GUI(Graphical User Interface), and Tkinter is one of them. Creating GUI with Tkinter is very easy and it is the most commonly used library for GUI development. In this article, we will create a Quiz application using Tkinter. A Quiz application has a set of questions and their answers and it checks for the correctness of answers that are submitted by users.
Module Required
pip install tkinter
Steps Needed to Create Quiz Application Using Tkinter
Step 1: Import required modules.
Python3
Step 2: Store questions, options, and their correct answer in variables. We will use Python dictionaries to store questions and their respective options. And we will use a Python list to store correct answers.
Python3
# define question dictionary
question = {
"2+3": ['2', '3', '5', '9'],
"2-1": ['2', '1', '5'],
"3+3": ['3', '6', '9', '7']
}
# define answer list
ans = ['5', '1', '6']
Step 3: Define a variable to store how many questions are attempted by the user.
Python3
Step 4: Points to remember while creating a basic GUI window.
- Create Tkinter variables to get answers submitted by a user and to show the score last.
- Create a Label to Give Heading to our GUI.
- Create a button to start the quiz. When the user will click this button start_quiz function will be called.
- Define a frame, this frame will be used to show questions and take user responses.
- Define a button to show the next question but don't pack it as we don't want to see it until the user starts the quiz.
Python3
if __name__ == "__main__":
root = Tk()
# setup basic window
root.title("GFG QUIZ APP")
root.geometry("850x520")
root.minsize(800, 400)
user_ans = StringVar()
user_ans.set('None')
user_score = IntVar()
user_score.set(0)
Label(root, text="Quiz App",
font="calibre 40 bold",
relief=SUNKEN, background="cyan",
padx=10, pady=9).pack()
Label(root, text="",
font="calibre 10 bold").pack()
start_button = Button(root, text="Start Quiz",
command=start_quiz,
font="calibre 17 bold")
start_button.pack()
f1 = Frame(root)
f1.pack(side=TOP, fill=X)
next_button = Button(root, text="Next Question",
command=next_question,
font="calibre 17 bold")
root.mainloop()
Step 5: Define the start_quiz function. This function is used to remove the start quiz button and render the next question button. It also calls the next_question function to render the question.
Python3
def start_quiz():
start_button.forget()
next_button.pack()
next_question()
Step 6: Define the next_question function. This function is used to render questions, and options in GUI. And when a user attempts all the questions, it will render the score.
Python3
def next_question():
global current_question
if current_question<len(question):
# get key or question that need to be printed
check_ans()
user_ans.set('None')
c_question = list(question.keys())[current_question]
# clear frame to update its content
clear_frame()
# printing question
Label(f1,text=f"Question : {c_question}",padx=15,font="calibre 12 normal").pack(anchor=NW)
# printing options
for option in question[c_question]:
Radiobutton(f1,text=option,variable=user_ans,value=option,padx=28).pack(anchor=NW)
current_question+=1
else:
next_button.forget()
check_ans()
clear_frame()
output = f"Your Score is {user_score.get()} out of {len(question)}"
Label(f1,text=output,font="calibre 25 bold").pack()
Label(f1,text="Thanks for Participating",font="calibre 18 bold").pack()
Step 7: Define the check_ans function. This function is used to check the answer that is submitted by the user and update the score.
Python3
def check_ans():
temp_ans = user_ans.get()
if temp_ans != 'None' and temp_ans==ans[current_question-1]:
user_score.set(user_score.get()+1)
Step 8: Define the clear_frame function. This function is used to clear the frame(remove all widgets) so that we can update its content.
Python3
def clear_frame():
for widget in f1.winfo_children():
widget.destroy()
Complete Code:
Python3
from tkinter import *
# define question dictionary
question = {
"2+3": ['2', '3', '5', '9'],
"2-1": ['2', '1', '5'],
"3+3": ['3', '6', '9', '7']
}
# define answer list
ans = ['5', '1', '6']
current_question = 0
def start_quiz():
start_button.forget()
next_button.pack()
next_question()
def next_question():
global current_question
if current_question < len(question):
# get key or question that need to be printed
check_ans()
user_ans.set('None')
c_question = list(question.keys())[current_question]
# clear frame to update its content
clear_frame()
# printing question
Label(f1, text=f"Question : {c_question}", padx=15,
font="calibre 12 normal").pack(anchor=NW)
# printing options
for option in question[c_question]:
Radiobutton(f1, text=option, variable=user_ans,
value=option, padx=28).pack(anchor=NW)
current_question += 1
else:
next_button.forget()
check_ans()
clear_frame()
output = f"Your Score is {user_score.get()} out of {len(question)}"
Label(f1, text=output, font="calibre 25 bold").pack()
Label(f1, text="Thanks for Participating",
font="calibre 18 bold").pack()
def check_ans():
temp_ans = user_ans.get()
if temp_ans != 'None' and temp_ans == ans[current_question-1]:
user_score.set(user_score.get()+1)
def clear_frame():
for widget in f1.winfo_children():
widget.destroy()
if __name__ == "__main__":
root = Tk()
# setup basic window
root.title("GFG QUIZ APP")
root.geometry("850x520")
root.minsize(800, 400)
user_ans = StringVar()
user_ans.set('None')
user_score = IntVar()
user_score.set(0)
Label(root, text="Quiz App",
font="calibre 40 bold",
relief=SUNKEN, background="cyan",
padx=10, pady=9).pack()
Label(root, text="", font="calibre 10 bold").pack()
start_button = Button(root,
text="Start Quiz",
command=start_quiz,
font="calibre 17 bold")
start_button.pack()
f1 = Frame(root)
f1.pack(side=TOP, fill=X)
next_button = Button(root, text="Next Question",
command=next_question,
font="calibre 17 bold")
root.mainloop()
Output:
output
Related Articles:Â Python - MCQ Quiz Game using Tkinter - GeeksforGeeks
Similar Reads
Creating Your First Application in Python Python is one of the simplest and most beginner-friendly programming languages available today. It was designed with the goal of making programming easy and accessible, especially for newcomers. In this article, we will guide you through creating your very first Python application from a simple prin
4 min read
Quiz Application using Django In this article, we will create the Django Quiz Application generally the Django Quiz App is a versatile and interactive web application designed to revolutionize learning and assessment. Created to address the need for engaging and adaptable online quizzes, it offers educators, businesses, and indi
6 min read
Create a Quiz Application Using JavaScript In this article, we will learn how to create a quiz application using JavaScript. The quiz application will contain questions followed by a total score shown at the end of the quiz. The score will increase based on the correct answers given. Initially, there are only three questions, but you can inc
4 min read
HAUS Connect - Python Project One of the most common problems faced by college students is that we have very erratic timetables and it is a cumbersome task to keep up with them, these issues have been amplified enormously during these pandemic times where everyone is on their own. The purpose of this application is to help stude
15+ min read
Create First GUI Application using Python-Tkinter We are now stepping into making applications with graphical elements, we will learn how to make cool apps and focus more on its GUI(Graphical User Interface) using Tkinter.What is Tkinter?Tkinter is a Python Package for creating GUI applications. Python has a lot of GUI frameworks, but Tkinter is th
12 min read