3.1 Dynamic
Notification
Dynamic notification is useful in cases where a
notification handler cannot be defined on the class (or a subclass) whose trait
attribute changes are to be monitored, or if you want to monitor changes on
certain instances of a class, but not all of them. To use dynamic notification,
you define a handler method or function, and then invoke the on_trait_change()
or on_trait_event() method to register that handler with the object being
monitored. Multiple handlers can be defined for the same object, or even for
the same trait attribute on the same object.
The handler registration methods have the following
signatures:
obj.on_trait_change(handler, name=None,
remove=False, dispatch='same')
obj.on_trait_event(handler, name=None,
remove=False, dispatch='same')
·
handler—Specifies the function or
bound method to be called whenever the trait attributes specified by the name parameter are modified.
·
name—Specifies trait attributes
whose changes trigger the handler being called. If this parameter is omitted or
is None, the handler is called whenever any trait
attribute of the object is modified. The syntax supported by this parameter is
discussed in Section 3.1.2.
·
remove—If True (or non-zero), then handler will no longer be called when the specified
trait attributes are modified. In other words, it causes the handler to be
“unhooked”.
·
dispatch—String indicating the
thread on which notifications must be run. In most cases, it can be omitted.
See the Traits API Reference for details on
non-default values.
|