0

I'm trying to get the user input from an entry box and once a button is pressed, display that in a tk.Text(). The reason I am not doing it in a label is because I'd like the gui looking something like this:

User: Hey

Response: What's up

User: Nothing..

I had a look at this documentation :http://effbot.org/tkinterbook/text.htm And example uses here but can't get mine to work.

result = None
window = Tk()

def Response():
    global result
    result = myText.get()

#The below print displays result in console, I'd like that in GUI instead.
    #print "User: ", result

#Creating the GUI
myText = tk.StringVar()
window.resizable(False, False)
window.title("Chatbot")
window.geometry('400x400')
User_Input = tk.Entry(window, textvariable=myText, width=50).place(x=20, y=350)
subButton = tk.Button(window, text="Send", command=Response).place(x =350, y=350)
displayText = Text(window, height=20, width=40)
displayText.pack()
displayText.configure(state='disabled')
scroll = Scrollbar(window, command=displayText).pack(side=RIGHT)
window.mainloop()

I have tried variations of; displayText.insert(window,result) and displayText.insert(End, result)

But still get nothing when I submit the text. The key thing is to obviously keep the last stored text of the User, rather than overwriting it, simply displaying each input underneath each other, I have been advised Text is the best method for this.

UPDATE

Thanks to the comments and answer by Kevin, the user text is now showing in the gui, but when I enter something and click send again, it goes to the side, like this:

HeyHey

rather than :

Hey

Hey

My chatbot is linked to Dialogflow and so in between each user input the chatbot will respond.

Tipping44
  • 281
  • 4
  • 16
  • 3
    With `state='disabled'`, the Text truly is disabled - programmatic changes are disallowed just as well as manual typing. You have to momentarily enable it before inserting your text, then disable it again. – jasonharper Aug 02 '18 at 12:29
  • Thanks for replying, something like this still doesn't work, am I right in thinking that I'd need it put this in the `def Response()` : `displayText.configure(state='normal') displayText.insert(window,result ) displayText.configure(state='disabled')` – Tipping44 Aug 02 '18 at 12:45
  • @jasonharper sorted now, up voted and thank you for your help. – Tipping44 Aug 02 '18 at 13:11

1 Answers1

1

as jasonharper indicated in the comments, you need to un-disable your text box before you can add text to it. Additionally, displayText.insert(window,result) is not the correct way to call it. insert's first argument should be an index, not the window object.

Try:

def Response():
    #no need to use global here
    result = myText.get()
    displayText.configure(state='normal')
    displayText.insert(END, result)
    displayText.configure(state='disabled')

(You may need to do tk.END or tkinter.END instead of just END, depending on how you originally imported tkinter. It's hard to tell since you didn't provide that part of your code)

Kevin
  • 74,910
  • 12
  • 133
  • 166
  • Thank you for the response @Kevin, that seems to have done the trick and have updated the question with the output,I was under the impression END dictated a possible new line? – Tipping44 Aug 02 '18 at 13:07
  • 1
    I don't think so. If you want a newline, you can add it yourself: `displayText.insert(END, "\n" + result)` – Kevin Aug 02 '18 at 13:10
  • Ignore that, think I solved the additional positioning with + '\n' Thank you – Tipping44 Aug 02 '18 at 13:10