Skip to content Skip to sidebar Skip to footer

Traits List Not Reporting Items Added Or Removed

Given, from enthought.traits.api import HasTraits, Tuple, Delegate, Trait, Float,Dict,List class Foo(HasTraits): def __init__(self): super(Foo,self).__init__()

Solution 1:

There seems to be a distinction between changing the value of the collection as a whole (the List) and changing a member within the collection. Appending appears to change a member within (or at least results in the same notification). If you change the value of the container as a whole, you do indeed get the changed list as the new value:

from enthought.traits.api import HasTraits, Tuple, Delegate, Trait, Float,Dict,ListclassFoo(HasTraits):
    def__init__(self):
        super(Foo,self).__init__()
        self.add_trait('node',List)
    def_node_changed(self,name,old,new):
        print("_node_changed: %s %s %s" % (name, str(old), str(new)))
    def_node_items_changed(self,name,old,new):
        print("_node_items_changed: %s %s %s" % (name, str(old), str(new)))

f = Foo()

# change the List membership with append:
f.node.append(0)
# _node_items_changed: node_items <undefined> <traits.trait_handlers.TraitListEvent object at 0x10128af50># change the List itself:
f.node = [1,2,3]
# _node_changed: node [0] [1, 2, 3]# change a member (same result as append):
f.node[1] = 4# _node_items_changed: node_items <undefined> <traits.trait_handlers.TraitListEvent object at 0x10128af50>

There's more info here, if you haven't seen this section already. See this answer too.

Post a Comment for "Traits List Not Reporting Items Added Or Removed"