Can someone explain why this code crashes? What I think should happen is that it should not crash if it is using fully qualified trait names, which it is in this case.
from traits.api import *
from traitsui.api import *
class Struct(HasTraits): pass
class Struct1(Struct):
some_data=Int(4)
some_more_data=Str('pizza')
class Struct2(Struct):
some_data=Int(5)
some_more_data=Str('wossar')
class Subwindow(Handler):
struct1=Instance(Struct1)
struct2=Instance(Struct2)
which_struct=Enum(1,2)
cur_struct=Any
def _struct1_default(self): return Struct1()
def _struct2_default(self): return Struct2()
def _cur_struct(self): return self.struct1
@on_trait_change('which_struct')
def switch_views(self): NotImplemented #switch views here
traits_view=View(
Item(name='which_struct'),
Item(name='object.cur_struct.some_data'),
Item(name='object.cur_struct.some_more_data'),
)
Subwindow().configure_traits()
When I run this, I get
AttributeError: 'Subwindow' object has no attribute 'object.cur_struct.some_data'
but it does, if you inspect the object.
I was fiddling with this example and I made it work correctly if I replace cur_struct
with a Property
trait, and I don't know why. However, that isn't feasible for my real application, where another class listens for events from an entirely different class and switches cur_struct
.
Ah, don't use Item(name=...)
. Just pass the name as the first positional argument. The constructor does some special processing on the value passed to it before assigning it to the name
trait. Explicitly using name
is only used internally when we need to avoid that processing.