0

Just wondering, How do I bind an entry field to (with the return key) a function that requires 2 arguments counting event as one of them without lambda or classes, just one function and 2 arguments:

def function(event,entry):#entry is the widget to be focused. also, entry is just a place holder NOT A WIDGET. entry MUST be specified. it cannot be removed.
    entry.focus()

entry1.bind("<Return>",function(None,entry2))

When entry1 is binded, the function that it is bound to executes right when it is binded and then it ignores all other input. It lets me put characters into the field, but when I hit return, it does not go through and focus the second entry. If I remove None as one of the arguments it gives me an error that only one of two required arguments are defined, it doesn't matter what I put in place of None, it still doesn't work. How do I make it work without classes or an anonymous function?

3 Answers 3

3

When you write function(None,entry2) you /are/ calling it right away -- the function probably returns None, so essentially what you are doing is:

function(None, entry2)
entry1.bind("<Return>", None)

What you are probably looking for is this:

entry1.bind("<Return>", lambda e: function(entry2))

This generates a function (note: generates the function, but doesn't call it) that takes one parameter (the event, "e") and ignores it

When you then hit the return key, this generated function will be called, and in turn it will call function(entry2)

2
  • Thanks! That worked, I had to remove event as one of the arguments! Thank you sooooo much! Oct 11, 2011 at 3:26
  • Do you know how to pass arguments to a function like I did above except with a Python Mega-Widget Scrolled Listbox(and not binding but by using the built-in dblclickcommand=function(1)? because right now, that just calls function right away and it does not work. Apr 14, 2012 at 0:15
1

The answer is to use lambda. For a good explanation see Tkinter Callbacks on effbot.org

-1

Without a full example, it's difficult to provide a full answer. However there is one common error that is easily fixed: the second argument to bind should be a function name, not a function call.

If you change the bind statement to:

entry1.bind("<Return>", function)

the function will be called when the Return event is triggered instead of when you execute the bind statement.

2
  • what if the function needs 2 arguments including event as one of them? Oct 11, 2011 at 2:03
  • A lambda is the right answer, but you ruled out lambdas and classes in the problem statement. If for some reason you absolutely hate lambda, then call a dummy function with just the event arg which then calls the actual function with the event and entry args.
    – Dave
    Oct 13, 2011 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.