Ilan Rossler
Ilan Rossler

Reputation: 33

Python chat box whith Tkinter

I'm trying to do a chat box with Tkinter, but for send the text I want to press on Return key and not to click on a button. When Ï run this code, I can wrote in the Entry section, but when I press the Return key, nothing append. Thanks for your help. (Sorry for bad English)

from tkinter import *


window = Tk()


input_user = StringVar()
input_field = Entry(window, text=input_user)
input_field.pack()

def Enter_pressed(event):
    """Took the current string in the Entry field."""
    input_get = input_field.get()
    print(input_get)



frame = Frame(window, width=100, height=100)
frame.bind("<Return>", Enter_pressed)
frame.pack()


window.mainloop()

Upvotes: 1

Views: 2977

Answers (1)

James Kent
James Kent

Reputation: 5933

you are binding to the wrong widget, when you press the return key that event is sent to the entry widget not the frame, so change

frame.bind("<Return>", Enter_pressed)

to:

input_field.bind("<Return>", Enter_pressed)

and if you want to prevent other bindings from firing you can add

return "break"  

to the end of your function

Upvotes: 4

Related Questions