2014-06-05 40 views
0

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

但是,我不喜欢使用上下文的想法,因为它需要我为每个对象(我可能会有很多)多次写出相同的视图。所以我试图编辑代码,使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() 

回答

2

会像这样的工作? 它简化了一些事情,并为您提供包含房屋视图列表的视图。

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

谢谢!这给出了理想的输出。你能解释一下style =“custom”实际上在做什么吗?我在traitsui.Group文档页面找不到这个属性。 – user2175850