Custom events best practices

I have a MainForm that has event handlers that can be called by other forms, e.g.:

self.set_event_handler('x-update-title', self.update_title)

def update_title(self,  **event_args):
   # make some changes to self.title.text

I can happily call this from other places in the app when I know main form is open:

get_open_form().raise_event('x-update-title')  

Works fine. However, I also want the update_title thing to happen when the form shows. There are several ways to do this, I’m just trying to find out if there is a best practice approach I should be following.

Options:

a) raise the event from inside the form:

self.raise_event('x-update-title')  

This works, but seems circuitous.

b) call the event handling routine like I would any other routine from inside the form:

self.update_title() 

This works because there are no event_args needed for this routine, but it feels like cheating in a way I can’t articulate.

c) make a separate function that does the thing, and call that from inside the event handler:

  def update_title(self,  **event_args):
    self.update_title_text()

  def update_title_text(self):
     # Do the actual stuff here

Then from inside form_show (or wherever), I just call the actual routine that does the thing:

    self.update_title_text()

This seems a little hoop-jumpy, but otoh it is a pattern I can use that will be more consistent for ALL event handlers, including ones where the event_args are important.

Advice???

I generally use your option B, just call the event handler function directly.

3 Likes