Skip to content Skip to sidebar Skip to footer

Defining View Elements From Dictionary Elements In Traitsui

Is there a way to reference items in a dictionary in traitsui views? In other words, is there a way to do what I mean with the following, using a Dict trait: from traits.api import

Solution 1:

This worked from me.

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()

Post a Comment for "Defining View Elements From Dictionary Elements In Traitsui"