Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sizing an image to a button

47 views
Skip to first unread message

Mark Tarver

unread,
Oct 31, 2017, 9:18:14 AM10/31/17
to
Hi,

I want to place an image on a button but want the image resized to fit the dimensions of the button.

I've tried resizing the image when created and resizing the button to no avail. If I wanted an image sized to fit a button of say 200 height x 100 width in pixels, how would I do it?

Mark

Gerald Lester

unread,
Oct 31, 2017, 9:40:20 AM10/31/17
to
I'd suggest rather you should use the -image option of the button to say
that the image should be placed in the button.


--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+

Mark Tarver

unread,
Oct 31, 2017, 10:06:53 AM10/31/17
to
I'm configuring the height and width of the button. But the image - which is large - is simply cropped when displayed. I want to shrink it to the dimensions of the button.

Mark

Gerald Lester

unread,
Oct 31, 2017, 10:09:33 AM10/31/17
to
On 10/31/2017 09:06 AM, Mark Tarver wrote:
> I'm configuring the height and width of the button. But the image - which is large - is simply cropped when displayed. I want to shrink it to the dimensions of the button.
>
> Mark
>
> On Tuesday, October 31, 2017 at 1:40:20 PM UTC, Gerald Lester wrote:
>> On 10/31/2017 08:18 AM, Mark Tarver wrote:
>>> Hi,
>>>
>>> I want to place an image on a button but want the image resized to fit the dimensions of the button.
>>>
>>> I've tried resizing the image when created and resizing the button to no avail. If I wanted an image sized to fit a button of say 200 height x 100 width in pixels, how would I do it?
>>
>> I'd suggest rather you should use the -image option of the button to say
>> that the image should be placed in the button.

You need to resize your image to say 195 by 95 and used the resized
image as the -image argument to the button. Do not give the button
sizing information and it will form to the image.

Mark Tarver

unread,
Oct 31, 2017, 10:30:31 AM10/31/17
to
I've tried the following

package require Img
image create photo s2 -file "C:/Users/User/Desktop/Whist/PNG-cards-1.3/2_of_spades.png" -height 150 -width 90
button .b -image s2
pack .b

package require Img
image create photo s2 -file "C:/Users/User/Desktop/Whist/PNG-cards-1.3/2_of_spades.png"
button .b -image s2 -height 150 -width 90
pack .b

package require Img
image create photo s2 -file "C:/Users/User/Desktop/Whist/PNG-cards-1.3/2_of_spades.png" -height 150 -width 90
button .b -image s2 -height 150 -width 90
pack .b

None work.

Mark

Rich

unread,
Oct 31, 2017, 10:42:16 AM10/31/17
to
[Please don't top-quote - it breaks the temporal ordering of the
context of the reply]

Mark Tarver <dr.mt...@gmail.com> wrote:
> On Tuesday, October 31, 2017 at 2:09:33 PM UTC, Gerald Lester wrote:
>> On 10/31/2017 09:06 AM, Mark Tarver wrote:
>> > On Tuesday, October 31, 2017 at 1:40:20 PM UTC, Gerald Lester wrote:
>> >> On 10/31/2017 08:18 AM, Mark Tarver wrote:
>> >>> I want to place an image on a button but want the image resized
>> >>> to fit the dimensions of the button.
>> >>>
>> >>> I've tried resizing the image when created and resizing the
>> >>> button to no avail. If I wanted an image sized to fit a button
>> >>> of say 200 height x 100 width in pixels, how would I do it?
>> >>
>> >> I'd suggest rather you should use the -image option of the button
>> >> to say that the image should be placed in the button.
>> >>
>> > I'm configuring the height and width of the button. But the image
>> > - which is large - is simply cropped when displayed. I want to
>> > shrink it to the dimensions of the button.
>> >
>>
>> You need to resize your image to say 195 by 95 and used the resized
>> image as the -image argument to the button. Do not give the button
>> sizing information and it will form to the image.
>
> I've tried the following
>
> package require Img
> image create photo s2 -file "C:/Users/User/Desktop/Whist/PNG-cards-1.3/2_of_spades.png" -height 150 -width 90
> button .b -image s2
> pack .b
>
> package require Img
> image create photo s2 -file "C:/Users/User/Desktop/Whist/PNG-cards-1.3/2_of_spades.png"
> button .b -image s2 -height 150 -width 90
> pack .b
>
> package require Img
> image create photo s2 -file "C:/Users/User/Desktop/Whist/PNG-cards-1.3/2_of_spades.png" -height 150 -width 90
> button .b -image s2 -height 150 -width 90
> pack .b
>
> None work.

That is because the -height and -width options to the Tk image object
are not "resize to this" options. They merely set initial sizes (or
crop, as the case may be). The Tk image subsystem does not contain a
scaling module, per se. It presumes you've already prescaled your
images to the sizes you need ahead of time.

With that said, you can scale an image via proper usage of the -zoom
and -subsample options to the "copy" subcommand. But if doing this
with large images you can run into memory consumption issues.

See http://wiki.tcl-lang.org/8448 for details on using these options to
scale an image.

Alternately, you can compile a module that actually provides a scaling
API:

http://wiki.tcl-lang.org/25685
http://wiki.tcl-lang.org/47656
http://wiki.tcl-lang.org/5581

Christian Gollwitzer

unread,
Oct 31, 2017, 5:05:42 PM10/31/17
to
Am 31.10.17 um 15:42 schrieb Rich:
> That is because the -height and -width options to the Tk image object
> are not "resize to this" options.

Yes and this is arguably a shortcoming of Tk.

> See http://wiki.tcl-lang.org/8448 for details on using these options to
> scale an image.
>
> Alternately, you can compile a module that actually provides a scaling
> API:
>
> http://wiki.tcl-lang.org/25685
> http://wiki.tcl-lang.org/47656
> http://wiki.tcl-lang.org/5581

The first one doesn't do proper interpolation. The third one is a giant
package (binding to ImageMagick) which might be overkill and introduces
a big number of dependencies. The second one is a self-standing package
with an interesting feature set and good quality - I'd recommend using
that one out of these three. Here is another single-purpose extension
which does resampling and was adopted from the netpbm library:

https://github.com/auriocus/PhotoResize

Caveat: I'm the author if the latter.

Christian


Gerald Lester

unread,
Oct 31, 2017, 5:12:50 PM10/31/17
to
Or the OP could resize the image externally and save the resized file to
be the one given as the image.

The other option is to display the images on a canvas and just put
bindings on them and not use buttons at all.

Christian Gollwitzer

unread,
Oct 31, 2017, 5:27:12 PM10/31/17
to
Am 31.10.17 um 22:12 schrieb Gerald Lester:
> The other option is to display the images on a canvas and just put
> bindings on them and not use buttons at all.

The canvas likewise doesn't scale images (which is a bug IMHO, but this
decision is stuck since then). tkpath, which can otherwise be used as a
drop-in replacement, does scale and rotate images.

Christian

0 new messages