2014-06-05 45 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