bobhig

guiquiz

May 29th, 2021
695
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Add library packages
  2. from guizero import App, TextBox, PushButton, Picture, Text
  3. from random import randrange
  4.  
  5. # define variable questions - a list of questions
  6. questions = ['What Python function is equivalent to the Scratch "say" block ?',
  7.              'What Python module contains the randint function ?',
  8.              'Fill in the blank - pseudo____ ?']
  9.  
  10. # define the list of matching                                                                                                                                                           answers
  11. answers = ['print',
  12.            'random',
  13.            'code']
  14.  
  15. # function to start the quiz
  16. def start():
  17.     # pick a random question in the range determined by the number of questions in the list.                                                                                                                                                                                                                        
  18.     input_box.value = ""
  19.     question.index_value = randrange(len(questions))
  20.     question.value = questions[question.index_value]
  21. # change text on button to next after start
  22.     start.text = 'Next'
  23. # and show the check button having started
  24.     check_answer.show()
  25.  
  26.  
  27. # function to check answer in input box against answer in list
  28. # and set value to display
  29. def check():
  30.     if input_box.value == answers[question.index_value]:
  31.         question.value = 'Correct'
  32.     else:
  33.         question.value = 'Incorrect'
  34.     input_box.value = ""
  35.  
  36. # setup gui window
  37. app = App(title='Quiz', width=1000, height=500)
  38. # add a line of text to the gui
  39. question = Text(app, text='Ready to start the quiz?')
  40. # and a text box for the answers                          
  41. input_box = TextBox(app, text='Type answers here', width=20)
  42. # call the check function when the push button widget is pushed
  43. check_answer = PushButton(app, command = check, text='Check answer')
  44. # hide the check pushbutton until a question is asked
  45. check_answer.hide()
  46. # pick the next question using the start function when button is pushed
  47. start = PushButton(app, command=start, text='Start')
  48.  
  49. app.display()
RAW Paste Data