0

I am building a gui using HasTraits objects. I have a main object, and then objects for each component of the gui. I would like to share an object across multiple components. For example, i have a main window A which has components B and C. C needs to have access to things in B. currently i am doing this

class B(HasTraits):
     ...

class C(HasTraits):
     ...

class A(HasTraits):
    b = Instance(B,())
    c = Instance(C)
    ...

def _c_default(self):
    return C(b = self.b,...)
    ...

Which seems like not the best way to do this. Also there are sub-sub-components so A.C.D, and D needs things in A.B.

Is this the best way to share objects using traits?

alex
  • 2,968
  • 3
  • 23
  • 25

1 Answers1

0

It depends a bit on the use case but what you suggest looks like a first approach.

Some comments:

  1. you could listen to changes on b to make sure C is kept in sync with the changes on a.
  2. you could use the DelegatesTo with the C trait to expose some of the B traits to C.

If you are talking more in terms of application design, you might look at Envisage and how it uses services and extension points to properly share objects within a pluggable application.

Example :

class B(HasTraits):
    name = Str

class C(HasTraits):
     b = Instance(B)
     name = DelegatesTo(b)

class A(HasTraits):
    b = Instance(B,())
    c = Instance(C)
    ...

    def _c_default(self):
        return C(b = self.b,...)
        ...

    def _b_changed(self):
         self.c.b = b
dpinte
  • 432
  • 2
  • 4