Advertisement
Faiakes

Guizero-box1-5

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