Treehopper Python API
EventHandler Class Reference

A simple event handling class, which manages callbacks to be executed. More...

Public Member Functions

def __init__ (sender)
 Construct a new EventHandle. More...
 
def __call__ ()
 Executes all callbacks. More...
 
def __iadd__ (callback)
 Adds a callback to the EventHandler. More...
 
def __isub__ (callback)
 Removes a callback from the EventHandler. More...
 
def __len__ ()
 Gets the amount of callbacks connected to the EventHandler. More...
 
def __getitem__ (index)
 Gets the callback at the specified index. More...
 
def __setitem__ (index, value)
 Sets the callback at the specified index. More...
 
def __delitem__ (index)
 Deletes the callback at the specified index. More...
 
def add (callback)
 Adds a callback to the EventHandler. More...
 
def remove (callback)
 Removes a callback from the EventHandler. More...
 

Public Attributes

 callbacks
 a list of callbacks that have registered to this event More...
 
 sender
 the sender (captured by the constructor) More...
 

Detailed Description

A simple event handling class, which manages callbacks to be executed.

Subscribing to events

This class uses C# syntax for subscribing/unsubscribing to events. You may either declare a plain function as a handler, or you can use a lambda.

Regardless, the first argument with which your method will be called is the "sender" of the event, which is always the class the EventHandler is instantiated inside (it's not the EventHandler object itself).

The second argument is the event. Unlike our strongly-typed EventArgs classes in C#, Java, and C++, this event class is more Pythonic: raw event values are published as the second argument of this event handler, removing the need to unpack the event.

Because this is not strongly typed, be sure to read the documentation so you know what type to expect.

Here's an example of subscribing to the Pin.analog_voltage_changed event using a lambda:

>>> board = find_boards()[0]
>>> board.connect()
>>> pin = board.pins[0]
>>> pin.mode = PinMode.AnalogInput
>>> pin.analog_voltage_changed += lambda sender, value: print(value)

Though you could just as easily pass the name of a function in place of the lambda in-line statement. Imagine having a ``, then passing it to the event:

>>> def my_event_handler(sender, value):
>>> print(value)
>>>
>>> pin.analog_voltage_changed += my_event_handler

Publishing events

You can use this module in your own classes to publish events. Simply initialize a variable as an EventHandler:

>>> my_event = EventHandler(self)

Then call the event just like a function:

>>> my_event(0.5)

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   sender)

Construct a new EventHandle.

Member Function Documentation

◆ __call__()

def __call__ ( )

Executes all callbacks.

Executes all connected callbacks in the order of addition, passing the sender of the EventHandler as first argument and the optional args as second, third, ... argument to them.

◆ __delitem__()

def __delitem__ (   index)

Deletes the callback at the specified index.

◆ __getitem__()

def __getitem__ (   index)

Gets the callback at the specified index.

◆ __iadd__()

def __iadd__ (   callback)

Adds a callback to the EventHandler.

◆ __isub__()

def __isub__ (   callback)

Removes a callback from the EventHandler.

◆ __len__()

def __len__ ( )

Gets the amount of callbacks connected to the EventHandler.

◆ __setitem__()

def __setitem__ (   index,
  value 
)

Sets the callback at the specified index.

◆ add()

def add (   callback)

Adds a callback to the EventHandler.

◆ remove()

def remove (   callback)

Removes a callback from the EventHandler.

Member Data Documentation

◆ callbacks

callbacks

a list of callbacks that have registered to this event

◆ sender

sender

the sender (captured by the constructor)


The documentation for this class was generated from the following file: