2013-02-08 68 views
1

这是我用来启动程序的主要部分的函数的代码,但是我想要某种循环或者其他东西来创建十个问题,但是在进入下一个之前等待来自输入框的输入题。 任何想法?如何使程序在Python GUI中使用输入框等待输入?

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() 

我希望循环的AnswerReply = None

回答

0

我不知道Tk的后开始,但那里没有input text changed任何信号?应该是肯定的。只要检查这个信号是否发生,然后转移到新的问题上,因为这意味着某人在输入框中输入了某些内容。

2

你不想要循环。 GUI中唯一真正重要的循环应该是mainloop(),处理信号并执行回调

例子:

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) 

我们只有一个标签,一个入口和一个按钮(我也没在意布局!只是pack())。

附加到按钮是一个命令(aka回调)。按下该按钮时,将读取答案并将新问题分配给标签。

这个类的用法可以从'if name =='main“的例子中理解。请注意:答案列表填写到位,问题列表保持不变。

+0

好吧,还有其他想法吗? – 2013-02-08 12:34:25

+0

实施样品需要几分钟的时间,我被打电话打断了。这是我现在的答案。 – 2013-02-08 12:52:39

+0

啊谢谢你(: – 2013-02-08 13:10:14