Advertisement
Faiakes

Guizero-box1-6

Jun 23rd, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.74 KB | None | 0 0
  1. ## Imports
  2. from guizero import App, TextBox, PushButton, Picture, Text, ButtonGroup
  3. from random import randrange
  4.  
  5. ## Create a list of questions
  6. questions_eng = ['What is 1+1?', 'What is 2+2?', "What is 3+3?", "What is 4+4?"]
  7. questions_spn = ['Que est 1+1?', 'Que est 2+2?', "Que est 3+3?", "Que est 4+4?"]
  8. answers = ["2", '4', '6', '8']
  9. images = ["0.jpg", "1.jpg", "2.jpg", "3.jpg" ]
  10.  
  11. ## Function to call at start of quiz
  12. def start():
  13.     question.index_value  = 0
  14.     #Switching between lists depending on language
  15.     if languages.value == "English":
  16.         question.value = questions_eng[question.index_value]
  17.     else:
  18.         question.value = questions_spn[question.index_value]
  19.     picture.value = images[question.index_value]
  20.     check_answer.show()
  21.     input_box.show()
  22.     choice.show()
  23.     picture.show()
  24.     #hiding butons that don't need to be visible
  25.     languages.hide()
  26.     start.hide()
  27.  
  28. ## Check if the answer in the box or the multiple choice is identical to the answer in the list
  29. def check():
  30.     if input_box.value == answers[question.index_value]:
  31.         question.value = 'Correct'
  32.         check_answer.bg="#5A7B5A"
  33.         next.show()
  34.     elif choice.value == answers[question.index_value]:
  35.         question.value = 'Correct'
  36.         check_answer.bg="#5A7B5A"
  37.         next.show()
  38.         check_answer.show()
  39.     else:
  40.         question.value = "Incorrect. Answer and check again."
  41.         check_answer.bg="red"
  42.         next.hide()
  43.  
  44. ## Function to progress through the list values
  45. def next():
  46.     if question.index_value  == len(questions_eng)-1:
  47.         question.value = "Thanks for playing!"
  48.         next.hide()
  49.         check_answer.hide()
  50.         choice.hide()
  51.         input_box.hide()
  52.         picture.value = "congats.png"
  53.     elif question.index_value  == len(questions_spn)-1:
  54.         question.value = "Thanks for playing!"
  55.         next.hide()
  56.         check_answer.hide()
  57.         choice.hide()
  58.         input_box.hide()
  59.         picture.value = "congats.png"
  60.         #start_again.show()
  61.         #return
  62.     elif languages.value == "English":
  63.         question.index_value  += 1
  64.         question.value = questions_eng[question.index_value]
  65.         picture.value = images[question.index_value]
  66.         check_answer.show()
  67.     else:
  68.         question.index_value  += 1
  69.         question.value = questions_spn[question.index_value]
  70.         picture.value = images[question.index_value]
  71.         check_answer.show()
  72.  
  73. ## Create and App
  74. app = App(title='Quiz - Answer in text or use the options.', width = 550, layout="grid",  bg = "#9932CC")
  75. ## Give option between 2 languages
  76. languages = ButtonGroup(app, options=["English", "Spanish"], horizontal=True, selected="English", grid=[0,0])
  77. ## Text for the question
  78. question = Text(app, text='Select a language & click on the image to start the quiz.', grid=[0,1])
  79. ## Box for written answers
  80. input_box = TextBox(app, text='', grid=[0,2])
  81. input_box.hide()
  82. ## Button to check the answer
  83. check_answer = PushButton(app, command = check, text='Check answer', grid=[0,3])
  84. ## Hide the button until we start
  85. check_answer.hide()
  86. ## Button to start the quiz, then used as Next from that point onwards
  87. start = PushButton(app, command=start, text='Start', grid=[0,4], image="start-test.jpg")
  88. ## Dispaly a start picture under the Start button
  89. picture = Picture(app, image="start-test.jpg", grid=[1,1])
  90. picture.hide()
  91. ## Next button to use the next function and increment the index
  92. next = PushButton(app, command=next, text='Next', grid=[0,6])
  93. next.hide()
  94. ## Multiple choice radio buttons, answer and 1 extra wrong answer (16)
  95. choice = ButtonGroup(app, options=["2", "4", "6", "8", "16"], horizontal=True, selected="16", command=check, grid=[0,7])
  96. choice.hide()
  97.  
  98.  
  99.  
  100. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement