Search code examples
pythonenthoughttraitsui

views on Nested HasTraits object


I am having trouble getting the views to work as expected, using nested HasTraits. For example:

class A(HasTraits):
    b= Any()

...
view = View(...
    Item('b', style='custom')
...

I would like to import the class of b and assign it to A,

from some_other_mod import B 
# B HasTraits also
a = A(b = B())

This works, but the view of B() does not display within a, when i a.configure_traits()

(Note this is related, but not identical to this post)


Solution

  • You need to use an InstanceEditor.

    class A(HasTraits):
        b = Instance(HasTraits)
        traits_view = View( Item('b', editor=InstanceEditor(), style='custom') )
    
    class B(HasTraits):
        c = Int
        traits_view = View( Item('c') )
    

    Note that an Instance trait uses an InstanceEditor by default. An Any trait by default uses a TextEditor instead.