Reputation: 281
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.
Upvotes: 0
Views: 5747
Reputation: 76234
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)
Upvotes: 2