Enum usage question

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

Enum usage question

Pearu Peterson
Hi,

Is it possible to change Enum traits values in runtime? For example,
when reading a file
that contains a list of strings (each file may have different list)
that I would like to make
selectable from traits ui. As I understand, Enum values can be
specified in class definitions
but in my case the values are not known at that time.
To make the example explicit:

class AFile(HasTraits):
    filename = File
    is_loaded = Bool(False)
    selected_string = Enum('<selection>')

    traits_view = View( Group(Item(name='filename')),
                                Group(Item(name='selected_string',
visible_when='is_loaded'))

    @on_trait_change('filename')
    def on_filename_selection(self):
        string_list = <read string_list from file <filename>>
        # my wish: set string_list as allowed values to selected_string Enum
        self.is_loaded = True

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

Re: Enum usage question

Peter Wang
On Thu, Nov 11, 2010 at 7:12 AM, Pearu Peterson
<[hidden email]> wrote:
> Hi,
>
> Is it possible to change Enum traits values in runtime?

Yep!  It's very easy:

from enthought.traits.api import *
from enthought.traits.ui.api import *

class Foo(HasTraits):
    names = List(["Peter", "Pearu", "Robert", "Eric"])
    selected_person = Enum(values="names")

    traits_view = View("names", "selected_person", resizable=True)

    def _selected_person_changed(self):
        print "new selected person:", self.selected_person

Foo().configure_traits()


Note: Click the blue icon to the left of the list elements to add new
elements or remove existing ones.


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

Re: Enum usage question

Corran Webster
In reply to this post by Pearu Peterson


On Thu, Nov 11, 2010 at 7:12 AM, Pearu Peterson <[hidden email]> wrote:
Hi,

Is it possible to change Enum traits values in runtime? For example,
when reading a file
that contains a list of strings (each file may have different list)
that I would like to make
selectable from traits ui. As I understand, Enum values can be
specified in class definitions
but in my case the values are not known at that time.
To make the example explicit:

class AFile(HasTraits):
   filename = File
   is_loaded = Bool(False)
   selected_string = Enum('<selection>')

   traits_view = View( Group(Item(name='filename')),
                               Group(Item(name='selected_string',
visible_when='is_loaded'))

   @on_trait_change('filename')
   def on_filename_selection(self):
       string_list = <read string_list from file <filename>>
       # my wish: set string_list as allowed values to selected_string Enum
       self.is_loaded = True


Hi Pearu,

you can use the value metadata in the Enum trait definition to point to another trait that holds the valid values, like so:

class Test(HasTraits):
     l = List([1, 2, 3])
     e = Enum(values='l')

You can then change the list trait, and the valid set of Enums will change.  So in your example:

class AFile(HasTraits):
   filename = File
   is_loaded = Bool(False)
   string_list = List([''])
   selected_string = Enum(values='string_list')

   traits_view = View( Group(Item(name='filename')),
                               Group(Item(name='selected_string', visible_when='is_loaded'))

   @on_trait_change('filename')
   def on_filename_selection(self):
       self.string_list = <read string_list from file <filename>>
       # my wish: set string_list as allowed values to selected_string Enum
       self.is_loaded = True

Another, possibly better, approach may be to just have the selected_string be a string, and restrict the possibilities through the Editor you use.  You could do:

traits_view = View( Group(Item(name='filename')),
                               Group(Item(name='selected_string', visible_when='is_loaded',
                               editor=EnumEditor(name='string_list')
))

I think (you might need to double-check that name is the right argument to use).

-- Corran

 

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

Re: Enum usage question

Pearu Peterson
Peter and Corran, thanks for the tips! traits is a very powerful
technology indeed:)
I actually ended up to a combinations of your suggestions:

...
    available_strings = List(Str)
    selected_strings = List(editor = CheckListEditor(name =
'available_strings'))

    traits_view = View(Group(Item(name = 'filename'),
                                         Item(name =
'selected_strings', style='custom', visible_when = 'is_loaded')))
    @on_trait_change('filename')
    def on_filename_selection(self):
        self.available_strings = <read string_list from file <filename>>
        self.is_loaded = True
...

Thanks again and best regards,
Pearu

On Thu, Nov 11, 2010 at 5:42 PM, Corran Webster <[hidden email]> wrote:

>
>
> On Thu, Nov 11, 2010 at 7:12 AM, Pearu Peterson <[hidden email]>
> wrote:
>>
>> Hi,
>>
>> Is it possible to change Enum traits values in runtime? For example,
>> when reading a file
>> that contains a list of strings (each file may have different list)
>> that I would like to make
>> selectable from traits ui. As I understand, Enum values can be
>> specified in class definitions
>> but in my case the values are not known at that time.
>> To make the example explicit:
>>
>> class AFile(HasTraits):
>>    filename = File
>>    is_loaded = Bool(False)
>>    selected_string = Enum('<selection>')
>>
>>    traits_view = View( Group(Item(name='filename')),
>>                                Group(Item(name='selected_string',
>> visible_when='is_loaded'))
>>
>>    @on_trait_change('filename')
>>    def on_filename_selection(self):
>>        string_list = <read string_list from file <filename>>
>>        # my wish: set string_list as allowed values to selected_string
>> Enum
>>        self.is_loaded = True
>>
>
> Hi Pearu,
>
> you can use the value metadata in the Enum trait definition to point to
> another trait that holds the valid values, like so:
>
> class Test(HasTraits):
>      l = List([1, 2, 3])
>      e = Enum(values='l')
>
> You can then change the list trait, and the valid set of Enums will change.
>  So in your example:
>
> class AFile(HasTraits):
>    filename = File
>    is_loaded = Bool(False)
>    string_list = List([''])
>    selected_string = Enum(values='string_list')
>
>    traits_view = View( Group(Item(name='filename')),
>                                Group(Item(name='selected_string',
> visible_when='is_loaded'))
>>
>>    @on_trait_change('filename')
>>    def on_filename_selection(self):
>>        self.string_list = <read string_list from file <filename>>
>>        # my wish: set string_list as allowed values to selected_string
>> Enum
>>        self.is_loaded = True
>
> Another, possibly better, approach may be to just have the selected_string
> be a string, and restrict the possibilities through the Editor you use.  You
> could do:
>
> traits_view = View( Group(Item(name='filename')),
>                                Group(Item(name='selected_string',
> visible_when='is_loaded',
>                                editor=EnumEditor(name='string_list')
> ))
>
> I think (you might need to double-check that name is the right argument to
> use).
>
> -- Corran
>
>
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
>
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev