Skip to content

Commit

Permalink
Quiz Demo #396
Browse files Browse the repository at this point in the history
  • Loading branch information
jarvisteach committed Apr 10, 2018
1 parent 3b50924 commit 18d9f43
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/quiz/questions.txt
@@ -0,0 +1,3 @@
What is my favourite colour?, red, yellow, green, blue, red
What year is it?, 2000, 2018, 2017, 2020, 2018
What month is it?, October, February, April, May, April
9 changes: 9 additions & 0 deletions examples/quiz/quiz_1.py
@@ -0,0 +1,9 @@
import sys
sys.path.append("../../")
from appJar import gui

def launcher(btn):
print(btn)

with gui("Quiz Demo") as app:
app.buttons(["Quiz", "Scores"], launcher)
15 changes: 15 additions & 0 deletions examples/quiz/quiz_2.py
@@ -0,0 +1,15 @@
import sys
sys.path.append("../../")
from appJar import gui

def launcher(btn):
app.showSubWindow(btn)

with gui("Quiz Demo") as app:
app.buttons(["Quiz", "Scores"], launcher)

with app.subWindow("Quiz", modal=True):
app.label("Quiz")

with app.subWindow("Scores", modal=True):
app.label("Scores")
36 changes: 36 additions & 0 deletions examples/quiz/quiz_3.py
@@ -0,0 +1,36 @@
import sys
sys.path.append("../../")
from appJar import gui

# make the question array
questions = []
with open("questions.txt") as f:
for line in f:
parts = line.split(",")
q = {}
q["question"] = parts[0]
q["options"] = parts[1:-1]
q["answer"] = parts[-1].strip()
questions.append(q)

def launcher(btn):
app.showSubWindow(btn)

def check():
app.nextFrame("Questions")

with gui("Quiz Demo") as app:
app.buttons(["Quiz", "Scores"], launcher)

with app.subWindow("Quiz", modal=True):
with app.frameStack("Questions", start=0):
for pos, q in enumerate(questions):
with app.frame():
with app.labelFrame("Question " + str(pos + 1)):
app.label(q["question"])
for o in q["options"]:
app.radio(str(pos), o)
app.button("SUBMIT", check)

with app.subWindow("Scores", modal=True):
app.label("Scores")
45 changes: 45 additions & 0 deletions examples/quiz/quiz_4.py
@@ -0,0 +1,45 @@
import sys
sys.path.append("../../")
from appJar import gui

# make the question array
questions = []
with open("questions.txt") as f:
for line in f:
parts = line.split(",")
q = {}
q["question"] = parts[0]
q["options"] = parts[1:-1]
q["answer"] = parts[-1].strip()
questions.append(q)

def launcher(btn):
app.showSubWindow(btn)

def check():
qNum = app.getCurrentFrame("Questions")
choice = app.radio(str(qNum))
answer = questions[qNum]["answer"]

if choice == answer:
app.infoBox("Correct", "You got it right")
else:
app.errorBox("Wrong", "You got it wrong")

app.nextFrame("Questions")

with gui("Quiz Demo") as app:
app.buttons(["Quiz", "Scores"], launcher)

with app.subWindow("Quiz", modal=True):
with app.frameStack("Questions", start=0):
for pos, q in enumerate(questions):
with app.frame():
with app.labelFrame("Question " + str(pos + 1)):
app.label(q["question"])
for o in q["options"]:
app.radio(str(pos), o)
app.button("SUBMIT", check)

with app.subWindow("Scores", modal=True):
app.label("Scores")
54 changes: 54 additions & 0 deletions examples/quiz/quiz_5.py
@@ -0,0 +1,54 @@
import sys
sys.path.append("../../")
from appJar import gui

# make the question array
questions = []
with open("questions.txt") as f:
for line in f:
parts = line.split(",")
q = {}
q["question"] = parts[0]
q["options"] = parts[1:-1]
q["answer"] = parts[-1].strip()
questions.append(q)

score = 0

def showScore():
app.label("score", "Score: " + str(score) + "/" + str(len(questions)))

def launcher(btn):
app.showSubWindow(btn)

def check():
global score
qNum = app.getCurrentFrame("Questions")
choice = app.radio(str(qNum))
answer = questions[qNum]["answer"]

if choice.strip() == answer.strip():
app.infoBox("Correct", "You got it right")
score += 1
showScore()
else:
app.errorBox("Wrong", "You got it wrong")

app.nextFrame("Questions")

with gui("Quiz Demo") as app:
app.buttons(["Quiz", "Scores"], launcher)

with app.subWindow("Quiz", modal=True):
with app.frameStack("Questions", start=0):
for pos, q in enumerate(questions):
with app.frame():
with app.labelFrame("Question " + str(pos + 1)):
app.label(q["question"])
for o in q["options"]:
app.radio(str(pos), o)
showScore()
app.button("SUBMIT", check)

with app.subWindow("Scores", modal=True):
app.label("Scores")

0 comments on commit 18d9f43

Please sign in to comment.