问题描述
我目前正在尝试为包含单个对象的许多实例的类制作traitsUI GUI.我的问题与 MultiObjectView中解决的问题非常相似示例TraitsUI .
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.
但是,我不喜欢使用上下文的想法,因为它要求我为我拥有的每个对象多次写相同的视图(并且我可能会有很多).因此,我尝试对代码进行编辑,以使House对象的每个实例在从Houses对象查看时都默认为其正常视图.它几乎起作用了,只是现在我得到一个按钮,该按钮将我带到想要的视图,而不是看到嵌套在一个窗口中的视图(如上面的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).
是否有办法使以下内容适应所需的输出?我认为我必须进一步编辑create_editor函数,但是我几乎找不到关于它的文档-只有许多指向不同特征编辑器工厂的链接...
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...
谢谢
蒂姆
# 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()
推荐答案
这样的作品有用吗?它可以简化一些事情,并为您提供一个包含房屋视图列表的视图.
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()
这篇关于多对象视图行为-为HasTraits子类创建编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!