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.
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
question = {
"2+3" : [ '2' , '3' , '5' , '9' ],
"2-1" : [ '2' , '1' , '5' ],
"3+3" : [ '3' , '6' , '9' , '7' ]
}
ans = [ '5' , '1' , '6' ]
|
Step 3: Define a variable to store how many questions are attempted by the user.
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()
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):
check_ans()
user_ans. set ( 'None' )
c_question = list (question.keys())[current_question]
clear_frame()
Label(f1,text = f "Question : {c_question}" ,padx = 15 ,font = "calibre 12 normal" ).pack(anchor = NW)
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 *
question = {
"2+3" : [ '2' , '3' , '5' , '9' ],
"2-1" : [ '2' , '1' , '5' ],
"3+3" : [ '3' , '6' , '9' , '7' ]
}
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):
check_ans()
user_ans. set ( 'None' )
c_question = list (question.keys())[current_question]
clear_frame()
Label(f1, text = f "Question : {c_question}" , padx = 15 ,
font = "calibre 12 normal" ).pack(anchor = NW)
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()
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
|