Skip to content

Instantly share code, notes, and snippets.

@potaycat
Created October 31, 2018 15:51
Show Gist options
  • Save potaycat/acc0ea751cd6eb7d5858bcd69adf1f8d to your computer and use it in GitHub Desktop.
Save potaycat/acc0ea751cd6eb7d5858bcd69adf1f8d to your computer and use it in GitHub Desktop.
Protober Quiz
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from random import randint
import copy
import xmlHandler
class MainWindow:
def __init__(self, master):
self.master = master
self.frame = ttk.Frame(self.master)
self.button0 = ttk.Button(self.frame, text = 'PLAY!', width = 25, command = self.game)
self.button0.pack()
self.button1 = ttk.Button(self.frame, text = 'Quiz Studio', width = 25, command = self.new_window)
self.button1.pack()
self.frame.pack()
self.master.title("Quiz")
def new_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = QStudio(self.newWindow)
def game(self):
self.game = tk.Toplevel(self.master)
self.app = PlayTime(self.game, self.openQz())
def openQz(self):
openfile = filedialog.askopenfilename(title = "Select file",filetypes = (("XML Quiz files","*.xml"),("all files","*.*")))
return xmlHandler.read(openfile)
class PlayTime:
def __init__(self, master, quizzes):
self.master = master
self.master.title("Quiz Time!")
self.frame = ttk.Frame(self.master)
self.frame.pack()
self.w=tk.Canvas()
self.data = quizzes
self.score = 0
self.length = len(self.data)
self.load()
def load(self):
if len(self.data)>0:
for q in self.data:
self.spawn(q)
self.data.pop(q)
break
else:
self.gameOver()
def spawn(self, q):
a=self.data[q]
self.w.destroy()
self.w = tk.Canvas(self.master, width=200, height=100)
self.w.pack()
self.q = ttk.Label(self.w, text=q)
self.q.pack()
while len(a)>0:
ran = randint(0,len(a)-1)
if ran == 0:
self.b = ttk.Button(self.w, text = a[ran], command = self.correct)
else:
self.b = ttk.Button(self.w, text = a[ran], command = self.wrong)
self.b.pack()
a.pop(ran)
def correct(self):
self.score +=1
self.load()
def wrong(self):
self.load()
def gameOver(self):
final = str(self.score) + "/" + str(self.length)
self.w.destroy()
self.q = ttk.Label(self.master, text="Congrats! Your score is")
self.q.pack()
self.q = ttk.Label(self.master, text=final)
self.q.pack()
def close_windows(self):
self.master.destroy()
class QStudio:
def __init__(self, master):
self.master = master
self.master.title("Quiz Studio")
self.frame = ttk.Frame(self.master)
self.frame.pack()
self.exportbutton = ttk.Button(self.frame, text = 'EXPORT QUIZZES AS FILE', command = self.export)
self.exportbutton.pack()
self.addQ = ttk.Button(self.frame, text = 'Add Question', command = self.addQuestion)
self.addQ.pack()
self.remQ = ttk.Button(self.frame, text = 'Remove Question', command = self.remQuestion)
self.remQ.pack()
self.addA = ttk.Button(self.frame, text = 'Add Answer', command = self.addPseudoAns)
self.addA.pack()
self.remA = ttk.Button(self.frame, text = 'Remove Answer', command = self.remPseudoAns)
self.remA.pack()
self.updatebutton = ttk.Button(self.frame, text = 'Apply', command = self.apply)
self.updatebutton.pack()
self.data={
'Question here' : ["Correct answer","Wrong Answer 1","Wrong Answer 2","Wrong Answer 3"],
'What is the result of 1+4' : ["5","6","7","8"],
}
self.boxes=[]
self.currentQ = None
self.spawnDropList()
self.w=tk.Canvas()
def boxtext(self, new_value): #updates text
self.currentQ = new_value
self.l.config(text = self.data[new_value])
self.questionBoxes(new_value)
def close_windows(self):
self.master.destroy()
def questionBoxes(self, new_value):
self.boxes.clear()
self.w.destroy()
self.w = tk.Canvas(self.master, width=200, height=100)
self.w.pack()
self.e = ttk.Entry(self.w, width=60)
self.e.insert(0, self.currentQ)
self.e.pack()
for ans in self.data[new_value]:
self.b = ttk.Entry(self.w)
self.b.insert(0, ans)
self.b.pack()
self.boxes.append(self.b)
def addPseudoAns(self):
for q in self.data:
self.data[q].append("new answer")
self.questionBoxes(self.currentQ)
def remPseudoAns(self):
for q in self.data:
self.data[q].pop(-1)
self.questionBoxes(self.currentQ)
def addQuestion(self):
nq = "insert question here" + str(randint(0,99))
new = {nq:[]}
for value in self.data[self.currentQ]:
new[nq].append(value)
self.data.update(new)
self.dropListRefresh()
def remQuestion(self):
self.data.pop(self.currentQ)
self.dropListRefresh()
def spawnDropList(self):
var = tk.StringVar()
self.l = tk.OptionMenu(self.frame, var, *self.data, command=self.boxtext)
self.l.config(width=50)
self.l.pack()
def dropListRefresh(self):
self.l.destroy()
self.spawnDropList()
def apply(self):
self.data[self.currentQ].clear()
for ans in self.boxes:
self.data[self.currentQ].append(ans.get())
self.data[self.e.get()] = self.data.pop(self.currentQ)
self.dropListRefresh()
self.popup_ok()
def export(self):
xmlHandler.write(self.data)
self.popup_ok()
def popup_ok(self):
win = tk.Toplevel()
win.wm_title("Confirm")
l = ttk.Label(win, text="Done!")
l.pack()
b = ttk.Button(win, text="Okay", command=win.destroy)
b.pack()
def main():
root = tk.Tk()
app = MainWindow(root)
root.mainloop()
if __name__ == '__main__':
main()
<data name="Pokemon Move Knowledge!">
<question name="Which Pokemon below cannot use Thunderbolt?">
<trueAns>Rhydon</trueAns>
<fake>
<pseudoAns option="Pikachu"/>
<pseudoAns option="Emolga"/>
<pseudoAns option="Mareep"/>
</fake>
</question>
<question name="Which Pokemon below can use Hypnosis?">
<trueAns>Gardevoir</trueAns>
<fake>
<pseudoAns option="Xatu"/>
<pseudoAns option="Medicham"/>
<pseudoAns option="Latias"/>
</fake>
</question>
</data>
from lxml import etree
def read(xml):
qdict = {}
tree = etree.parse(xml)
root = tree.getroot()
for child in root.findall('question'):
name = child.get('name')
qdict[name]=[]
trueAns = child.find('trueAns').text
qdict[name]=[trueAns]
for todler in child.findall('fake'):
for fakeAns in todler.iter('pseudoAns'):
qdict[name].append(fakeAns.attrib['option'])
return qdict
def write(qdict):
root = etree.Element("data", name="hm")
for q in qdict:
questions = etree.SubElement(root, "question", name=q)
answer = etree.SubElement(questions, "trueAns").text = qdict[q][0]
fakes = etree.SubElement(questions, "fake")
for a in qdict[q]:
if a!=qdict[q][0] :
etree.SubElement(fakes, "pseudoAns", option=a)
tree = etree.ElementTree(root)
tree.write("output.xml", pretty_print=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment