In: Computer Science
Hi,
I'm trying to make a basic GUI quiz in python using tkinter.
I would like the user to have to press a start button to begin the quiz and then be asked 5 different questions with the total score showing at the end. Ideally it would be a multiple choice quiz with the user having 4 different answers to choose from.
A very basic example of this would be appreciated so I can understand where exactly I'm going wrong.
Thanks in advance
#source code:
from tkinter import *
import random
q=["days in a year",
"kind of apple",
"How many days in a week",
"What is the Capital of AndhraPradesh",
"who is the Present C.M of AndhraPrasad"
]
options=[
["365","368","410","362"],
["heat","non-veg","vegatable","fruit"],
[3,8,7,9],
["Hyderabad","Rajahmundry","Amaravathi","Guntur"],
["Pawan Kalyan","ChandraBabu Naidu","K.A
Paul","Jagan"]
]
a=[1,4,3,3,4]
rand_show=random.sample(range(5),5)
class Quiz:
def __init__(self,master):
self.opt_selected=IntVar()
self.qn=0
self.r=rand_show[self.qn]
self.correct=0
self.ques=self.create_q(master,self.qn,self.r)
self.opts=self.create_options(master,4)
self.display_q(self.qn,self.r)
self.button=Button(master,text="Submit",command=self.next_btn)
self.button.pack(side=BOTTOM)
def create_q(self,master,qn,r):
w=Label(master,text=q[r])
w.pack(side=TOP)
return w
def create_options(self,master,n):
b_val=0
b=[]
while b_val<n:
btn=Radiobutton(master,variable=self.opt_selected,value=b_val+1)
b.append(btn)
btn.pack(side=TOP,anchor="w")
b_val=b_val+1
return b
def display_q(self,qn,r):
b_val=0
temp=qn
self.opt_selected.set(0)
self.ques['text']=str(temp+1)+"."+q[r]
for op in options[r]:
self.opts[b_val]['text']=op
b_val=b_val+1
def check_q(self,r):
if
self.opt_selected.get()==a[self.r]:
return
True
return False
def print_results(self):
root.destroy()
king=Tk()
king.geometry("500x300")
Label(king, text="YOUR WEEKEND
RESULTS:").pack()
Label(king, text="").pack()
score="Marks:
"+str(self.correct)+"/"+str(len(q))
Label(king,
text=score,fg="green").pack()
marks=0
id=""
print(marks)
def next_btn(self):
if self.check_q(self.r):
print("correct")
self.correct+=1
self.qn=self.qn+1
if(self.qn==5):
pass
else:
self.r=rand_show[self.qn]
if self.qn>=len(q):
self.print_results()
else:
self.display_q(self.qn,self.r)
root=Tk()
root.geometry("500x300")
app=Quiz(root)
root.mainloop()
#output:
#if you have any doubts comment below....