Skip to content

Instantly share code, notes, and snippets.

@encela95dus
Created October 16, 2018 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save encela95dus/4e520ac2104b902b7a2394d4e7855fa3 to your computer and use it in GitHub Desktop.
Save encela95dus/4e520ac2104b902b7a2394d4e7855fa3 to your computer and use it in GitHub Desktop.
interactive_quiz.pyui
import ui
questions = [('Which is the smallest planet in the solar system?',
('mercury', 'mars', 'saturn'), 0),
('which one is the moon of the saturn?',
('io', 'enceladus', 'europa'), 1),
('Which is the largest planet in the solar system?',
('mercury', 'mars', 'jupiter'), 2)]
class QuizDialog(object):
def __init__(self, pyui_dialog, title,
question, choices, answer):
self.view = ui.load_view(pyui_dialog)
self.view.name = title
self.view['textview1'].text = question
datasource = ui.ListDataSource(choices)
datasource.action = self.table_tapped
tv = self.view['tableview1']
tv.data_source = tv.delegate = datasource
self.result = False
self.reference_answer = answer
def table_tapped(self, sender):
self.result = (sender.selected_row == self.reference_answer)
self.view.close()
def make_dialog(i, q):
question, choices, reference_answer = q
c = QuizDialog('quiz_dialog.pyui', 'Question '+str(i+1),
question, choices, reference_answer)
c.view.present('sheet')
c.view.wait_modal()
return c.result
def button_action(sender):
result = [make_dialog(i, q) for i, q in enumerate(questions)]
count = sum([1 if i else 0 for i in result])
sender.superview['label1'].text = f'{count} out of {len(result)} questions answered correctly'
v = ui.load_view()
v.present('sheet')
[
{
"class" : "View",
"attributes" : {
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"enabled" : true,
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"frame" : "{{0, 0}, {507, 471}}",
"selected" : false,
"nodes" : [
{
"class" : "Button",
"attributes" : {
"font_size" : 15,
"uuid" : "86E8849F-638D-456F-9306-E442698E84FD",
"name" : "button1",
"action" : "button_action",
"class" : "Button",
"frame" : "{{214, 220}, {80, 32}}",
"title" : "Start quiz"
},
"frame" : "{{158, 140}, {183, 58}}",
"selected" : true,
"nodes" : [
]
},
{
"class" : "Label",
"attributes" : {
"font_size" : 18,
"text" : "",
"font_name" : "<System>",
"name" : "label1",
"class" : "Label",
"alignment" : "center",
"frame" : "{{179, 220}, {150, 32}}",
"uuid" : "9970BFC7-EE59-4703-AA1B-98C01B8602CA"
},
"frame" : "{{66, 319}, {383, 59}}",
"selected" : false,
"nodes" : [
]
}
]
}
]
[
{
"class" : "View",
"attributes" : {
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"enabled" : true,
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"frame" : "{{0, 0}, {517, 417}}",
"selected" : false,
"nodes" : [
{
"class" : "TextView",
"attributes" : {
"font_size" : 17,
"font_name" : "<System>",
"flex" : "WH",
"autocorrection_type" : "default",
"name" : "textview1",
"uuid" : "74902482-63C1-4CDF-81A5-314F11E08D5D",
"class" : "TextView",
"editable" : false,
"alignment" : "left",
"frame" : "{{158, 130}, {200, 200}}",
"spellchecking_type" : "default"
},
"frame" : "{{30, 34}, {461, 116}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "TableView",
"attributes" : {
"data_source_font_size" : 18,
"data_source_number_of_lines" : 1,
"flex" : "WH",
"name" : "tableview1",
"row_height" : 44,
"data_source_delete_enabled" : true,
"class" : "TableView",
"data_source_items" : "Row 1\nRow 2\nRow 3",
"frame" : "{{158, 130}, {200, 200}}",
"background_color" : "RGBA(1.0, 1.0, 1.0, 1.0)",
"uuid" : "FB7469B8-69BB-4D44-920C-4A4BF4929F1A"
},
"frame" : "{{30, 229}, {460, 137}}",
"selected" : false,
"nodes" : [
]
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment