How to use the last user input in Text widget in an If/Else statement after Enter is pressed?

Discussion in 'Python' started by viktor, Mar 31, 2019.

  1. viktor

    viktor

    Joined:
    Mar 31, 2019
    Messages:
    1
    Likes Received:
    0
    Hello. I am trying to make a chatbot using tkinter and I can't seem to make it use each last input inserted by the user in the Text widget to display the next according question.
    I tried already to put the nested if/else statements within the Enter_pressed function but it uses only the first if and the last else.
    Any help appreciated" Thank you.

    root = Tk()
    ...
    Text and Entry widgets created
    ...
    def first_question():
    question = str(">>>Question....?")
    messages.insert(INSERT, '%s\n' % question)
    messages.after(2000, first_question)

    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('')
    if input_get == answer1: ## the answer for first_question
    quest2 = str(">>>Question 2?")
    messages.insert(INSERT, '%s\n' % quest2)
    return "continue"
    if input_get == answer2: ## the program can't seem to read this new input_get
    quest3 = str(">>>Question 3?")
    messages.insert(INSERT, '%s\n' % quest3)
    return "continue"
    else:
    messages.insert(INSERT, " goodbye")
    else:
    quest4 = str(">>>Question4?")
    messages.insert(INSERT, '%s\n' % quest3)
    frame = Frame(root)
    input_field.bind("<Return>", Enter_pressed)
    frame.pack()

    root.mainloop()
     
    Last edited: Mar 31, 2019
    viktor, Mar 31, 2019
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.