|
| 1 | +#!/usr/bin/env python |
| 2 | +# Written by: DGC |
| 3 | + |
| 4 | +from Tkinter import * |
| 5 | + |
| 6 | +#============================================================================== |
| 7 | +class Model(object): |
| 8 | + |
| 9 | + def __init__(self): |
| 10 | + # q_and_a is a dictionary where the key is a question and the entry is |
| 11 | + # a list of pairs, these pairs are an answer and whether it is correct |
| 12 | + self.q_and_a = dict() |
| 13 | + |
| 14 | + self.q_and_a["Is the Earth a sphere?"] = [("Yes", True), ("No", False)] |
| 15 | + |
| 16 | + def question_and_answer(self): |
| 17 | + """ Returns a randomly chosen question and answer as a pair. """ |
| 18 | + key = "Is the Earth a sphere?" |
| 19 | + return (key, self.q_and_a[key]) |
| 20 | + |
| 21 | +#============================================================================== |
| 22 | +class View(object): |
| 23 | + |
| 24 | + def __init__(self): |
| 25 | + self.parent = Tk() |
| 26 | + self.controller = None |
| 27 | + |
| 28 | + def new_question(self, question, answers): |
| 29 | + """ |
| 30 | + question is a string, answers is a list of pairs. each pair is a |
| 31 | + possible answer then a boolean of if it's correct. |
| 32 | + e.g |
| 33 | + view.new_question( |
| 34 | + "Is the earth a sphere?", |
| 35 | + [("yes",True), ("no",False)] |
| 36 | + ) |
| 37 | + """ |
| 38 | + |
| 39 | + # put the question on as a label |
| 40 | + question_label = Label(self.parent, text=question) |
| 41 | + question_label.pack() |
| 42 | + |
| 43 | + # put the answers on as a radio buttons |
| 44 | + var = BooleanVar() |
| 45 | + var.set(False) # initialize |
| 46 | + |
| 47 | + for answer, correct in answers: |
| 48 | + option = Radiobutton( |
| 49 | + self.parent, |
| 50 | + text=answer, |
| 51 | + variable=var, |
| 52 | + value=correct |
| 53 | + ) |
| 54 | + option.pack() |
| 55 | + |
| 56 | + |
| 57 | + def main_loop(self): |
| 58 | + mainloop() |
| 59 | + |
| 60 | + def register(self, controller): |
| 61 | + """ Register a controller to give callbacks to. """ |
| 62 | + self.controller = controller |
| 63 | + |
| 64 | +#============================================================================== |
| 65 | +class Controller(object): |
| 66 | + |
| 67 | + def __init__(self, model, view): |
| 68 | + self.model = model |
| 69 | + self.view = view |
| 70 | + |
| 71 | + self.view.register(self) |
| 72 | + q_and_a = self.model.question_and_answer(); |
| 73 | + self.view.new_question(q_and_a[0], q_and_a[1]) |
| 74 | + |
| 75 | + def answer(self): |
| 76 | + pass |
| 77 | + |
| 78 | +#============================================================================== |
| 79 | +if (__name__ == "__main__"): |
| 80 | + # Note: The view should not send to the model but it is often useful |
| 81 | + # for the view to receive update event information from the model. |
| 82 | + # However you should not update the model from the view. |
| 83 | + |
| 84 | + view = View() |
| 85 | + model = Model() |
| 86 | + controller = Controller(model, view) |
| 87 | + |
| 88 | + view.main_loop() |
| 89 | + |
0 commit comments