Quiz app in python with source code

Hello friends how are you, Today in this post "Quiz app in python with source code" i am going to teach you how you can create a QUIZ Application in Python using Tkinter library. If you are a student from computer science field then it may be a good project for you because i have made this application that look like a real Online Quiz Application.

I have used Tkinter library to create a Graphical User Interface (GUI) layout and Label for displaying question and options and I have used Radiobutton for user's selection from the given answer options. 

Here in my post "Quiz app in python with source code" you will get the complete code of this project. I have used very simple code to create this application so i think you will love this application. I have explained every single line of this application in details using comment so that you can understand this code easily and my aim is also to teach programming to everyone in a very easy way.


Quiz app in python with source code

Now i am going to provide the complete code of this application, You just need to type this code in your python editor or you can copy this also for your personal use only.

Note: Always use Python 3 or higher version of Python for this kind of application. 

Complete code of Quiz App in Python

Here in this application i have stored only 5 question but you can increase the number of question as per your requirements.

# Importing Tkinter module
from tkinter import *
from tkinter import  messagebox
# Creating master Tkinter window 
master = Tk()
#setting geometry for layout
master.geometry("475x275")
#title for window
master.title("Quiz by krazyprogrammers")
#background of window
master["bg"]="#04112d"
#list for storing  questions
QueList=["Who developed C?","Who developed Python?","When Python is released?","Which of the following is not a keyword?","What is the answer to this expression, 22 % 3 is?"]
#list for storing options
OptList=[["Denish Ritchie" ,"Bjarne Stroustrup" ,"James Gosling" ,"Tim Berners Lee"],["Denish Ritchie" ,"Bjarne Stroustrup" ,"James Gosling" ,"Guido van Rossum"],["1992","1991","1990","1994"],["assert","nonlocal","pass","eval"],["5","7","1","0"]]
#list for storing correct output
OutList=["1","4","2","4","3"]
#initiaize variable i and result with 0
i=result=0
# Tkinter string variable 
# able to store any string value 
v = StringVar(master, "1")
que=StringVar()
op1=StringVar()
op2=StringVar()
op3=StringVar()
op4=StringVar()
#label for heading
Label(master, width="300", text="Python Quiz Application", bg="orange", fg="#000000",font='Helvetica 12 bold').pack()
#label for question
Label(master,text='',textvariable=que,bg="#04112d",fg="#ffffff",font='Helvetica 13 bold').place(x=90,y=35)
#radiobuttno for option1
Radiobutton(master,text='',variable=v,value="1",bg="#04112d").place(x=140,y=60)
#label for option1
Label(master,text="",textvariable=op1,bg="#04112d",fg="#ffffff",font='Helvetica 12').place(x=160,y=60)
#radiobuttno for option2
Radiobutton(master,text='',variable=v,value="2",bg="#04112d").place(x=140,y=80)
#label for option2
Label(master,text="",textvariable=op2,bg="#04112d",fg="#ffffff",font='Helvetica 12').place(x=160,y=80)
#radiobuttno for option3
Radiobutton(master,text='',variable=v,value="3",bg="#04112d").place(x=140,y=100)
#label for option3
Label(master,text="",textvariable=op3,bg="#04112d",fg="#ffffff",font='Helvetica 12').place(x=160,y=100)
#radiobuttno for option4
Radiobutton(master,text='',variable=v,value="4",bg="#04112d").place(x=140,y=120)
#label for option4
Label(master,text="",textvariable=op4,bg="#04112d",fg="#ffffff",font='Helvetica 12').place(x=160,y=120)
#setting first question in label
que.set("Q.1 "+QueList[0])
#passing first question's options in variable option
option=OptList[0]
#displaying options in labels
op1.set(option[0])
op2.set(option[1])
op3.set(option[2])
op4.set(option[3])
#incrementing i
i+=1
#defining function
def myFun():
    #declaring i and result as global variable
    global i
    global result
    #applying condition for question
    if i<=len(QueList):
     #applying condition for selected option
     if v.get() == OutList[i-1]:
         #incrementing the result counter
         result+=1
     # applying condition for last question
     if(i!=len(QueList)):
      #setting question in label
      que.set("Q."+str(i+1)+" "+(QueList[i]))
      option=OptList[i]
      #setting options in labels
      op1.set(option[0])
      op2.set(option[1])
      op3.set(option[2])
      op4.set(option[3])
     else:
         #displaying result
      messagebox.showinfo("Result","Total Question:"+str(len(QueList))+"\nCorrect:"+str(result)+"\nIncorrect:"+str(len(QueList)-result)+"\nResult:"+str((result/(len(QueList))*100))+"%")
    #incrementing i for question
    i += 1
#label for instruction
Label(master,text="Click on radio button to answer",font='Helvetica 11',bg="#04112d",fg="yellow").place(x=100,y=180)
#button for submit and next
Button(master,text="Next",command=myFun,bg="green",fg="white",width="10",font='Helvetica 12').place(x=160,y=200)
# Infinite loop can be terminated by 
# keyboard or mouse interrupt 
# or by any predefined function (destroy()) 
mainloop() 


When you will run this code in any Python editor then you will get output screen like below

Quiz app in python with source code

Output screen will display the first question from the list in which all the questions are stored. Here you have to select a radio button from the given options to answer. After selecting the radio button click the next button then you will get next question and so on.

when last question will display and you will click on next button after selecting radio button  the you will get result of your test and the result will contain the following details.

1.Total number of question
2.Correct output
3.Incorrect output
4.Percentage of result

For better understanding see the below screenshot.

Quiz app in python with source code


I hope you like my this post "Quiz app in python with source code"

If you want to Learn complete Python in a very simple language then visit the below link.


If you have any doubt regarding this post  or you want something more in this post then let me know by comment below i will work on it definitely.

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

If you like my post then share it with your friends. Thanks😊Happy Coding.

Post a Comment

0 Comments