Defining view elements from dictionary elements in TraitsUI

試著忘記壹切 提交于 2019-12-11 23:33:04

问题


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

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


来源:https://stackoverflow.com/questions/19228631/defining-view-elements-from-dictionary-elements-in-traitsui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!