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 bind开发者_运维问答ed 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?
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)
The answer is to use lambda
. For a good explanation see Tkinter Callbacks on effbot.org
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.
精彩评论