How to Pass an Argument to Event Handler in Tkinter

Passing arguments to a tkinter event callback bound to a key

Your second attempt was close, but since checkPassword isn't a class method, you shouldn't be prefixing it with self..

def checkPassword(event, some_value_you_want):
# do stuff

Entry.bind("<KeyRelease-Return>", lambda event: checkPassword(event, name_of_your_entry_widget))

Tkinter binding a function with arguments to a widget

When you create a binding with bind, Tkinter automatically adds an argument that has information about the event. You'll need to account for that either in your rand_func definition or in how you call it.

This argument is not included when you use the command attribute. You must take care to account for this extra argument either in how you call the function in each case, or in how the function interprets its parameters.

Here's one solution that uses lambda in the binding to accept the extra event only when using bind command, but not pass it on to the final command.

import tkinter as tk

class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.frame = tk.Frame(self)
self.frame.pack()
self.button = tk.Button(self.frame, text="click me",
command=lambda a=1, b=2, c=3:
self.rand_func(a, b, c))
self.button.pack()
self.frame.bind("<Return>",
lambda event, a=10, b=20, c=30:
self.rand_func(a, b, c))
# make sure the frame has focus so the binding will work
self.frame.focus_set()

def rand_func(self, a, b, c):
print "self:", self, "a:", a, "b:", b, "c:", c
print (a+b+c)

app = SampleApp()
app.mainloop()

That being said, it's rare that binding to a frame is the right thing to do. Typically a frame won't have keyboard focus, and unless it has focus the binding will never fire. If you are setting a global binding you should either bind to the "all" binding tag (using the bind_all method) or to the toplevel widget.

How do arguments get passed when using .bind?

When you bind a function to an event, tkinter will always pass one argument to the function. This argument is an object that contains information about the event, such as the widget that received the event, the key or mouse that caused the event, etc.

If you are calling a function that doesn't need the argument, just ignore it. If you need to use the function also outside the context of an event, give it a default value of None.

If you are passing additional arguments to your callback, you need to account for the event argument in your lambda. You don't have to use it, but your lambda needs to accept it. For example:

self.Button.bind('<Button-3>', lambda event: self.setState(self.state,1)) 

When using an event in Tkinter, how can you pass in other variables to the event function?

Here are two possible ways.

  1. You can put your 'callback' function in a class, and use self to pass in self-defined variables
  2. You can set all your variables in another module file and import it to this file. Since variables from another file have been imported to this namespace, you can use them without passing into 'callback' function, just the same as global variables.


Related Topics



Leave a reply



Submit