Sam Wedge | 10 Feb 16:58

Display of values within object

Hi,

I have a view defined in a Controller class. On this view, I would like to display two integers: one from
my_model, and one from an instance of an object residing in my_model.

The code below demonstrates what I am trying to achieve, except that Item('baz') currently points to an
instance of an object, rather than the integer residing within the instance.

I thought that a dot notation might work here, as in: Item('baz.foo'), but no such luck.

This is just a simplified version of my full application. It would not be useful to put a View() within the
my_object class, as I would like to be able to display different values from my_object in different places
of my UI.

Is there something like baz.foo that I can use?

Thanks for your time.

Regards,

Sam

from traits.api import HasTraits, Int, Instance
from traitsui.api import View, Item, Controller

class my_object(HasTraits):
    foo = Int(5)

class my_model(HasTraits):
    bar = Int(10)
(Continue reading)

Jonathan March | 10 Feb 17:06
Gravatar

Re: Display of values within object

Sam,

You are very close. In your sample code, replace
     Item('baz'),
with

    Item('object.baz.foo'),

-- Jonathan

On Fri, Feb 10, 2012 at 9:58 AM, Sam Wedge <sam.wedge@...> wrote:

> from traits.api import HasTraits, Int, Instance
> from traitsui.api import View, Item, Controller
>
> class my_object(HasTraits):
>    foo = Int(5)
>
> class my_model(HasTraits):
>    bar = Int(10)
>    baz = Instance(my_object)
>
>    def __init__(self):
>        self.baz = my_object()
>
> class controller(Controller):
>    traits_view = View(
>                Item('bar'),
>                Item('baz'),
>            )
(Continue reading)

Sam Wedge | 10 Feb 17:12

Re: Display of values within object

Jonathan,

Thank you so much. I saw that in the help files online, but assumed that "object" was a placeholder for the
name of my object. I feel a fool now!

Thanks again,

Sam

>>> Jonathan March <jmarch@...> 10/02/2012 16:06 >>>
Sam,

You are very close. In your sample code, replace
     Item('baz'),
with

    Item('object.baz.foo'),

-- Jonathan

On Fri, Feb 10, 2012 at 9:58 AM, Sam Wedge <sam.wedge@...> wrote:

> from traits.api import HasTraits, Int, Instance
> from traitsui.api import View, Item, Controller
>
> class my_object(HasTraits):
>    foo = Int(5)
>
> class my_model(HasTraits):
>    bar = Int(10)
(Continue reading)

Robert Kern | 10 Feb 17:17
Gravatar

Re: Display of values within object

On Fri, Feb 10, 2012 at 4:12 PM, Sam Wedge <sam.wedge@...> wrote:
> Jonathan,
>
> Thank you so much. I saw that in the help files online, but assumed that "object" was a placeholder for the
name of my object. I feel a fool now!

You shouldn't. It's an admittedly confusing feature that was bolted on
relatively late in Traits UI's lifespan.

--

-- 
Robert Kern
Enthought
Jonathan March | 10 Feb 17:23
Gravatar

Re: Display of values within object

On Fri, Feb 10, 2012 at 10:17 AM, Robert Kern <rkern@...> wrote:

> On Fri, Feb 10, 2012 at 4:12 PM, Sam Wedge <sam.wedge@...> wrote:
> > Jonathan,
> >
> > Thank you so much. I saw that in the help files online, but assumed that
> "object" was a placeholder for the name of my object. I feel a fool now!
>
> You shouldn't. It's an admittedly confusing feature that was bolted on
> relatively late in Traits UI's lifespan.
>

We will make the docs clearer.
Sam Wedge | 13 Mar 17:20

Re: Display of values within object

Hi,

This is really an extension question of a previous question I asked last month.

I have a class "my_model" in which is defined an instance of "my_object". This is displayed on a view,
defined in "controller".

If I generate the instance of "my_object" within an "__init__" function, everything is hunky-dory, and I
can see my "foo" value displayed in the view.

Supposing I want to display that value only when a button is pushed: I can use "visible_when" in the View. But
if I also want to generate the instance of "my_object" when the button is pushed, I get all kinds of
problems, because the view is presumable trying to do something with "foo" before it exists, despite it
not being visible.

Are there any ideas for a way forward? I guess I am looking for something like enabled_when, but that gets
used beyond the initial view set-up (just like visible_when).

Example code below.

Your insight would be much appreciated.

Regards,

Sam

====================================================================

from traits.api import HasTraits, Int, Instance, Button, Bool
from traitsui.api import View, Item, Controller
(Continue reading)

Brennan Williams | 14 Mar 23:51

Re: Display of values within object

On 14/03/2012 5:20 a.m., Sam Wedge wrote:
> Hi,
>
> This is really an extension question of a previous question I asked last month.
>
> I have a class "my_model" in which is defined an instance of "my_object". This is displayed on a view,
defined in "controller".
>
> If I generate the instance of "my_object" within an "__init__" function, everything is hunky-dory, and I
can see my "foo" value displayed in the view.
>
> Supposing I want to display that value only when a button is pushed: I can use "visible_when" in the View.
But if I also want to generate the instance of "my_object" when the button is pushed, I get all kinds of
problems, because the view is presumable trying to do something with "foo" before it exists, despite it
not being visible.
>
> Are there any ideas for a way forward? I guess I am looking for something like enabled_when, but that gets
used beyond the initial view set-up (just like visible_when).
I'm not sure what the correct way to sort this out is but I can tell you 
what I have done.

If you read the Traits documentation (github.enthought.com/traits and 
github.enthought.com/traitsui) then it specifically states ( 
http://github.enthought.com/traitsui/traitsui_user_manual/advanced_view.html 
) that you should "avoid extended trait references where one of the 
intermediate objects could be None, because there is no way to obtain a 
valid reference from None".

Of course I read that about two years too late.

(Continue reading)


Gmane