2013-10-07 59 views
0

有沒有方法可以在traitsui視圖中引用字典中的項目?在TraitsUI中定義字典元素中的視圖元素

換句話說,有沒有辦法做我的意思是用下面,用快譯通特點:

from traits.api import * 
from traitsui.api import * 
from traitsui.ui_editors.array_view_editor import ArrayViewEditor 
import numpy as np 

class SmallPartOfLargeApplication(HasTraits): 
    a=Dict 

    def _a_default(self): 
    return {'a_stat':np.random.random((10,1)), 
      'b_stat':np.random.random((10,10))} 

    traits_view=View(
    Item('a.a_stat',editor=ArrayViewEditor())) 

SmallPartOfLargeApplication().configure_traits() 

回答

2

這從我的工作。

from traits.api import * 
from traitsui.api import * 
from traitsui.ui_editors.array_view_editor import ArrayViewEditor 
import numpy as np 

class DContainer(HasTraits): 
    _dict=Dict 
    def __getattr__(self, k): 
     if k in self._dict: 
      return self._dict[k] 

class SmallPartOfLargeApplication(HasTraits): 
    d=Instance(DContainer) 

    def _d_default(self): 
    d=DContainer() 
    d._dict={'a_stat':np.random.random((10,1)), 
      'b_stat':np.random.random((10,10))} 

    return d 

    def traits_view(self): 
    v=View(
     Item('object.d.a_stat',editor=ArrayViewEditor())) 
    return v 

SmallPartOfLargeApplication().configure_traits()