I am trying to write a multiple choice quiz using Python Tkinter. I have a 2 part question. I have radio buttons that display the choices and collect the selected option. I also have a created a button to navigate to the next question or back to the previous question as well as another button to view the score.
Part 1 - How do I keep the selected option of the radio button present for each question when navigation backwards/forwards through the quiz?
Part 2 - The way I have thought out how the view score button should work is:
- Compare each collected option (saved in a
list
?) to the correct answer - Calculate score
- Display
Points 2 and 3 are the easiest part for me. Can you indicate to me the right direction to go on Point number one?- Compare each collected option (saved in a
from tkinter import messagebox
import tkinter as tk
from tkinter import *
# question list
q = [
"question 1", "question 2", "question 3", "question 4"
]
# options list
options = [
["a","b","c","d"],
["b","c","d","a"],
["c","d","a","b"],
["d","a","b","c"],
]
# correct answers list
a = [3,4,1,2]
class Quiz:
def __init__(self, master):
self.opt_selected = IntVar()
self.qn = 0
self.correct = 0
self.ques = self.create_q(master, self.qn)
self.opts = self.create_options(master, 4)
self.display_q(self.qn)
self.button = Button(master, text="Previous Question", command=self.back_btn,
width=16, borderwidth=3, relief=RAISED)
self.button.pack(side=LEFT)
self.button = Button(master, text="Next Question", command=self.next_btn,
width=16, borderwidth=3, relief=RAISED)
self.button.pack(side=LEFT)
self.button = Button(master, text="View Score", command=self.score_viewer,
width=16, borderwidth=3, relief=RAISED)
self.button.pack(side=LEFT)
# define questions
def create_q(self, master, qn):
w = Label(master, text=q[qn],
anchor='w',
wraplength=400, justify=LEFT)
w.pack(anchor='w')
return w
# define multiple options
def create_options(self, master, n):
b_val = 0
b = []
while b_val < n:
btn = Radiobutton(master, text="foo", variable=self.opt_selected, value=b_val+1)
b.append(btn)
btn.pack(side=TOP, anchor="w")
b_val = b_val + 1
return b
# define questions for display when clicking on the NEXT Question Button
def display_q(self, qn):
b_val = 0
self.opt_selected.set(0)
self.ques['text'] = q[qn]
for op in options[qn]:
self.opts[b_val]['text'] = op
b_val = b_val + 1
# define questions for display when clicking on the PREVIOUS Question Button
def display_prev_q(self, qn):
b_val = 0
self.opt_selected.set(0)
self.ques['text'] = q[qn]
for op in options[qn]:
self.opts[b_val]['text'] = op
b_val = b_val + 1
# check option selected against correct answer list
def check_q(self, qn):
if self.opt_selected.get() == a[qn]:
self.correct += 1
else:
self.correct += 0
# print results
def print_results(self):
print("Score: ", self.correct, "/", len(q))
# define PREVIOUS button
def back_btn(self):
self.qn = self.qn - 1
self.display_prev_q(self.qn)
# define NEXT button
def next_btn(self):
# if self.check_q(self.qn):
# print("Correct")
# self.correct += 1
self.qn = self.qn + 1
self.display_prev_q(self.qn)
# if self.qn >= len(q):
# self.print_results()
# else:
# self.display_q(self.qn)
# define SCORE view button and score results
def score_viewer(self):
score_viewer = messagebox.askquestion("Warning", 'Would you like to view your current score?', icon='warning')
if score_viewer == 'yes':
self.check_q(self.qn)
corr_ans = self.correct
total_quest = len(q)
output = '{:.1%}'.format(self.correct / len(q))
score_text = "\nScore: %s " % output
output_text = "Correctly answered %a out of %d questions. %s" % (corr_ans, total_quest, score_text)
messagebox.showinfo("Score", output_text)
else:
tk.messagebox.showinfo('Return', 'Returning to quiz')