3

亲爱的社区成员,

我目前遇到了TableEditor'selected属性的问题(traits 版本 4.5.0,traitsui 版本 4.5.1,Ubuntu)。该问题仅发生在一种特殊的俄罗斯套娃结构中,如下面的代码所示。基本上,无论如何,selected一直指向None。相应地,观察特征变化是行不通的。

这是说明我的观点的代码:

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()

如果您现在按“发生了什么事?” 按钮,您将看到特征的 idNone和trait 的 id 保持不变,无论您单击selected_guy哪一行。TabularEditor这是预期的行为吗?您是否有任何解决方法,允许使用该Matryoshka结构,否则对我保持我的代码井井有条是有益的?

任何建议都将受到欢迎!

问候,

4

0 回答 0