Editing list of instances - traitsui documentation example

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Editing list of instances - traitsui documentation example

Neil Hodgson

In the traitsui documentation there is the example below of editing a list of Instances.

When the View is constructed it seems to be using the name attribute on the Person to populate the drop down of list contents.

However if you edit the name trait on a Person instance then you can no longer edit the Person, because the drop down is not updated to reflect the new name.  I assume there is a lookup somewhere that is not updated. 

Seems like a bug, or at least not 'intuitive' behaviour?

Thanks
Neil
# instance_editor_selection.py -- Example of an instance editor
#                                 with instance selection

from traits.api    \
    import HasStrictTraits, Int, Instance, List, Regex, Str
from traitsui.api \
    import View, Item, InstanceEditor

class Person ( HasStrictTraits ):
    name  = Str
    age   = Int
    phone = Regex( value = '000-0000',
                   regex = '\d\d\d[-]\d\d\d\d' )

    traits_view = View( 'name', 'age', 'phone' )

people = [
  Person( name = 'Dave',   age = 39, phone = '555-1212' ),
  Person( name = 'Mike',   age = 28, phone = '555-3526' ),
  Person( name = 'Joe',    age = 34, phone = '555-6943' ),
  Person( name = 'Tom',    age = 22, phone = '555-7586' ),
  Person( name = 'Dick',   age = 63, phone = '555-3895' ),
  Person( name = 'Harry',  age = 46, phone = '555-3285' ),
  Person( name = 'Sally',  age = 43, phone = '555-8797' ),
  Person( name = 'Fields', age = 31, phone = '555-3547' )
]

class Team ( HasStrictTraits ):

    name    = Str
    captain = Instance( Person )
    roster  = List( Person )

    traits_view = View( Item('name'),
                        Item('_'),
                        Item( 'captain',
                              label='Team Captain',
                              editor =
                                  InstanceEditor( name = 'roster',
                                                  editable = True),
                              style = 'custom',
                             ),
                        buttons = ['OK'])

if __name__ == '__main__':
    Team( name    = 'Vultures',
          captain = people[0],
          roster  = people ).configure_traits()

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Editing list of instances - traitsui documentation example

Pietro Berkes
Mmmh... it does look like a bug. I had not seen this use of InstanceEditor before.

This is how I would do it instead:

from traits.api    \
    import HasStrictTraits, Int, Instance, List, Regex, Str, Property, Dict
from traitsui.api \
    import View, Item, InstanceEditor, EnumEditor, ListEditor

class Person ( HasStrictTraits ):
    name  = Str
    age   = Int
    phone = Regex( value = '000-0000',
                   regex = '\d\d\d[-]\d\d\d\d' )

    traits_view = View( 'name', 'age', 'phone' )

people = [
  Person( name = 'Dave',   age = 39, phone = '555-1212' ),
  Person( name = 'Mike',   age = 28, phone = '555-3526' ),
  Person( name = 'Joe',    age = 34, phone = '555-6943' ),
  Person( name = 'Tom',    age = 22, phone = '555-7586' ),
  Person( name = 'Dick',   age = 63, phone = '555-3895' ),
  Person( name = 'Harry',  age = 46, phone = '555-3285' ),
  Person( name = 'Sally',  age = 43, phone = '555-8797' ),
  Person( name = 'Fields', age = 31, phone = '555-3547' )
]

class Team ( HasStrictTraits ):

    name    = Str

    roster  = List( Person )

    captain = Instance(Person)

    def _captain_default(self):
        return self.roster[0]

    person_to_name = Property(Dict, depends_on='roster.name')

    def _get_person_to_name(self):
        return {p: p.name for p in self.roster}
                            
    def default_traits_view(self):
        traits_view = View( Item('name'),
                            Item('_'),
                            Item('captain', editor=EnumEditor(name='person_to_name')),
                            Item('captain', editor=InstanceEditor(), style='custom'),
                            buttons = ['OK'])
        return traits_view

if __name__ == '__main__':
    team = Team(name='Vultures', roster=people)
    team.configure_traits()

I'll file a ticket in the meanwhile.
Best,
Pietro



On Fri, Feb 14, 2014 at 10:29 AM, Neil Hodgson <[hidden email]> wrote:

In the traitsui documentation there is the example below of editing a list of Instances.

When the View is constructed it seems to be using the name attribute on the Person to populate the drop down of list contents.

However if you edit the name trait on a Person instance then you can no longer edit the Person, because the drop down is not updated to reflect the new name.  I assume there is a lookup somewhere that is not updated. 

Seems like a bug, or at least not 'intuitive' behaviour?

Thanks
Neil
# instance_editor_selection.py -- Example of an instance editor
#                                 with instance selection

from traits.api    \
    import HasStrictTraits, Int, Instance, List, Regex, Str
from traitsui.api \
    import View, Item, InstanceEditor

class Person ( HasStrictTraits ):
    name  = Str
    age   = Int
    phone = Regex( value = '000-0000',
                   regex = '\d\d\d[-]\d\d\d\d' )

    traits_view = View( 'name', 'age', 'phone' )

people = [
  Person( name = 'Dave',   age = 39, phone = '555-1212' ),
  Person( name = 'Mike',   age = 28, phone = '555-3526' ),
  Person( name = 'Joe',    age = 34, phone = '555-6943' ),
  Person( name = 'Tom',    age = 22, phone = '555-7586' ),
  Person( name = 'Dick',   age = 63, phone = '555-3895' ),
  Person( name = 'Harry',  age = 46, phone = '555-3285' ),
  Person( name = 'Sally',  age = 43, phone = '555-8797' ),
  Person( name = 'Fields', age = 31, phone = '555-3547' )
]

class Team ( HasStrictTraits ):

    name    = Str
    captain = Instance( Person )
    roster  = List( Person )

    traits_view = View( Item('name'),
                        Item('_'),
                        Item( 'captain',
                              label='Team Captain',
                              editor =
                                  InstanceEditor( name = 'roster',
                                                  editable = True),
                              style = 'custom',
                             ),
                        buttons = ['OK'])

if __name__ == '__main__':
    Team( name    = 'Vultures',
          captain = people[0],
          roster  = people ).configure_traits()

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev




--
Pietro Berkes
Scientific software developer
Enthought UK


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev