Multi Object View Behaviour - Creating an Editor for a HasTraits subclass

北战南征 提交于 2019-12-08 04:28:39

问题


I am currently trying to make a traitsUI GUI for a class that contains many instances of a single object. My problem is very similar to the one solved in MultiObjectView Example TraitsUI.

However, I don't like the idea of using a context as it requires me to write out the same view many times for each object I have (and I might have a lot). So I tried to edit the code to make it so that each Instance of the House object would default to looking like its normal view when it is viewed from the Houses object. It almost worked, except now I get a button that takes me to the view I want rather than seeing the views nested in one window (like the output of the TraitsUI example above).

Is there a way to adapt the below to get the desired output? I think I have to further edit the create_editor function but I can find very little documentation on this - only many links to different trait editor factories...

Thanks,

Tim

# 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:


Would something like this work? It simplifies things a little bit and gives you a view that contains the list of views for your houses.

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


来源:https://stackoverflow.com/questions/24071067/multi-object-view-behaviour-creating-an-editor-for-a-hastraits-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!