按下Enter键后,如何在If / Else语句中使用Text小部件中的最后一个用户输入?

时间:2019-03-24 07:20:10

标签: python tkinter

当按Enter键时,我设法从“输入”字段中获取user_input并显示在“文本”小部件上,但是当我尝试使用用户输入的最后一个答案来构建嵌套的if / statement时,它不会似乎无法识别它并且停止了。

root = Tk()
...
Text and Entry widgets created
...

def Enter_pressed(event):
    global input_get
    input_get = input_field.get()
    print(input_get)
    messages.tag_config("right", justify="right")    
    messages.insert(INSERT, '%s\n' % input_get, "right")
    input_user.set('')
    return "continue"
frame = Frame(root)
input_field.bind("<Return>", Enter_pressed)
frame.pack()

def question():
    question1 = str(">>>Do you like apples?")
    messages.insert(INSERT, '%s\n' % question1)
    if input_get == str("Yes") or input_get == str("yes"):
        messages.insert(INSERT, ">>>Great")
    else:
        question2 = str(">>>How about peaches?")
        messages.insert(INSERT, '%s\n' % question2)            
        if input_get == str("Yes") or input_get == str("yes"):
            messages.insert(INSERT, ">>>I like peaches too.")
        else:
            messages.insert(INSERT, ">>Have you tried mango?")
messages.after(5000, question)

root.mainloop()

2 个答案:

答案 0 :(得分:0)

我正在尝试向oo方法传播...所以请看下面的脚本

import tkinter as tk

class App(tk.Frame):

    def __init__(self,):

        super().__init__()

        self.master.title("Hello World")

        self.option =tk.IntVar()
        self.option.set(2)
        self.questions = {0:"Do you like apples?",
                          1:"How about peaches?",
                          2:"I like peaches too.",
                          3:"Have you tried mango?",
                          4:"Great",
                          5:"End of the questions."}

        self.which = tk.IntVar()

        self.which.set(0)

        self.init_ui()

    def init_ui(self):

        self.pack(fill=tk.BOTH, expand=1,)

        f = tk.Frame()

        self.question = tk.Label(f, text = self.questions[0])
        self.question.pack()

        ops = ('Yes','No',)

        for index, text in enumerate(ops):
            tk.Radiobutton(f,
                            text=text,
                            variable=self.option,
                            command=self.callback,
                            value=index,).pack(anchor=tk.W)  

        w = tk.Frame()

        tk.Button(w, text="Print", command=self.callback).pack()
        tk.Button(w, text="Reset", command=self.on_reset).pack()
        tk.Button(w, text="Close", command=self.on_close).pack()

        f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
        w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)


    def callback(self):

        #print(self.option.get())

        if self.which.get()==0:
            if self.option.get() == 0:
                self.which.set(4)

            else:
                self.which.set(1)

        elif self.which.get()==1:

            if self.option.get() == 0:
                self.which.set(2)
            else:
                self.which.set(3)

        else:
            self.which.set(5)

        a = self.questions[self.which.get()]
        self.question.config(text=a)
        self.option.set(2)            

    def on_reset(self):

       self.which.set(0)
       self.option.set(2)
       a = self.questions[self.which.get()]
       self.question.config(text=a)

    def on_close(self):
        self.master.destroy()

if __name__ == '__main__':
    app = App()
    app.mainloop()

答案 1 :(得分:0)

感谢您抽出宝贵的时间回复。我尝试了您的选择,但无法使其正常运行。`

我试图将嵌套的if语句插入def Enter_presssed(event)代码块内,并且可以正常工作。它可以识别第一个“ if”和最后一个“ else”,但不能识别中间的ifs和else。

在按回车键作为对文本小部件中显示的最后一个问题的答案之后,程序不应该看到并使用每个新的input_get吗?