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

2024-04-19 15:36:23 发布

您现在位置:Python中文网/ 问答频道 /正文

当按下Enter键时,我已经成功地从输入字段获取用户输入,并显示在文本小部件上,但是当我尝试使用用户输入的最后一个答案构建嵌套的if/语句时,它似乎无法识别它,并且停止了。你知道吗

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

Tags: 用户rightfieldinputgetifdefroot
2条回答

我正在努力向一个面向对象的方法传福音。。。看看下面的脚本

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

感谢您抽出时间回复。我试过你的选择,但不知怎么还是没能成功

我试着在def Enter\pressed(event)代码块中插入嵌套的if语句,它可以工作到一定程度。它识别第一个“如果”和最后一个“其他”,但不识别中间的“如果”和“其他”。你知道吗

在按下Return键作为文本小部件中显示的最后一个问题的答案之后,程序不应该看到并使用每个新的输入吗?你知道吗

相关问题 更多 >