http://www.perlmonks.org?node_id=104583

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

I'm a pretty total newbie to Tk, and I wanted to play with JPEG support. I searched CPAN and checked out Tk::JPEG, which seemed suited for my purposes. So I installed it, read the Tk documentation and tried to write a small script which will display the file 'test.jpg'. However, when I tried to run this:
use strict; use warnings; use Tk; use Tk::JPEG; my($main_window); $main_window = MainWindow->new(); $main_window->Photo(-format => 'jpeg', -file => 'test.jpg')->pack; MainLoop;
I was greeted with the fateful words:
'wrong # args: should be "pack option arg ?arg ...?" at tk.pl line 9.'
Not to be put off, I tried specifying more arguments to the pack function., specifically '-side => 'bottom'' and '-expand => 1'. Rather annoyingly, this caused the following incomprehensible error message:
'bad option "image1": must be configure, forget, info, propagate or sl +aves at tk.pl line 9'
Removing either of the arguments furnished no solution. The most helpful thing someone could do here would just be to post a very basic chunk of Tk::JPEG using code, that I could learn from. I expect it's something really simple I'm missing here, but hey, better to ask and be a fool for five minutes...

Replies are listed 'Best First'.
(ichimunki) Re: Tk::JPEG
by ichimunki (Priest) on Aug 14, 2001 at 01:29 UTC

    You can't attach a Photo to a MainWindow (Correction: Yes, you can, see Jouke's great example "below"). It needs to belong to a Label or a Button or a Text widget (and probably others).

    my $mw = MainWindow->new; my $button = $mw->Button()->pack(); my $photo = $button->Photo( -format => 'jpeg', -file => 'test.jpg' ); $button->configure( -image => $photo ); MainLoop;

    See also the perldoc for Tk::Photo, it's infinitely more useful than the docs for Tk::JPEG.

    Update: now tested, added configure method, without which Tk never realizes it's supposed to be the -image for $button.

      Aha. I thought it was something obvious, and I had probably read the wrong docs...*hits self* Thanks a lot.
      And of course you can define all of this at once:
      my $mw = MainWindow->new; my $photo = $button->Photo( -format => 'jpeg', -file => 'test.jpg' ); my $button = $mw->Button(-image => $photo)->pack(); MainLoop;


      Jouke Visser, Perl 'Adept'
      Using Perl to help the disabled: pVoice and pStory
        I think it's a little strange to call a method on $button before you have declared $button with my $button. Maybe you patched your parser with a lookahead feature, "hey, do I declare this later, if so I can use it now, right?" :)