class Controller: #Constructor to create the window and its features. def __init__(self): #Create a window and give it a title. self.window = Tk() self.window.title('Simple Quiz') #Create an instance of the quiz class. self.quiz = Quiz() #Labels for the question to appear on the screen. self.question_text = Text(self.window, font="arial 16", width = 40, height = 4, wrap = WORD) self.question_text.insert(1.0, self.quiz.ask_current_question()) self.question_text.pack() #Variable and entry for the user to put in his or her answer. self.answer = StringVar() self.answerEntry = Entry (self.window, textvariable = self.answer) self.answerEntry.pack(side = LEFT) #Bind the return key to enter the answer in the entry. self.answerEntry.focus_set() self.answerEntry.bind("<Return>", self.check_answer) #Label for the instructions for the quiz. self.instructions = StringVar() self.instructions.set('\u21D0 Enter your answer here') self.instrLabel = Label(self.window, textvariable = self.instructions) self.instrLabel.pack(side = LEFT) #Main loop to loop through the initialization method. self.window.mainloop() #Method that checks if the user was correct or incorrect. def check_answer(self, event): if self.quiz.check_current_answer(self.answer.get()): #Got it right!! self.instructions.set("Good job! Next question ...") else: self.instructions.set("Sorry, the answer was " + self.quiz.get_current_answer()) self.answer.set('') #Go to the next question if it exists self.question_text.delete(1.0, END) if (self.quiz.has_next()): self.quiz.next() self.question_text.insert(1.0, self.quiz.ask_current_question()) else: self.question_text.insert(1.0, 'Sorry, there are no more questions.') self.answerEntry.configure(state='disabled')
class Controller: def __init__(self): self.window = Tk() self.window.title('Simple Quiz') self.quiz = Quiz() #setting the text box self.question_text = Text(self.window, font="arial 16", width=40, height=4, wrap=WORD) self.question_text.insert(1.0, self.quiz.ask_current_question()) self.question_text.pack() #making the text entry and binding the 'enter' button to it self.answer = StringVar() self.answerEntry = Entry(self.window, textvariable=self.answer) self.answerEntry.pack(side=LEFT) self.answerEntry.focus_set() self.answerEntry.bind("<Return>", self.check_answer) #label self.instructions = StringVar() self.instructions.set('\u21D0 Enter your answer here') self.instrLabel = Label(self.window, textvariable=self.instructions) self.instrLabel.pack(side=LEFT) self.window.mainloop() def check_answer(self, event): if self.quiz.check_current_answer(self.answer.get()): #Got it right!! self.instructions.set("Good job! Next question ...") else: self.instructions.set("Sorry, the answer was " + self.quiz.get_current_answer()) self.answer.set('') #Go to the next question if it exists self.question_text.delete(1.0, END) if (self.quiz.has_next()): self.quiz.next() self.question_text.insert(1.0, self.quiz.ask_current_question()) else: self.question_text.insert( 1.0, 'Sorry, there are no more questions.' ) #when there are no more questions to be asked self.answerEntry.configure(state='disabled')
class Controller: ''' Drive an interactive quiz GUI ''' def __init__(self, window): ''' Create a quiz and GUI frontend for that quiz ''' self.quiz = Quiz() self.question_text = Text(window, font="arial 16", width=40, height=4, wrap=WORD) self.question_text.insert(1.0, self.quiz.ask_current_question()) self.question_text.pack() self.answer = StringVar() self.answerEntry = Entry(window, textvariable=self.answer) self.answerEntry.pack(side=LEFT) self.answerEntry.focus_set() self.answerEntry.bind("<Return>", self.check_answer) self.instructions = StringVar() self.instructions.set('\u21D0 Enter your answer here') self.instrLabel = Label(window, textvariable=self.instructions) self.instrLabel.pack(side=LEFT) def check_answer(self, event): ''' Check if the user's current answer is correct ''' if self.quiz.check_current_answer(self.answer.get()): #Got it right!! self.instructions.set("Good job! Next question ...") else: self.instructions.set("Sorry, the answer was " + self.quiz.get_current_answer()) self.answer.set('') #Go to the next question if it exists self.question_text.delete(1.0, END) if (self.quiz.has_next()): self.quiz.next() self.question_text.insert(1.0, self.quiz.ask_current_question()) else: self.question_text.insert(1.0, 'Sorry, there are no more questions.') self.answerEntry.configure(state='disabled')