Dear community members,
I currently ran into a problem with the TableEditor
's selected
attribute (traits version 4.5.0, traitsui version 4.5.1, Ubuntu). The problem occurs only for a special kind of Matryoshka structure, which is illustrated in the code below. Basically, no matter what, the selected
keeps pointing at None
. Correspondingly, watching for trait changes does not work.
Here is the code for illustration of my point:
from traits.api import HasTraits, List, Str, Float, Instance, Button
from traitsui.api import View, Item, TableEditor
from traitsui.table_column import ObjectColumn
table_editor = TableEditor(
selected = 'selected_guy',
columns = [ObjectColumn(name = 'name' , label = 'Name'),
ObjectColumn(name = 'height', label = 'Height in meters')]
)
class TallGuy(HasTraits):
name = Str('')
height = Float(0) #in meters
class BunchOfTallGuys(HasTraits):
selected_guy = Instance(TallGuy)
bunch_of_guys = List(TallGuy,
editor = table_editor)
whats_going_on = Button('What\'s going on?')
def _whats_going_on_changed(self):
print 'id of None: ', id(None)
print 'id of selected_guy: ', id(self.selected_guy)
#this still works fine, the problem arises on the next level
class BunchWrapper(HasTraits):
bunch = Instance(BunchOfTallGuys, required=True)
traits_view = View(
Item('object.bunch.bunch_of_guys', style='custom'),
Item('object.bunch.whats_going_on')
)
def __init__(self):
self.bunch = BunchOfTallGuys()
self.bunch.bunch_of_guys.append(TallGuy(name='Larry', height = 1.8))
self.bunch.bunch_of_guys.append(TallGuy(name='Garry', height = 2.0))
self.bunch.bunch_of_guys.append(TallGuy(name='Jerry', height = 1.95))
bw = BunchWrapper()
bw.configure_traits()
If you now press the 'What's going on?' button, you will see that the id of None
and of the selected_guy
trait keeps the same regardless of what line in the TabularEditor
you click. Is this expected behavior? Do you have any workaround for this, that allows to use the Matryoshka
structure, that is otherwise beneficial for me to keep my code well-arranged?
Any suggestions will be welcome!
Regards,
Jan