Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Graphical User Interface (GUI)

How do I use transparency in my Tk window icons? by Kirsle
Posted: 8 Dec 07

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:


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');


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!

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close