Find what default events an object has

What I’m trying to do:
Create a method that adds events to an object based on the events that object has by default.

What I’ve tried and what’s not working:

I have tried using the augment class from anvil_extras, but if the object already has the event being set, it will be raised twice.

I could easily solve my issue by checking what obj has what event and putting if statements, but I’d like to check if the event is a default event programmatically.

Code Sample:

def add_event(obj):
    augment.set_event_handler(obj,'click',self.alter_color)  #If the Obj is a button, click will be raised twice.

I’m not sure I totally follow the question. But you might find get_event_handlers a useful piece of api. See the component api docs

1 Like

Thank you for pointing me to that document.

Here is a clone to illustrate my issue:

You’ll see that clicking the label will yield a single “Clicked” print statement where the button will print two “Clicked” statements.

When printing out the event handlers using the get_event_handlers method, they both only have a single event handler.

What I was looking for was a method that is more akin to get_default_events. This would allow me to detect that the button object already has a default “click” event without knowing its type before hand. I could than use the objects add_event_handler instead of using augment, to avoid the double click that occurs.

Ah that makes sense, thanks for providing the clone.

The augment module isn’t designed to augment existing Anvil events, such as augmenting a Button with a "click" event. That explains why it isn’t functioning as anticipated. I’ve implemented a fix, and it should work as expected soon.

There isn’t a method to determine which events are available for a specific component.


On a side note,
I presume you’re attempting more than simply making elements clickable? If your sole aim is to create a clickable object, you can wrap it in a Link Component and assign the click event handler to the Link component.


There may be a more suitable approach to addressing the issue you’re encountering, but it’s difficult to make a better suggestion without knowing the initial problem that prompted the question.

1 Like

I appreciate it!

For more context, I am working on a creating tool that will allow you to add a container and any type of object to it and give that object a few properties:

  1. Alternate color on hover
  2. select and alternate color on click
  3. Be able to drag-select multiple items

Then you would be able to pull all the selected items similar to the multi-drop-down object.

It’s hard to capture the drag-select feature via a gif but here are all three properties on display:

file

This is how I currently avoid the issue.

  def add_item(self,key,value):
    self.selected_map[key] = value
    key.set_event_handler('click',self.select_item)
    augment.add_event_handler(key,'hover',self.alter_color)
    augment.add_event_handler(key,'mouseleave',self.final_color)
    augment.add_event_handler(key,'hover',self.select_item_drag)

I have a few other ideas in mind and want to do a show-n-tell post, but I’d like to make it robust before that. I thought, if there was an easy way to dynamically determine which objects-event pairs would fire twice, I could avoid some headaches.

EDIT: Updated the context statement above with the italic containter portion.

We’ve fixed that bug now on the anvil-extras development dependency.

Looking forward to the show and tell!

1 Like