Quiz Application Using Python With Source Code

Introduction:

In this project, we have built a Quiz Application with the help of the  tkinter module in Python. In this, users will be provided with a GUI in order to select one of the correct options. As the user will choose the correct option the point will increase. Let’s look forward to the steps in detail to make this project.

Explanation:

The first step is to import all the necessary libraries. For the latest version, the tkinter module comes under the future module. So firstly we have to install the future module with the help of the command “pip install <module-name>”. The future module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions. With “future. moves” we will import the tkinter module.

In the next step, to create an object of the tkinter frame will use “.Tk()” and with this, we will also set the title of our tkinter application. Now, we will create the Label widget stating “QUIZ” with the help of “tkinter. Label()” and by using the “.place()” function we will set the position for the label widget.

For getting the user name we will again create one more label widget with the same process and also with this we will create an entry widget to provide space for the user to enter the details with the help of “tkinter. Entry()” and with the help of “.place()” we will set the x and y coordinates accordingly. In addition to this, we will create an Enter Button with the help of “tkinter. Button()”.

 

Before discussing the whole part, for a moment let’s discuss one of the most important parts of this project. To shift from one question frame to another frame we will need a callback function. In tkinter, some widgets allow us to associate a callback function within an event using the command binding which means we can pass the name of the function to the command of the widget of the function so that whenever the event occurs on the widget, the function will be called automatically.

While creating the Button widget named “Enter”, with the help of the command we will call the UDF “info1()” function. The function contains the information about the first question. So as the event of the button widget will occur it will call the info1(). Now, let’s discuss the block of code under info1().

In info1(), first of all, we will store the information of the entry widget under one variable by “.get()”. Now we will create two label widgets with the help of “tkinter. Label()”. One for the Question heading and the other one stating the question. Under the text parameter we will pass the particular statements and again with the help of “.place()”  we will set the positions. For providing the options to the user we will create 4 button widgets stating the possible answers for that particular question by using “.tkinter.Button()”. While creating the button widgets we will pass another UDF “info2()” under command stating another question. So that, whether the user clicks right or wrong answers the next question should get displayed. But, whenever the user clicks the right option then for that particular button widget we will pass 2 functions in the command by “lambda:[fun1(), fun2()]”. This will help us to move to another question and increase a point as well. For increment, we have created a function named “increase()” under which we are adding the point by 1.

While calling these functions we will also make sure to destroy the previously created widgets and with the help of “.destroy()” we will do so.

Source Code:

				
					# Importing the library
import tkinter

# Window
root = tkinter.Tk()
root.title("QUIZ")
root.geometry('700x900')
c = 0
label_t = tkinter.Label(root, text="QUIZ", font=("arial", 50))
label_t.place(x=600, y=50)

# Label widget for user
label_u = tkinter.Label(root, text="Username", font=("arial", 30))
label_u.place(x=250, y=250)

# Entry widget
entry = tkinter.Entry(root, font=("arial", 30))
entry.place(x=500, y=250)


# Function to increase the point
def increase():
    global c
    c += 1


# Question 1
def info1():
    global c
    name = entry.get()
    label_q1 = tkinter.Label(root, text="Question 1", font=("Arial", 35))
    label_q1.place(x=550, y=50)
    label1 = tkinter.Label(root, text="Which of them is a Keyword in Python?", font=("Arial", 30))
    label1.place(x=300, y=100)

    # Button widget for options
    op1 = tkinter.Button(root, text="range", font=("Arial", 20), command=info2)
    op1.place(x=400, y=200)
    op2 = tkinter.Button(root, text="def", font=("Arial", 20), command=lambda: [info2(), increase()])
    op2.place(x=800, y=200)
    op3 = tkinter.Button(root, text="Val", font=("Arial", 20), command=info2)
    op3.place(x=400, y=300)
    op4 = tkinter.Button(root, text="to", font=("Arial", 20), command=info2)
    op4.place(x=800, y=300)

    label_t.destroy()
    label_u.destroy()
    entry.destroy()
    button.destroy()


# Question 2
def info2():
    global c, label_q1, label1, op1, op2, op3, op4
    c += 1
    label_q2 = tkinter.Label(root, text="Question 2", font=("Arial", 35))
    label_q2.place(x=550, y=50)
    label2 = tkinter.Label(root, text="Which of the following is built-in function in Python?", font=("Arial", 30))
    label2.place(x=300, y=100)

    # Button widget for options
    opp1 = tkinter.Button(root, text="factorial()", font=("Arial", 20), command=info3)
    opp1.place(x=400, y=200)
    opp2 = tkinter.Button(root, text="print()", font=("Arial", 20), command=lambda: [info3(), increase()])
    opp2.place(x=800, y=200)
    opp3 = tkinter.Button(root, text="seed()", font=("Arial", 20), command=info3)
    opp3.place(x=400, y=300)
    opp4 = tkinter.Button(root, text="sqrt()", font=("Arial", 20), command=info3)
    opp4.place(x=800, y=300)

    label_q1.destroy()
    label1.destroy()
    op1.destroy()
    op2.destroy()
    op3.destroy()
    op4.destroy()


# Question 3
def info3():
    global c, opp1, opp2, opp3, opp4, label_q2, label2
    c += 1
    label_q3 = tkinter.Label(root, text="Question 3", font=("Arial", 35))
    label_q3.place(x=550, y=50)
    label3 = tkinter.Label(root, text="Which of the following is not the core datatype in Python?",
                           font=("Arial", 30))
    label3.place(x=200, y=100)

    # Button widgets for options
    oppp1 = tkinter.Button(root, text="Tuple", font=("Arial", 20), width=8, command=info4)
    oppp1.place(x=400, y=200)
    oppp2 = tkinter.Button(root, text="Dictionary", font=("Arial", 20), width=7, command=info4)
    oppp2.place(x=800, y=200)
    oppp3 = tkinter.Button(root, text="Lists", font=("Arial", 20), width=7, command=info4)
    oppp3.place(x=400, y=300)
    oppp4 = tkinter.Button(root, text="Class", font=("Arial", 20), command=lambda: [info4(), increase()])
    oppp4.place(x=800, y=300)
    opp1.destroy()
    opp2.destroy()
    opp3.destroy()
    opp4.destroy()
    label_q2.destroy()
    label2.destroy()


# Question 4
def info4():
    global c, oppp1, oppp2, oppp3, oppp4, label_q3, label3
    c += 1
    label_q4 = tkinter.Label(root, text="Question 4", font=("Arial", 38))
    label_q4.place(x=550, y=50)
    label4 = tkinter.Label(root, text="Who developed python programming language?", font=("Arial", 35))
    label4.place(x=200, y=100)

    # Button widget for options
    opppp1 = tkinter.Button(root, text="Wick Van Rossum", font=("Arial", 20), command=info5)
    opppp1.place(x=400, y=200)
    opppp2 = tkinter.Button(root, text="Rasmus Lerdorf", font=("Arial", 20), command=info5)
    opppp2.place(x=800, y=200)
    opppp3 = tkinter.Button(root, text="Guido Van Rossum", font=("Arial", 20), command=lambda: [info5(), increase()])
    opppp3.place(x=400, y=300)
    opppp4 = tkinter.Button(root, text="Niene Stom", font=("Arial", 20), command=info5)
    opppp4.place(x=800, y=300)

    label_q3.destroy()
    label3.destroy()
    oppp1.destroy()
    oppp2.destroy()
    oppp3.destroy()
    oppp4.destroy()


# Question 5
def info5():
    global c, opppp1, opppp2, opppp3, opppp4, label_q4, label4
    c += 1
    label_q5 = tkinter.Label(root, text="Question 5", font=("Arial", 38))
    label_q5.place(x=550, y=50)
    label5 = tkinter.Label(root, text="Which of the following is the extension for Python File?",
                           font=("Arial", 35))
    label5.place(x=150, y=100)

    # Button widget for options
    oppppp1 = tkinter.Button(root, text=".python", font=("Arial", 20), width=15, command=info6)
    oppppp1.place(x=400, y=200)
    oppppp2 = tkinter.Button(root, text=".p", font=("Arial", 20), width=13, command=info6)
    oppppp2.place(x=800, y=200)
    oppppp3 = tkinter.Button(root, text=".pl", font=("Arial", 20), width=16, command=info6)
    oppppp3.place(x=400, y=300)
    oppppp4 = tkinter.Button(root, text=".py", font=("Arial", 20), width=10, command=lambda: [info6(), increase()])
    oppppp4.place(x=800, y=300)

    label_q4.destroy()
    label4.destroy()
    opppp1.destroy()
    opppp2.destroy()
    opppp3.destroy()
    opppp4.destroy()


# Displaying the score
def info6():
    global c, name, oppppp1, oppppp2, oppppp3, oppppp4, label_q5, label5
    label_q6 = tkinter.Label(root, text="SCORE", font=("Arial", 38), width=10)
    label_q6.place(x=550, y=50)
    label6 = tkinter.Label(root, text=name, font=("Arial", 35))
    label6.place(x=150, y=200)
    labell6 = tkinter.Label(root, text=c, font=("Arial", 30))
    labell6.place(x=400, y=200)

    label_q5.destroy()
    label5.destroy()
    oppppp1.destroy()
    oppppp2.destroy()
    oppppp3.destroy()
    oppppp4.destroy()


button = tkinter.Button(root, text="ENTER", font=("arial", 30), command=info1)
button.place(x=600, y=350)
root.mainloop()
				
			

By following the same process we will create “info2()” and within the buttons widgets for this function, we will call “info3()”. The chain goes similarly. While creating button widgets for “info3()” we will call “info4()” and similarly “info5()” will be called. Within every function, we will destroy the previously created widgets.

The last UDF is “info6()”, this is to display the scores. Under this, we will create 3 Label widgets. The first one is for displaying the heading “SCORE”, the second one is for displaying the score and the third one is to display the count.

To execute all these functions successfully, we will run a tkinter event loop by using “.mainloop()”.  According to your need, you can add up many more questions and build up a Quiz Application by using the tkinter module in python.

Output:

quize Using Python 2
quize Using Python 1
quize Using Python 3

Find More Projects

Build a Quiz Game Using HTML CSS and JavaScript Introduction Hello coders, you might have played various games, but were you aware …

Emoji Catcher Game Using HTML CSS and JavaScript Introduction Hello Coders, Welcome to another new blog. In this article we’ve made a …

Typing Challenge Using HTML CSS and JavaScript Introduction Hello friends, all you developer friends are welcome to our new project. If you …

Breakout Game Using HTML CSS and JavaScript With Source Code Introduction Hello friends, welcome to today’s new blog post. All of you …

Digital and Analog Clock using HTML CSS and JavaScript Introduction : This project is a digital clock and stopwatch system, which allows …

Coffee Shop Website using HTML, CSS & JavaScript Introduction : This project is a website for coffee house business. It uses HTML …

Get Huge Discounts
More Python Projects

All Coding Handwritten Notes