python - enthought traits UI, add values dynamically to 'values' trait of CheckListEditor -
how can add 'values' dynamically checklisteditor?
instead of static 'values', have dynamically bclass options attr.
from enthought.traits.api import hastraits, instance, str 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 checklisteditor list of values, can give name of trait containing values:
from traits.api import hastraits, instance, str, list 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() = aclass(bclass=b) a.configure_traits() in example, 'object.bclass.options' means: traits called options attribute of trait called bclass in context object, i.e., namespace of current object. context necessary when using name of trait in class.
update following poster's comment:
in comment, poster asked happen if bclass not hastraits class, and/or options dictionary.
if bclass not hastraits class, won't able respond changes in dictionary content, this:
from traits.api import hastraits, instance, str 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 called create traitsui view dynamically.
if bclass hastraits class, can better:
from traits.api import hastraits, instance, str, property, dict 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 case, view updated whenever content of options dictionary changes.
Comments
Post a Comment