Understanding dynamic views in traitsui

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

Understanding dynamic views in traitsui

Richard Graham
Hi all,

I'm getting started with traits and am having some difficulty
understanding dynamic views. I am trying to make a simple example where
some check boxes are dynamically added to a panel. I would also like a
callback when one of the values in changed. The following code (which
doesn't work) should illustrate what I am trying to do

switch_names = ['Red', 'Green', 'Blue']

class SwitchHandler(Handler):
    def setattr(self, info, object, name, value):
        print 'switch', name, 'changed to', str(value)

class Switches(HasDynamicViews):
    def __init__(self):

        view = DynamicView(name = 'all_switches')

        for s in switch_names:
            view.add_trait(s, Bool())

        view.handler = SwitchHandler()

        self.declare_dynamic_view( view )

class ManualPanel(SingletonHasTraits):
    switches = Instance(Switches, ())
    view = View(Item('switches', show_label=False))

If anyone could point out what I am misunderstanding here or post a
similar simple example I would appreciate it.

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

Re: Understanding dynamic views in traitsui

Pierre Haessig
Hi Richard,

I'm not familiar with DynamicView, but I played a bit with dynamically
adding traits some months ago. I ran into an issue
https://github.com/enthought/traits/issues/84 but I didn't get any
feedback on it. So I don't know if there is a bug with using add_traits,
or if I just misunderstood how to use it.

best,
Pierre


Le 19/03/2014 23:43, Richard Graham a écrit :

> Hi all,
>
> I'm getting started with traits and am having some difficulty
> understanding dynamic views. I am trying to make a simple example where
> some check boxes are dynamically added to a panel. I would also like a
> callback when one of the values in changed. The following code (which
> doesn't work) should illustrate what I am trying to do
>
> switch_names = ['Red', 'Green', 'Blue']
>
> class SwitchHandler(Handler):
>     def setattr(self, info, object, name, value):
>         print 'switch', name, 'changed to', str(value)
>
> class Switches(HasDynamicViews):
>     def __init__(self):
>
>         view = DynamicView(name = 'all_switches')
>
>         for s in switch_names:
>             view.add_trait(s, Bool())
>
>         view.handler = SwitchHandler()
>
>         self.declare_dynamic_view( view )
>
> class ManualPanel(SingletonHasTraits):
>     switches = Instance(Switches, ())
>     view = View(Item('switches', show_label=False))
>
> If anyone could point out what I am misunderstanding here or post a
> similar simple example I would appreciate it.
>
> Regards,
> --
> Richard Graham
> _______________________________________________
> 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
Reply | Threaded
Open this post in threaded view
|

Re: Understanding dynamic views in traitsui

Tim Diller
I'm not sure if this is quite what you had in mind, but here's how I would go about implementing what I think you're after.
HTH.
-Tim

from traits.api import (HasTraits, Bool, Event, List, Instance, Str, 
                        on_trait_change)
from traitsui.api import View, Item, ButtonEditor, CheckListEditor, ListEditor


class Foo(HasTraits):
    checklist = List(Str)
    selection = List
    new_name = Str
    add_item = Event
    
    traits_view = View(
        Item("new_name", label="Name of item to add",),
        Item("add_item", editor=ButtonEditor(label="Add"),
             show_label=False),
        Item("selection",
             editor=CheckListEditor(name="checklist"),
             style="custom"),
        Item("selection",
             editor=ListEditor(style="readonly"),
             style="readonly"),
        resizable=True,
    )
    
    def _add_item_fired(self):
        self.checklist.append(self.new_name)
    
    @on_trait_change("selection")
    def handler(self, name, new):
        print "Item '{}' just changed to {}".format(name, new)


if __name__ == "__main__":
    f = Foo()
    f.configure_traits()


Tim Diller, Ph.D.
Scientific Software Developer
Enthought, Ltd

Broers Building
21 J.J. Thomson Avenue
Cambridge, UK
CB3 0FA

UK +44 7583 843 072
US +1 512 436 3455


--
The contents of this email are privileged and confidential. If you have received it in error, please notify us at once by reply and delete the email.


On Thu, Mar 20, 2014 at 8:47 AM, Pierre Haessig <[hidden email]> wrote:
Hi Richard,

I'm not familiar with DynamicView, but I played a bit with dynamically
adding traits some months ago. I ran into an issue
https://github.com/enthought/traits/issues/84 but I didn't get any
feedback on it. So I don't know if there is a bug with using add_traits,
or if I just misunderstood how to use it.

best,
Pierre


Le 19/03/2014 23:43, Richard Graham a écrit :
> Hi all,
>
> I'm getting started with traits and am having some difficulty
> understanding dynamic views. I am trying to make a simple example where
> some check boxes are dynamically added to a panel. I would also like a
> callback when one of the values in changed. The following code (which
> doesn't work) should illustrate what I am trying to do
>
> switch_names = ['Red', 'Green', 'Blue']
>
> class SwitchHandler(Handler):
>     def setattr(self, info, object, name, value):
>         print 'switch', name, 'changed to', str(value)
>
> class Switches(HasDynamicViews):
>     def __init__(self):
>
>         view = DynamicView(name = 'all_switches')
>
>         for s in switch_names:
>             view.add_trait(s, Bool())
>
>         view.handler = SwitchHandler()
>
>         self.declare_dynamic_view( view )
>
> class ManualPanel(SingletonHasTraits):
>     switches = Instance(Switches, ())
>     view = View(Item('switches', show_label=False))
>
> If anyone could point out what I am misunderstanding here or post a
> similar simple example I would appreciate it.
>
> Regards,
> --
> Richard Graham
> _______________________________________________
> 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


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

Re: Understanding dynamic views in traitsui

Richard Graham
Hi Tim,

Thank you for that example. I hadn't seen the CheckListEditor. A
different approach to what I had in mind but quite suitable for my
immediate needs.

I am curious as to how this would be best implemented in a more
complicated case, such as if each check box were to be accompanied by a
a slider and other controls. Is writing a custom editor the best way to
go there? It seems like that approach allows the same functionality as
dynamic views and I was wondering what the relationship is between them.

Thanks again, regards,
--
Richard Graham


On 03/20/2014 03:20 AM, Tim Diller wrote:

> I'm not sure if this is quite what you had in mind, but here's how I
> would go about implementing what I think you're after.
> HTH.
> -Tim
>
> from traits.api import (HasTraits, Bool, Event, List, Instance, Str,
>                         on_trait_change)
> from traitsui.api import View, Item, ButtonEditor, CheckListEditor,
> ListEditor
>
>
> class Foo(HasTraits):
>     checklist = List(Str)
>     selection = List
>     new_name = Str
>     add_item = Event
>    
>     traits_view = View(
>         Item("new_name", label="Name of item to add",),
>         Item("add_item", editor=ButtonEditor(label="Add"),
>              show_label=False),
>         Item("selection",
>              editor=CheckListEditor(name="checklist"),
>              style="custom"),
>         Item("selection",
>              editor=ListEditor(style="readonly"),
>              style="readonly"),
>         resizable=True,
>     )
>    
>     def _add_item_fired(self):
>         self.checklist.append(self.new_name)
>    
>     @on_trait_change("selection")
>     def handler(self, name, new):
>         print "Item '{}' just changed to {}".format(name, new)
>
>
> if __name__ == "__main__":
>     f = Foo()
>     f.configure_traits()
>
>
> Tim Diller, Ph.D.
> /Scientific Software Developer/
> Enthought, Ltd
>
> Broers Building
> 21 J.J. Thomson Avenue
> Cambridge, UK
> CB3 0FA
>
> UK +44 7583 843 072
> US +1 512 436 3455
>
> --
> The contents of this email are privileged and confidential. If you have
> received it in error, please notify us at once by reply and delete the
> email.
>
>
> On Thu, Mar 20, 2014 at 8:47 AM, Pierre Haessig
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Hi Richard,
>
>     I'm not familiar with DynamicView, but I played a bit with dynamically
>     adding traits some months ago. I ran into an issue
>     https://github.com/enthought/traits/issues/84 but I didn't get any
>     feedback on it. So I don't know if there is a bug with using add_traits,
>     or if I just misunderstood how to use it.
>
>     best,
>     Pierre
>
>
>     Le 19/03/2014 23:43, Richard Graham a écrit :
>     > Hi all,
>     >
>     > I'm getting started with traits and am having some difficulty
>     > understanding dynamic views. I am trying to make a simple example
>     where
>     > some check boxes are dynamically added to a panel. I would also like a
>     > callback when one of the values in changed. The following code (which
>     > doesn't work) should illustrate what I am trying to do
>     >
>     > switch_names = ['Red', 'Green', 'Blue']
>     >
>     > class SwitchHandler(Handler):
>     >     def setattr(self, info, object, name, value):
>     >         print 'switch', name, 'changed to', str(value)
>     >
>     > class Switches(HasDynamicViews):
>     >     def __init__(self):
>     >
>     >         view = DynamicView(name = 'all_switches')
>     >
>     >         for s in switch_names:
>     >             view.add_trait(s, Bool())
>     >
>     >         view.handler = SwitchHandler()
>     >
>     >         self.declare_dynamic_view( view )
>     >
>     > class ManualPanel(SingletonHasTraits):
>     >     switches = Instance(Switches, ())
>     >     view = View(Item('switches', show_label=False))
>     >
>     > If anyone could point out what I am misunderstanding here or post a
>     > similar simple example I would appreciate it.
>     >
>     > Regards,
>     > --
>     > Richard Graham
>     > _______________________________________________
>     > Enthought-Dev mailing list
>     > [hidden email]
>     <mailto:[hidden email]>
>     > https://mail.enthought.com/mailman/listinfo/enthought-dev
>
>     _______________________________________________
>     Enthought-Dev mailing list
>     [hidden email]
>     <mailto:[hidden email]>
>     https://mail.enthought.com/mailman/listinfo/enthought-dev
>
>
>
>
> _______________________________________________
> 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