Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I use transparency in my Tk window icons?

Graphical User Interface (GUI)

How do I use transparency in my Tk window icons?

by  Kirsle  Posted    (Edited  )
If you're like me and most other Perl Tk programmers, you've no doubt asked yourself from time to time how in the world can you use transparent pixels in your Tk window's icon? Using a simple GIF or PNG image with transparent pixels doesn't cut it because the transparent parts show up as a solid color anyway. So how is it done?

The answer: you need to create an X BitMap Image (XBM) file and use it as a mask for your window's icon. Fortunately, this is easy to do.

Some image editing programs can save your images as XBM format already. I recommend The GIMP.

1. Open your icon image (we'll say that it's called appicon.png) in The GIMP.

2. Click "File / Save As". Make sure you select the file type to be "X BitMap Image (xbm)".

It might warn you about how you have to export the image first. Go ahead and click "Export" and let it do it for you. Then the XBM window will come up to ask you for some options for your image.

3. Make sure that "Write extra mask file" is checked. This is the whole purpose of exporting it into this format to begin with.

4. Go ahead and save it. It should create two files: appicon.xbm and appicon_mask.xbm in this case. We're only interested in the mask file.

5. Place the mask file in a location where your Perl script can get to it. Now, you simply call iconmask() on your MainWindow and point it in the right direction:

Code:
use Tk;
use Tk::PNG;

my $mw = MainWindow->new;

my $icon = $mw->Photo (
   -image => 'appicon.png',
   -format => 'PNG',
);

# Set the PNG icon.
$mw->Icon (-image => $icon);

# Apply the mask file.
$mw->iconmask ('@appicon_mask.xbm');

MainLoop;

Note: the @ before the mask's name is crucial. Otherwise Tk will assume you want a built-in bitmap and then error out when it doesn't find it.

And that should make your Perl Tk window's icon have transparent pixels and free it from always having to be a square!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top