I'm designing a HasTraits
subclass with dependent properties:
#!/usr/bin/env python
# Example for SO question on dynamically changing Dict contents.
from traits.api import HasTraits, Dict, Property, Trait, Int, cached_property
from traitsui.api import View, Item
class Foo(HasTraits):
"Has dependent properties, which I'd like to remain up-to-date in the GUI."
_dicts = [
{"zero": 0, "one": 1},
{"zero": 1, "one": 2},
{"zero": 2, "one": 3},
]
zap = Int(0)
bar = Property(Trait, depends_on=["zap"])
baz = Trait(list(_dicts[0])[0], _dicts[0])
@cached_property
def _get_bar(self):
return Trait(list(self._dicts)[self.zap], self._dicts)
traits_view = View(
Item("zap"),
Item("bar"),
Item("baz"),
width=500,
)
if __name__ == '__main__':
Foo().configure_traits()
When I run this code I see:
And if I change the value of Zap
:
Note the following:
After changing
Zap
, the address ofBar
has changed.This means that changes to
Bar
are being dynamically updated in the GUI, while it's still opened; that's great! However...The way
Bar
is displayed in the GUI is not very useful.I'd love to have
Bar
displayed asBaz
is displayed: selectable by the user.
What I'd like is to have the best of both worlds:
- the dynamic GUI updating I see with
Bar
, and - the display format of
Baz
.
Does anyone know how I can get this?
I've tried several ways of updating a Baz
-like item dynamically, to no avail.
(See this previous SO question.)