Search code examples
python-2.7pyqtenthoughtmayavitraitsui

Non-hacky solution for runtime created traits can't be displayed in View


Repeatedly I find myself in this situation,

class MainDisplay(HasTraits):
    a=Instance(A,())    
    def __init__(self):
        self.a=A()
    traits_view=View(...)

class A(HasTraits):
    i=Int()
    j=Int()
    k=Int()
    def __init__(self):
        i,j,k=something
    a_display=Group('i','j','k')

Problem: I want to display a.i,a.j,a.k in my main display.

Constraints:

(1) i,j,k absolutely must remain members of A and not MainDisplay. It just makes no sense to include them in MainDisplay and if I did that for every trait, MainDisplay would become far too cluttered.

(2) MainDisplay must not inherit class A. If it did, I could do "Include('a_display')" within traits_view. This is a nice trick for compartmentalizing some of the code from MainDisplay, but it will not work in my case.

(3) MainDisplay must not simply replicate the traits in A, and then sync them. For example MainDisplay.dummy_i=Int(), then later in init, self.sync_traits('dummy_i',self.a,'i',mutual=True). Lastly use 'dummy_i' in MainDisplay.traits_view. This works as well, but again the MainDisplay class becomes so cluttered! Also, every time I want to change the GUI (or a trait), I have two places to edit, which slows down development.

I'm new to GUI's, but I feel like not being able to variables created at runtime is a strange and awkward restriction. My impression is that Enaml, which looks to replace traitsUI also has this restriction.

My main interest is displaying and interacting with 3D geometries created by Mayavi. Is it possible maybe PyQt doesn't have this restriction?


Solution

  • Within MainDisplay's view, you can refer to Item("object.a.i"), etc, where object is literally what you type (not a placeholder). This is described in the TraitsUI documentation's "Advanced View Concepts", section Multi-Object Views.