Displaying an Image or Bitmap
The
simplest way to replace the text on your Button with an image is to
use a bitmap.[1] The image will take the
place of the text; you can’t display both at the same time. The
-bitmap option takes a string that specifies which
bitmap to use:
-bitmap => 'bitmapname'There are several built-in bitmaps: error,
gray12, gray25,
gray50, gray75,
hourglass, info,
questhead, question,
warning, Tk, and
transparent. Figure 4-7 shows
all the built-in bitmaps. To more easily show you the bitmaps, Figure 4-7 was grabbed from the
widget demonstration program.
Using -bitmap for a
Label is exactly the same as for a Button. You’ll notice that
the Tk bitmap is the same as that used in the
upper-left corner of the application. Here are some examples using
-bitmap when creating a Button:
$mw->Button(-bitmap => 'error', -command => \&handle_error)->pack;
To specify a bitmap from a
file, you need to put an @ in front of the path:
$mw->Button(-bitmap => '@/usr/nwalsh/mybitmap',
-command => sub { exit })->pack;
Note that if you use
double quotes, you have to escape the @ with a
backslash (e.g., "\@/usr/nwalsh/mybitmap").

Figure 4-7. Window showing all the default bitmaps
If you don’t want to use a bitmap,
or have a different format of file (such as a GIF), you can use the
-image option. First create an image pointer and
then use the -image option to display the image. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access