[英]enthought traits UI, add values dynamically to 'values' trait of CheckListEditor
How can I add 'values' dynamically to the CheckListEditor? 如何将“值”动态添加到CheckListEditor?
But instead of static 'values', I have to get it dynamically from Bclass options attr. 但是,我必须从Bclass选项attr动态获取它,而不是静态“值”。
from enthought.traits.api import HasTraits, Instance, Str
from enthought.traits.ui.api import View, Item, CheckListEditor
class Bclass:
options = ['one', 'two']
class Aclass(HasTraits):
bclass = Instance(Bclass)
abc = Str
view = View(
Item(name='abc', editor=CheckListEditor(values=['one', 'two']) ),
style='simple',
)
Instead of giving to the CheckListEditor
a list of values
, you can give it the name of the trait containing the values: 您可以给它一个包含值的特征名称,而不是给
CheckListEditor
values
列表:
from traits.api import HasTraits, Instance, Str, List
from traitsui.api import View, Item, CheckListEditor
class Bclass(HasTraits):
options = List(['one', 'two'])
class Aclass(HasTraits):
bclass = Instance(Bclass)
abc = Str
traits_view = View(
Item(name='abc', editor=CheckListEditor(name='object.bclass.options')),
)
b = Bclass()
a = Aclass(bclass=b)
a.configure_traits()
In the example, 'object.bclass.options'
means: the traits called options
which is an attribute of the trait called bclass
in the context object
, ie, the namespace of the current object. 在示例中,
'object.bclass.options'
含义是:称为options
的特征,它是上下文object
(即当前对象的名称空间)中称为bclass
的特征的属性。 The context is only necessary when using the name of a trait in another class. 仅当在另一个类中使用特征名称时,上下文才是必需的。
Update following poster's comment: 更新以下张贴者的评论:
In a comment, the poster asked what would happen if Bclass
is not an HasTraits
class, and/or options
is a dictionary. 在评论中,发布者询问如果
Bclass
不是HasTraits
类和/或options
是字典,将会发生什么。
If Bclass
is not a HasTraits
class, you won't be able to respond to changes in the dictionary content, so I would do this: 如果
Bclass
不是HasTraits
类,则您将无法响应字典内容中的更改,因此我可以这样做:
from traits.api import HasTraits, Instance, Str
from traitsui.api import View, Item, CheckListEditor
class Bclass(object):
options = {'one': 1, 'two': 2}
class Aclass(HasTraits):
bclass = Instance(Bclass)
abc = Str
def default_traits_view(self):
options = self.bclass.options.keys()
view = View(
Item(name='abc', editor=CheckListEditor(values=options)),
)
return view
The default_traits_view
method is called to create the TraitsUI view dynamically. 调用
default_traits_view
方法可动态创建TraitsUI视图。
If Bclass
is a HasTraits
class, then you can do better: 如果
Bclass
是HasTraits
类,则可以做得更好:
from traits.api import HasTraits, Instance, Str, Property, Dict
from traitsui.api import View, Item, CheckListEditor
class Bclass(HasTraits):
options = Dict({'one': 1, 'two': 2})
class Aclass(HasTraits):
bclass = Instance(Bclass)
abc = Str
options = Property(Str, depends_on='bclass.options')
def _get_options(self):
return self.bclass.options.keys()
traits_view = View(
Item(name='abc', editor=CheckListEditor(name='options')),
)
In this case, the view is updated whenever the content of the options
dictionary changes. 在这种情况下,只要
options
字典的内容发生更改,视图就会更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.