多对象视图行为 - 为HasTraits子类创建编辑器

时间:2014-06-05 22:13:38

标签: enthought traitsui

我目前正在尝试为包含单个对象的许多实例的类创建traitsUI GUI。我的问题与MultiObjectView Example TraitsUI中解决的问题非常相似。

但是,我不喜欢使用上下文的想法,因为它要求我为每个对象多次写出相同的视图(我可能会有很多)。因此,我尝试编辑代码,使得每个Instance of House对象在从Houses对象查看时默认看起来像普通视图。它几乎可以工作,除了现在我得到一个按钮,带我到我想要的视图,而不是看到嵌套在一个窗口中的视图(如上面的TraitsUI示例的输出)。

有没有办法调整下面的内容以获得所需的输出?我想我必须进一步编辑create_editor函数,但我可以找到很少的文档 - 只有很多链接到不同的特征编辑器工厂......

谢谢,

# multi_object_view.py -- Sample code to show multi-object view
#                         with context

from traits.api import HasTraits, Str, Int, Bool
from traitsui.api import View, Group, Item,InstanceEditor

# Sample class
class House(HasTraits):
    address = Str
    bedrooms = Int
    pool = Bool
    price = Int

    traits_view =View(
        Group(Item('address'), Item('bedrooms'), Item('pool'), Item('price'))
        )

    def create_editor(self):
        """ Returns the default traits UI editor for this type of trait.
        """
        return InstanceEditor(view='traits_view')



class Houses(HasTraits):
    house1 = House()
    house2= House()
    house3 = House()
    traits_view =View(
        Group(Item('house1',editor = house1.create_editor()), Item('house2',editor = house1.create_editor()), Item('house3',editor = house1.create_editor()))
        )


hs = Houses()
hs.configure_traits()

1 个答案:

答案 0 :(得分:3)

这样的事情会起作用吗? 它简化了一些事情,并为您提供了一个视图,其中包含您家的视图列表。

# multi_object_view.py -- Sample code to show multi-object view
#                         with context

from traits.api import HasTraits, Str, Int, Bool
from traitsui.api import View, Group, Item,InstanceEditor

# Sample class
class House(HasTraits):
    address = Str
    bedrooms = Int
    pool = Bool
    price = Int

    traits_view =View(
        Group(
            Item('address'), Item('bedrooms'), Item('pool'), Item('price')
        )
    )


class Houses(HasTraits):
    house1 = House()
    house2= House()
    house3 = House()

    traits_view =View(
        Group(
            Item('house1', editor=InstanceEditor(), style='custom'),
            Item('house2', editor=InstanceEditor(), style='custom'), 
            Item('house3', editor=InstanceEditor(), style='custom')
        )
    )

if __name__ == '__main__':
    hs = Houses()
    hs.configure_traits()