TonyDonker has asked for the wisdom of the Perl Monks concerning the following question:

I want to use a bitmap in my perl script. I have already the following script:

$showmenu->Button(-bitmap => "hourglass", -relief => 'raised', -borderwidth => '1', -pady => '-10', -padx => '5', -command => sub{ &AddPros($test_offset = $test_offset - $limit_entry) +}) ->pack(-side => 'left', -padx => '10');

I have downloaded an 'up.bpm' file. How can I use the 'up.bpm' in the place of 'hourglass'?
This doesn't work:

$showmenu->Button(-bitmap => "\@up.bpm",

Thanks in advance,
Tony

Edit by castaway - added code tags

Replies are listed 'Best First'.
Re: Bitmap for beginners (Perl Tk)
by mawe (Hermit) on Feb 15, 2005 at 16:41 UTC
    Hi!

    use Tk; my $top = new MainWindow; my $pic = $top->Photo(-file=>"up.bmp"); $top->Button(-image=>$pic)->pack(); MainLoop();
    Hope this helps.

    Regards, mawe

      Hi mawe, thanks for your help. I receive the following message:

      32-bits BMP file not (yet) supported at ../Image.pm at line 21

      Any ideas?
      Thanks,
      Ton
        Hi!

        As zentara mentions below, you have to base64 encode the image first. Here is a snippet from Matering Perl/Tk which shows one was to do it:

        sub encode_photo_data { my($file) = @_; use MIME::Base64; my ($bin, $data, $stat); open PHOTO, $file or die "Cannot open $file: $!"; while ( $stat = sysread PHOTO, $bin, 57 * 17 ) { $data .= encode_base64($bin); } close PHOTO or die $!; die "sysread error: $!" unless defined $stat; $data; } # end encode_photo_data

        Regards, mawe

Re: Bitmap for beginners (Perl Tk)
by zentara (Cardinal) on Feb 15, 2005 at 17:45 UTC
    Since you are a beginner, and the topic is bitmaps, I will tell you a few pointers to help you understand Tk images.

    1. Almost all "images" in Tk must be converted to "Photo objects" before they can be used, as in the example given to you. There are a few exceptions, like XPM files, which can be read in as Pixmaps. But Tk comes with built-in support for png, jpg, and gifs, but you may need to load the modules like "use Tk::JPEG;" or "use Tk::PNG;".

    2. If you are reading data into a photo object from "memory" as opposed to from a file, you must base64 encode the image first.


    I'm not really a human, but I play one on earth. flash japh
      Hi zentara,

      when trying to load a .jpg with the following:

      use Tk;
      use Tk::JPEG;
      my $top = new MainWindow;
      my $pic = $top->Photo(-file=>"arrow.jpg");
      $top->Button(-image=>$pic)->pack();
      MainLoop();

      I receive the message:

      Can't locate Tk/JPEG.pm in @INC (@INC contains:
      E:/Perl/lib E:/Perl/site/lib .)
      at E:\As2ConGUI\work\bitmap.pl line 2.
      BEGIN failed--compilation aborted at
      E:\As2ConGUI\work\bitmap.pl line 2.

      I have installed the correct Perl extensions...

      Thanks,

      Ton
        I use linux, and your code works, except for one thing...'arrow.jpg' is not in the current directory. So I don't know what your windows problem is. I would guess that you have an old perl version, and you need to get Tk::JPEG as a separate ppm(it's standard in the newer Perl versions). Also, unless 'arrow.jpg' is a file in the current script directory, you will need to specify the full path to it, or use something like:
        (-file => Tk->findINC('arrow.jpg'))

        I'm not really a human, but I play one on earth. flash japh