KeyError sender

This is a great contribution. Thank you.

May I ask a question?

when I use the following code
augment.set_event_handler(link_1, ‘hover’, function1)

It worked well. But when I need to pass an additional parameter to the function, and write
augment.set_event_handler(link_1, ‘hover’, function1(parameter1))

I got an error message: “KeyError, sender”

What did I do wrong? Would really appreciate. Thanks.

Hi @huarong and welcome to the forum.

It looks like you’re not passing a function as the third argument but calling a function.

I bet inside the function you have the line
event_args['sender']
This is likely where you’re getting the key error.

If you could provide a clone link or more code snippets then we can suggest some ways to achieve what your trying to do.

Also worth using back ticks to showcase code so that it is formatted correctly

```python
# code is formatted 

```

Here the object function1, that is the definition of function1, is passed to the event handler:

Here function1 is called, then the returned value is passed to the event handler:

Here function1 is called with an argument, then the returned value is passed to the event handler:

Very likely all you need is to use the first form, the one with the object function. This will tell the event handler what function to call when it’s time to call it. Then, when the event is triggered, the handler will know both the function to call and the parameter values, and will call that function with the correct parameter values.

2 Likes

Many thanks, @stucork and @stefano.menci

Stefano’s explanations clarified my issues. I have found a way to work around after understanding the differences.