TraitsUI ButtonEditor with image

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

TraitsUI ButtonEditor with image

Tiago Pereira
Hi,

A small question: how do I use an image label for a ButtonEditor()
factory? The documentation shows 'image' as a possible keyword, but
digging through the code and examples I could not find how to do it. How
do I use an image from a file?

The closest I could find was the ImageEnumEditor demo, which seems to
use images in buttons. But it seems to use cryptic arguments like
'suffix', 'prefix' , 'klass' arguments, so I could not follow how (what
I think are) the pyface icon files are actually used.

Also, are there any more icons in traitsui/pyface/etc than the 28
provided in pyface?

Thanks!

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

Re: TraitsUI ButtonEditor with image

Robert Kern
On Thu, Jul 12, 2012 at 1:40 AM, Tiago Pereira <[hidden email]> wrote:
> Hi,
>
> A small question: how do I use an image label for a ButtonEditor()
> factory? The documentation shows 'image' as a possible keyword, but
> digging through the code and examples I could not find how to do it. How
> do I use an image from a file?

Let's say you have a module `mypackage/mymodule.py` that will be
defining the ButtonEditor. Make a directory alongside it named
`images`, i.e. `mypackage/images/`. Put the image file there,
preferably a .png. Let's call it `foo.png`.

In `mymodule.py`, do the following

  from pyface.image_resource import ImageResource

  class MyClass(HasTraits):
    my_button = Button()

    traits_view = View(
      Item('my_button', editor=ButtonEditor(image=ImageResource('foo'))),
    )

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

Re: TraitsUI ButtonEditor with image

Jaidev Deshpande
On Thu, Jul 12, 2012 at 6:40 PM, Robert Kern <[hidden email]> wrote:

> On Thu, Jul 12, 2012 at 1:40 AM, Tiago Pereira <[hidden email]> wrote:
>> Hi,
>>
>> A small question: how do I use an image label for a ButtonEditor()
>> factory? The documentation shows 'image' as a possible keyword, but
>> digging through the code and examples I could not find how to do it. How
>> do I use an image from a file?
>
> Let's say you have a module `mypackage/mymodule.py` that will be
> defining the ButtonEditor. Make a directory alongside it named
> `images`, i.e. `mypackage/images/`. Put the image file there,
> preferably a .png. Let's call it `foo.png`.

Is it necessary to create the /images directory? Will it work if the
name attribute of the ImageResource is the path to the image?

>
> In `mymodule.py`, do the following
>
>   from pyface.image_resource import ImageResource
>
>   class MyClass(HasTraits):
>     my_button = Button()
>
>     traits_view = View(
>       Item('my_button', editor=ButtonEditor(image=ImageResource('foo'))),
>     )

Also, the Button trait has an image attribute. Can the ImageResource
object be passed to it?

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

Re: TraitsUI ButtonEditor with image

Robert Kern
On Thu, Jul 12, 2012 at 2:49 PM, Jaidev Deshpande
<[hidden email]> wrote:

> On Thu, Jul 12, 2012 at 6:40 PM, Robert Kern <[hidden email]> wrote:
>> On Thu, Jul 12, 2012 at 1:40 AM, Tiago Pereira <[hidden email]> wrote:
>>> Hi,
>>>
>>> A small question: how do I use an image label for a ButtonEditor()
>>> factory? The documentation shows 'image' as a possible keyword, but
>>> digging through the code and examples I could not find how to do it. How
>>> do I use an image from a file?
>>
>> Let's say you have a module `mypackage/mymodule.py` that will be
>> defining the ButtonEditor. Make a directory alongside it named
>> `images`, i.e. `mypackage/images/`. Put the image file there,
>> preferably a .png. Let's call it `foo.png`.
>
> Is it necessary to create the /images directory? Will it work if the
> name attribute of the ImageResource is the path to the image?

No. You can pass in search_path=['/absolute/path/to/images/'] in
addition to the name if you like.

>> In `mymodule.py`, do the following
>>
>>   from pyface.image_resource import ImageResource
>>
>>   class MyClass(HasTraits):
>>     my_button = Button()
>>
>>     traits_view = View(
>>       Item('my_button', editor=ButtonEditor(image=ImageResource('foo'))),
>>     )
>
> Also, the Button trait has an image attribute. Can the ImageResource
> object be passed to it?

Yes. In that case, do not specify editor=ButtonEditor() in the Item.

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

Re: TraitsUI ButtonEditor with image

Jaidev Deshpande
On Thu, Jul 12, 2012 at 7:23 PM, Robert Kern <[hidden email]> wrote:

> On Thu, Jul 12, 2012 at 2:49 PM, Jaidev Deshpande
> <[hidden email]> wrote:
>> On Thu, Jul 12, 2012 at 6:40 PM, Robert Kern <[hidden email]> wrote:
>>> On Thu, Jul 12, 2012 at 1:40 AM, Tiago Pereira <[hidden email]> wrote:
>>>> Hi,
>>>>
>>>> A small question: how do I use an image label for a ButtonEditor()
>>>> factory? The documentation shows 'image' as a possible keyword, but
>>>> digging through the code and examples I could not find how to do it. How
>>>> do I use an image from a file?
>>>
>>> Let's say you have a module `mypackage/mymodule.py` that will be
>>> defining the ButtonEditor. Make a directory alongside it named
>>> `images`, i.e. `mypackage/images/`. Put the image file there,
>>> preferably a .png. Let's call it `foo.png`.
>>
>> Is it necessary to create the /images directory? Will it work if the
>> name attribute of the ImageResource is the path to the image?
>
> No. You can pass in search_path=['/absolute/path/to/images/'] in
> addition to the name if you like.

Okay, so the 'name' attribute has to be only the name of the file?

>
>>> In `mymodule.py`, do the following
>>>
>>>   from pyface.image_resource import ImageResource
>>>
>>>   class MyClass(HasTraits):
>>>     my_button = Button()
>>>
>>>     traits_view = View(
>>>       Item('my_button', editor=ButtonEditor(image=ImageResource('foo'))),
>>>     )
>>
>> Also, the Button trait has an image attribute. Can the ImageResource
>> object be passed to it?
>
> Yes. In that case, do not specify editor=ButtonEditor() in the Item.
>
> --
> Robert Kern
> Enthought
> _______________________________________________
> 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: TraitsUI ButtonEditor with image

Robert Kern
On Thu, Jul 12, 2012 at 2:56 PM, Jaidev Deshpande
<[hidden email]> wrote:

> On Thu, Jul 12, 2012 at 7:23 PM, Robert Kern <[hidden email]> wrote:
>> On Thu, Jul 12, 2012 at 2:49 PM, Jaidev Deshpande
>> <[hidden email]> wrote:
>>> On Thu, Jul 12, 2012 at 6:40 PM, Robert Kern <[hidden email]> wrote:
>>>> On Thu, Jul 12, 2012 at 1:40 AM, Tiago Pereira <[hidden email]> wrote:
>>>>> Hi,
>>>>>
>>>>> A small question: how do I use an image label for a ButtonEditor()
>>>>> factory? The documentation shows 'image' as a possible keyword, but
>>>>> digging through the code and examples I could not find how to do it. How
>>>>> do I use an image from a file?
>>>>
>>>> Let's say you have a module `mypackage/mymodule.py` that will be
>>>> defining the ButtonEditor. Make a directory alongside it named
>>>> `images`, i.e. `mypackage/images/`. Put the image file there,
>>>> preferably a .png. Let's call it `foo.png`.
>>>
>>> Is it necessary to create the /images directory? Will it work if the
>>> name attribute of the ImageResource is the path to the image?
>>
>> No. You can pass in search_path=['/absolute/path/to/images/'] in
>> addition to the name if you like.
>
> Okay, so the 'name' attribute has to be only the name of the file?

Actually, it looks like it could be a relative path from the directory
of the module instantiating the ImageResource().

https://github.com/enthought/pyface/blob/master/pyface/resource/resource_manager.py#L87

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

Re: TraitsUI ButtonEditor with image

Brennan Williams
On 12/07/2012 3:01 p.m., Robert Kern wrote:

> On Thu, Jul 12, 2012 at 2:56 PM, Jaidev Deshpande
> <[hidden email]> wrote:
>> On Thu, Jul 12, 2012 at 7:23 PM, Robert Kern <[hidden email]> wrote:
>>> On Thu, Jul 12, 2012 at 2:49 PM, Jaidev Deshpande
>>> <[hidden email]> wrote:
>>>> On Thu, Jul 12, 2012 at 6:40 PM, Robert Kern <[hidden email]> wrote:
>>>>> On Thu, Jul 12, 2012 at 1:40 AM, Tiago Pereira <[hidden email]> wrote:
>>>>>> Hi,
>>>>>>
>>>>>> A small question: how do I use an image label for a ButtonEditor()
>>>>>> factory? The documentation shows 'image' as a possible keyword, but
>>>>>> digging through the code and examples I could not find how to do it. How
>>>>>> do I use an image from a file?
>>>>> Let's say you have a module `mypackage/mymodule.py` that will be
>>>>> defining the ButtonEditor. Make a directory alongside it named
>>>>> `images`, i.e. `mypackage/images/`. Put the image file there,
>>>>> preferably a .png. Let's call it `foo.png`.
If you have image files with the same name but different type (e.g.
a.ico and a.png) then I think the .ico
file takes precedence over the .png file

>>>> Is it necessary to create the /images directory? Will it work if the
>>>> name attribute of the ImageResource is the path to the image?
>>> No. You can pass in search_path=['/absolute/path/to/images/'] in
>>> addition to the name if you like.
>> Okay, so the 'name' attribute has to be only the name of the file?
> Actually, it looks like it could be a relative path from the directory
> of the module instantiating the ImageResource().
>
> https://github.com/enthought/pyface/blob/master/pyface/resource/resource_manager.py#L87
>


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

Re: TraitsUI ButtonEditor with image

Tiago Pereira
In reply to this post by Robert Kern
Hi,

Thanks for the answers. I tried this:

On 12/07/12 6:10 AM, Robert Kern wrote:

> In `mymodule.py`, do the following
>
>    from pyface.image_resource import ImageResource
>
>    class MyClass(HasTraits):
>      my_button = Button()
>
>      traits_view = View(
>        Item('my_button', editor=ButtonEditor(image=ImageResource('foo'))),
>      )
>

But all my button says is 'my_button'. It has no image. There is an
images/foo.png in the same directory. I also tried passing search_path
to ButtonEditor(), but the result was the same. I tried using
ImageResource('foo.png') but that also didn't work.

Any ideas on what could be going wrong? I am using pyface, traitsui, etc
from git a few days ago.

Thanks,

Tiago

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

Re: TraitsUI ButtonEditor with image

Tiago Pereira
Found the problem. One needs to specify style='custom':

Item('my_button', editor=ButtonEditor(image=ImageResource('foo'),
style='custom)

For some reason, the ImageEditor can accept an image like image='foo',
but ButtonEditor needs image=ImageResource('foo'). This API change is
confusing.


Tiago

On 12/07/12 11:37 AM, Tiago Pereira wrote:

> Hi,
>
> Thanks for the answers. I tried this:
>
> On 12/07/12 6:10 AM, Robert Kern wrote:
>
>> In `mymodule.py`, do the following
>>
>> from pyface.image_resource import ImageResource
>>
>> class MyClass(HasTraits):
>> my_button = Button()
>>
>> traits_view = View(
>> Item('my_button', editor=ButtonEditor(image=ImageResource('foo'))),
>> )
>>
>
> But all my button says is 'my_button'. It has no image. There is an
> images/foo.png in the same directory. I also tried passing search_path
> to ButtonEditor(), but the result was the same. I tried using
> ImageResource('foo.png') but that also didn't work.
>
> Any ideas on what could be going wrong? I am using pyface, traitsui, etc
> from git a few days ago.
>
> Thanks,
>
> Tiago
>

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

Re: TraitsUI ButtonEditor with image

Jaidev Deshpande
Hi,

On Fri, Jul 13, 2012 at 2:59 AM, Tiago Pereira <[hidden email]> wrote:
> Found the problem. One needs to specify style='custom':
>
> Item('my_button', editor=ButtonEditor(image=ImageResource('foo'),
> style='custom)
>

I tried doing this but in my case the style argument can only be one
of 'button', 'checkbox', 'radio' or 'toolbar'. It's not accepting the
'custom' argument. I'm using Traits 4.2.0. What are you using?

> For some reason, the ImageEditor can accept an image like image='foo',
> but ButtonEditor needs image=ImageResource('foo'). This API change is
> confusing.
>
>
> Tiago
>
> On 12/07/12 11:37 AM, Tiago Pereira wrote:
>> Hi,
>>
>> Thanks for the answers. I tried this:
>>
>> On 12/07/12 6:10 AM, Robert Kern wrote:
>>
>>> In `mymodule.py`, do the following
>>>
>>> from pyface.image_resource import ImageResource
>>>
>>> class MyClass(HasTraits):
>>> my_button = Button()
>>>
>>> traits_view = View(
>>> Item('my_button', editor=ButtonEditor(image=ImageResource('foo'))),
>>> )
>>>
>>
>> But all my button says is 'my_button'. It has no image. There is an
>> images/foo.png in the same directory. I also tried passing search_path
>> to ButtonEditor(), but the result was the same. I tried using
>> ImageResource('foo.png') but that also didn't work.
>>
>> Any ideas on what could be going wrong? I am using pyface, traitsui, etc
>> from git a few days ago.
>>
>> Thanks,
>>
>> Tiago
>>
>
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev

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

Re: TraitsUI ButtonEditor with image

Warren Weckesser


On Thu, Jul 12, 2012 at 6:05 PM, Jaidev Deshpande <[hidden email]> wrote:
Hi,

On Fri, Jul 13, 2012 at 2:59 AM, Tiago Pereira <[hidden email]> wrote:
> Found the problem. One needs to specify style='custom':
>
> Item('my_button', editor=ButtonEditor(image=ImageResource('foo'),
> style='custom)
>

I tried doing this but in my case the style argument can only be one
of 'button', 'checkbox', 'radio' or 'toolbar'. It's not accepting the
'custom' argument. I'm using Traits 4.2.0. What are you using?



style='custom' should be an argument of Item(), not ButtonEditor().

Warren


 
> For some reason, the ImageEditor can accept an image like image='foo',
> but ButtonEditor needs image=ImageResource('foo'). This API change is
> confusing.
>
>
> Tiago
>
> On 12/07/12 11:37 AM, Tiago Pereira wrote:
>> Hi,
>>
>> Thanks for the answers. I tried this:
>>
>> On 12/07/12 6:10 AM, Robert Kern wrote:
>>
>>> In `mymodule.py`, do the following
>>>
>>> from pyface.image_resource import ImageResource
>>>
>>> class MyClass(HasTraits):
>>> my_button = Button()
>>>
>>> traits_view = View(
>>> Item('my_button', editor=ButtonEditor(image=ImageResource('foo'))),
>>> )
>>>
>>
>> But all my button says is 'my_button'. It has no image. There is an
>> images/foo.png in the same directory. I also tried passing search_path
>> to ButtonEditor(), but the result was the same. I tried using
>> ImageResource('foo.png') but that also didn't work.
>>
>> Any ideas on what could be going wrong? I am using pyface, traitsui, etc
>> from git a few days ago.
>>
>> Thanks,
>>
>> Tiago
>>
>
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev

Thanks
_______________________________________________
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: TraitsUI ButtonEditor with image

Tiago Pereira
On 12/07/12 4:10 PM, Warren Weckesser wrote:

> style='custom' should be an argument of Item(), not ButtonEditor().

That's right, sorry I missed a parenthesis in my email.

By the way, is there a way to change the image when the button is fired?

Thanks,

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

Re: TraitsUI ButtonEditor with image

Jaidev Deshpande
In reply to this post by Warren Weckesser
On Fri, Jul 13, 2012 at 4:40 AM, Warren Weckesser
<[hidden email]> wrote:

>
>
> On Thu, Jul 12, 2012 at 6:05 PM, Jaidev Deshpande
> <[hidden email]> wrote:
>>
>> Hi,
>>
>> On Fri, Jul 13, 2012 at 2:59 AM, Tiago Pereira <[hidden email]> wrote:
>> > Found the problem. One needs to specify style='custom':
>> >
>> > Item('my_button', editor=ButtonEditor(image=ImageResource('foo'),
>> > style='custom)
>> >
>>
>> I tried doing this but in my case the style argument can only be one
>> of 'button', 'checkbox', 'radio' or 'toolbar'. It's not accepting the
>> 'custom' argument. I'm using Traits 4.2.0. What are you using?
>>
>
>
> style='custom' should be an argument of Item(), not ButtonEditor().

Thanks, it's working fine now.

>
> Warren
>
>
>
>>
>> > For some reason, the ImageEditor can accept an image like image='foo',
>> > but ButtonEditor needs image=ImageResource('foo'). This API change is
>> > confusing.
>> >
>> >
>> > Tiago
>> >
>> > On 12/07/12 11:37 AM, Tiago Pereira wrote:
>> >> Hi,
>> >>
>> >> Thanks for the answers. I tried this:
>> >>
>> >> On 12/07/12 6:10 AM, Robert Kern wrote:
>> >>
>> >>> In `mymodule.py`, do the following
>> >>>
>> >>> from pyface.image_resource import ImageResource
>> >>>
>> >>> class MyClass(HasTraits):
>> >>> my_button = Button()
>> >>>
>> >>> traits_view = View(
>> >>> Item('my_button', editor=ButtonEditor(image=ImageResource('foo'))),
>> >>> )
>> >>>
>> >>
>> >> But all my button says is 'my_button'. It has no image. There is an
>> >> images/foo.png in the same directory. I also tried passing search_path
>> >> to ButtonEditor(), but the result was the same. I tried using
>> >> ImageResource('foo.png') but that also didn't work.
>> >>
>> >> Any ideas on what could be going wrong? I am using pyface, traitsui,
>> >> etc
>> >> from git a few days ago.
>> >>
>> >> Thanks,
>> >>
>> >> Tiago
>> >>
>> >
>> > _______________________________________________
>> > Enthought-Dev mailing list
>> > [hidden email]
>> > https://mail.enthought.com/mailman/listinfo/enthought-dev
>>
>> Thanks
>> _______________________________________________
>> 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