Skip to content Skip to sidebar Skip to footer

How Do I Make Bind And Command Do The Same Thing In Tkinter?

I have a tkinter GUI with an entry field and a validation button. I'd like to call the same function when I press a key in the entry, or when I click the button. The problem is, wi

Solution 1:

You could write your function so that it takes any number of arguments:

deffunction(*args):
    #args is now a tuple containing every argument sent to this functionprint("Here are some words.")

Now you can successfully call the function with 0, 1, or any other number of arguments.


Alternatively, you could keep your function as it is, and use a lambda to discard the unused self value:

input_widget.bind("<Return>", lambda x: function())

Solution 2:

Add this to your binding:

input_widget.bind("<Return>", lambda event: function())

Event bindings all require an event parameter which is automatically passed to the function. By using a lambda with the parameter event; you can take in this "event" variable and basically discard it and do whatever is in function()

Post a Comment for "How Do I Make Bind And Command Do The Same Thing In Tkinter?"