在TraitsUI中从字典元素定义视图元素 [英] Defining view elements from dictionary elements in TraitsUI

查看:89
本文介绍了在TraitsUI中从字典元素定义视图元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在traitsui视图中引用字典中的项目?

Is there a way to reference items in a dictionary in traitsui views?

换句话说,有一种方法可以使用Dict特性来完成我的意思:

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

推荐答案

这是我的工作.

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

这篇关于在TraitsUI中从字典元素定义视图元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆