【问题标题】:Multi Object View Behaviour - Creating an Editor for a HasTraits subclass多对象视图行为 - 为 HasTraits 子类创建编辑器
【发布时间】:2014-06-05 22:13:37
【问题描述】:

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

但是,我不喜欢使用上下文的想法,因为它需要我为我拥有的每个对象多次写出相同的视图(而且我可能有很多)。因此,我尝试编辑代码以使其在从 Houses 对象查看时,House 对象的每个 Instance 都默认看起来像它的正常视图。它几乎起作用了,除了现在我得到一个按钮,它将我带到我想要的视图,而不是看到嵌套在一个窗口中的视图(如上面 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()

【问题讨论】:

    标签: enthought traitsui


    【解决方案1】:

    这样的东西会起作用吗? 它稍微简化了一些事情,并为您提供了一个包含房屋视图列表的视图。

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

    【讨论】:

    • 谢谢!这给出了理想的输出。你能解释一下 style="custom" 实际上在做什么吗?我在 traitsui.Group 文档页面中找不到太多关于此属性的信息。
    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多