Tkinter: Why is my &#34;<keyrelease-return>&#34; Event not working?

时间:2017-04-10 00:50:58

标签: python-3.x tkinter

I am trying to get the value from an Entry Box when a 'Return Key Release' event occurs. But it does not seem to be working.

My code is something like :

EntryBox = Text(base, bg="white",width="29", height="5", font="Arial")
EntryBox.bind("<KeyRelease-Return>", sendData(EntryBox.get(1.0, END)))

My sendData function is:

def sendData(param):
    if len(param) > 1:
            EntryBox.delete(1.0, END)
            insertText(param)
            s.sendall(str.encode(param))
            data = s.recv(4096)
            insertText(data.decode('utf-8'))

My insertText function:

def insertText(param):
        ChatLog.config(state = NORMAL)
        ChatLog.insert(END, param)
        ChatLog.config(state = DISABLED)

1 个答案:

答案 0 :(得分:2)

The bind argument must be a function, not the result of a function. By adding () to the end of the function it's immediately executed and the result is provided to bind. You need to make a function and provide that:

EntryBox = Text(base, bg="white",width="29", height="5", font="Arial")
def on_return(event):
    sendData(EntryBox.get(1.0, END))
EntryBox.bind("<KeyRelease-Return>", on_return)

For a function this tiny you could use lambda to create it if you want:

EntryBox = Text(base, bg="white",width="29", height="5", font="Arial")
EntryBox.bind("<KeyRelease-Return>", lambda event: sendData(EntryBox.get(1.0, END)))