2

I have a button:

button3 = Button(app, text="Show Members", width=15, command=lambda: showLDAPMembers(yourName,yourPassword))

How do I bind the ENTER key to it? I tried doing:

app.bind('<Return>', showLDAPMembers(yourName,yourPassword))

but I get unresolved reference error..

def showLDAPMembers(yourName,yourPassword):
    app.lb.delete(0,END)
codefail
  • 371
  • 1
  • 8
  • 20
  • Possible duplicate of [How do I bind the enter key to a function in tkinter?](https://stackoverflow.com/questions/16996432/how-do-i-bind-the-enter-key-to-a-function-in-tkinter) – Stevoisiak Feb 22 '18 at 15:08

2 Answers2

7

You need to use a lambda if you're passing arguments.

app.bind("<Return>", lambda x: showLDAPMembers(yourName,yourPassword))

The bind command automatically returns the event that called it, so you need to define and throw away that (with lambda x:)

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • That did it, thanks..What if I have a more than one button that I'd like to bind the Enter key to? I need to have it bind the key to the button and the active entry? I have 2 fields, group1 and group2. I want to be able to type in group1, press Enter, see membership (lists in a listbox) and same with group2.. – codefail Feb 21 '14 at 19:52
  • You can bind a key to an Entry, so when it is active, and a key is pressed, it will call your function: group1.bind('', lambda e: function(args)). http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm – atlasologist Feb 21 '14 at 19:58
  • Three years and some later, I'd like to point out that it is perhaps more idiomatic to write this as `lambda evt: ...` or `lambda _: ...`. The former tells the reader what parameter you're expecting, and the latter tells the reader that you won't be using this parameter at all. `lambda x: ...` is, however, just lazy. – Adam Smith Aug 03 '17 at 23:39
0

first of all inside your function you need to

def showLDAPMembers(event= None)

then you can use the bind method to listen to your return key

app.bind("<Return>",showLDAPMembers())

I hope my answer helps you