1

This is the code for the function I'm using to start the main part of the program, however I want some sort of loop or something which creates ten questions, but waits for an input from the Entry box before moving onto the next question. Any ideas?

def StartGame():
    root = Tk()
    root.title("Maths Quiz - Trigonometry and Pythagoras' Theorem | Start The Game")
    root.geometry("640x480")
    root.configure(background = "gray92")
    global AnswerEntry
    TotScore = 0
    Count = 0
    AnswerReply = None
    WorkingArea = Text(root, width = 70, height = 10, wrap = WORD).place(x = 38, y = 100)
    n = GetRandomNumber()
    Angle,Opposite,Adjacent,Hypotenuse = Triangle()
    Question,RealAnswer = QuestionLibrary(Opposite,Adjacent,Hypotenuse,Angle,n)
    AskQuestion = Label(root, text = Question, wraplength = 560).place(x = 48, y = 300)
    PauseButton = ttk.Button(root, text = "Pause").place(x = 380, y = 10)
    HelpButton = ttk.Button(root, text = "Help", command = helpbutton_click).place(x = 460, y = 10)
    QuitButton = ttk.Button(root, text = "Quit", command = root.destroy).place(x = 540, y = 10)
    AnswerEntry = Entry(root)
    AnswerEntry.place(x = 252, y = 375)
    SubmitButton = ttk.Button(root, text = "Submit", command = submit_answer).place(x = 276, y = 400)
    TotScore,AnswerReply = IsAnswerCorrect(Answer,RealAnswer)
    ScoreLabel = ttk.Label(root, text = TotScore).place(x = 38, y = 10)
    AnswerReplyLabel = ttk.Label(root, text = AnswerReply).place(x = 295, y = 440)
    root.mainloop()

I want the loop to start after the AnswerReply = None

mmgp
  • 18,901
  • 3
  • 53
  • 80
Annie Royal
  • 23
  • 1
  • 2
  • 5

2 Answers2

2

You don't want a loop. The only really important loop inside a GUI should be the mainloop(), handling signal and executing callbacks.

Example:

try:
    import Tkinter as Tk
except ImportError:
    import tkinter as Tk

class QAGame(Tk.Tk):
    def __init__(self, questions, answers, *args, **kwargs):
        Tk.Tk.__init__(self, *args, **kwargs)
        self.title("Questions and answers game")
        self._setup_gui()
        self._questions = questions[:]
        self._answers = answers
        self._show_next_question()

    def _setup_gui(self):
        self._label_value = Tk.StringVar()
        self._label = Tk.Label(textvariable=self._label_value)
        self._label.pack()
        self._entry_value = Tk.StringVar()
        self._entry = Tk.Entry(textvariable=self._entry_value)
        self._entry.pack()
        self._button = Tk.Button(text="Next", command=self._move_next)
        self._button.pack()

    def _show_next_question(self):
        q = self._questions.pop(0)
        self._label_value.set(str(q))

    def _move_next(self):        
        self._read_answer()
        if len(self._questions) > 0:
            self._show_next_question()
            self._entry_value.set("")
        else:
            self.quit()
            self.destroy()

    def _read_answer(self):
        answer = self._entry_value.get()
        self._answers.append(answer)

    def _button_classification_callback(self, args, class_idx):
        self._classification_callback(args, self._classes[class_idx])
        self.classify_next_plot()

if __name__ == "__main__":
    questions = ["How old are you?",
             "What is your name?"]
    answers = []
    root = QAGame(questions, answers)
    root.mainloop()
    for q,a in zip(questions, answers):
        print "%s\n>>> %s" % (q, a)

We only have a Label, an Entry and a Button (I did not care about layout!, just pack()).

Attached to the button is a command (aka callback). When the button is pressed, the answer is read and the new question is assigned to the label.

Usage of this class is understandable from the example in the `if name == "main" block. Please note: the answers-list is filled in place, the questions-list is kept unchanged.

Thorsten Kranz
  • 12,492
  • 2
  • 39
  • 56
  • Okay, any other ideas then? – Annie Royal Feb 08 '13 at 12:34
  • It took some minutes to implement the sample, and I was interrupted by a phone call. It's in my answer now. – Thorsten Kranz Feb 08 '13 at 12:52
  • Don't forget to com back when this solved you problem. The means to tell the world that your problem is solved are "upvote" and "mark as answer" [compare the faqs](http://stackoverflow.com/faq#howtoask) – Thorsten Kranz Feb 08 '13 at 13:21
  • I like the idea of trying to import _Tkinter_. What I don't like is importing _Tkinter_ as _Tk_. This will cause confusion for beginners since the real _Tkinter.Tk_ is accessed via _self._ (in this example) and _Tkinter_ is being accessed by _Tk_. I recommend `import Tkinter as tki` to ensure no overlap / ambiguity. – Honest Abe Feb 08 '13 at 18:22
0

I don't know Tk, but is there no any signals of input text changed? There should be for sure. Just check if this signal occured and then move onto new question, because it means that someone typed something in input box.

Rafał Łużyński
  • 7,083
  • 5
  • 26
  • 38