Context menu for lists.

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

Context menu for lists.

Jordan Ilott-2
I'm trying to add a context menu to a ListStrEditor. I've found that I can add the menu by setting the menu parameter of the editor (ListStrEditor(menu = my_menu). The problem that I have is that I can't set the handler (attempting to pass handler = my_handler to the editor produces an error) and I'm having trouble understanding what is being executed as the handler.

I couldn't find any examples of this except in the case of tables. Has anybody done this before?

Jordan

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

Re: Context menu for lists.

Jordan Ilott-2
With a little more time reading the code and using the debugger, I was able to answer my own question. The handler that is used is the handler assigned to the view. Because I was explicitly setting the context menu when instantiating the editor, I assumed that it would have an explicit handler as well.  The action given in the menu definition will be call as: my_action(info,selection) where info is a UIInfo object. Perhaps this was obvious to most, but maybe it will help someone else one day.

Jordan


On Sun, Apr 14, 2013 at 10:21 PM, Jordan Ilott <[hidden email]> wrote:
I'm trying to add a context menu to a ListStrEditor. I've found that I can add the menu by setting the menu parameter of the editor (ListStrEditor(menu = my_menu). The problem that I have is that I can't set the handler (attempting to pass handler = my_handler to the editor produces an error) and I'm having trouble understanding what is being executed as the handler.

I couldn't find any examples of this except in the case of tables. Has anybody done this before?

Jordan


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

Re: Context menu for lists.

Jordan Ilott-2
I believe I spoke too soon. Inside of my handler method, I'm trying to access: info.ui.context.object.my_trait . Using the debugger(with a break point in the handler method), I can see the trait and the value that I'm interested in, but when I try to access it in the handler method, I get an error.

AttributeError: 'weakref' object has no attribute 'my_trait'

Can anyone offer some advice? I don't understand why I should be able to see the attribute in the debugger but not access it.

Thanks.

Jordan


On Mon, Apr 15, 2013 at 8:09 AM, Jordan Ilott <[hidden email]> wrote:
With a little more time reading the code and using the debugger, I was able to answer my own question. The handler that is used is the handler assigned to the view. Because I was explicitly setting the context menu when instantiating the editor, I assumed that it would have an explicit handler as well.  The action given in the menu definition will be call as: my_action(info,selection) where info is a UIInfo object. Perhaps this was obvious to most, but maybe it will help someone else one day.

Jordan


On Sun, Apr 14, 2013 at 10:21 PM, Jordan Ilott <[hidden email]> wrote:
I'm trying to add a context menu to a ListStrEditor. I've found that I can add the menu by setting the menu parameter of the editor (ListStrEditor(menu = my_menu). The problem that I have is that I can't set the handler (attempting to pass handler = my_handler to the editor produces an error) and I'm having trouble understanding what is being executed as the handler.

I couldn't find any examples of this except in the case of tables. Has anybody done this before?

Jordan



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

Re: Context menu for lists.

Didrik Pinte-2
On 15 April 2013 15:47, Jordan Ilott <[hidden email]> wrote:

> I believe I spoke too soon. Inside of my handler method, I'm trying to
> access: info.ui.context.object.my_trait . Using the debugger(with a break
> point in the handler method), I can see the trait and the value that I'm
> interested in, but when I try to access it in the handler method, I get an
> error.
>
> AttributeError: 'weakref' object has no attribute 'my_trait'
>
> Can anyone offer some advice? I don't understand why I should be able to see
> the attribute in the debugger but not access it.

What you access is a weakref to the HasTraits instance. You need to
call it to get to the instance itself.

if you try youref().my_trait, it should work.

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

Re: Context menu for lists.

Robert Kern
On Wed, Apr 17, 2013 at 1:51 PM, Didrik Pinte <[hidden email]> wrote:

> On 15 April 2013 15:47, Jordan Ilott <[hidden email]> wrote:
>> I believe I spoke too soon. Inside of my handler method, I'm trying to
>> access: info.ui.context.object.my_trait . Using the debugger(with a break
>> point in the handler method), I can see the trait and the value that I'm
>> interested in, but when I try to access it in the handler method, I get an
>> error.
>>
>> AttributeError: 'weakref' object has no attribute 'my_trait'
>>
>> Can anyone offer some advice? I don't understand why I should be able to see
>> the attribute in the debugger but not access it.
>
> What you access is a weakref to the HasTraits instance. You need to
> call it to get to the instance itself.
>
> if you try youref().my_trait, it should work.

Actually, the problem is that info.ui.context is a dict, specifically
a TraitDictObject. Jordan needs to access
info.ui.context['object'].my_trait . info.ui.context.object is a
weakref back to info.ui, not what you want.

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