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

Hi all ! i want to change a button's image in Tk but i don't arrive to do this.
#!/usr/bin/perl -w use strict ; use Tk ; # window's initialisation $mw = new MainWindow ; $img = 'example.xpm' ; { not really exist sur ! } #A button $bttn = $mw->Button(-image=>$img, -command=>\&clicked)->pack(-side=>'t +op'); sub clicked { if $bttn->configure(-image) eq 'example.xpm') { $bttn->configure(-image=>'example2.xpm'); } else { $bttn->configure(-image=>'example.xpm'); } } MainLoop;
i thought this code ran but definitly not. don't examin the syntax(or my english :) but just if it's possible to change a button's image
that many ways to do it (sic !)

Retitled by Chady -- was 'in perl TK'

Replies are listed 'Best First'.
Re: How Do I Change A Button Image in Perl TK?
by eserte (Deacon) on Jun 09, 2004 at 12:26 UTC
    You need to create a Photo object and use this in the -image options: Untested:
    $p = $mw->Photo(-file => "example.xpm"); $mw->Button(-image => $p, ...);
    If you use xpm then you can also use Pixmap instead of Photo (but this is really only needed if you're short on memory and use big and many pixmaps).
      Yes ! i know this. i've said :" don't examin my syntax !".Even if i use 'Photo' my procedure don't do that i want ! i just want change button's picture during the program run ! thanks man
      that many ways to do it (sic !)
        If you want to change the picture, then do
        $button->configure(-image => $photo_object);
        where $photo_object is constructed with $mw->Photo or $mw->Pixmap. And do not forget to delete unused photo objects, otherwise you'll got a memory leak. Another alternative is to change the filename in the photo object:
        #!/usr/bin/perl use Tk; $mw = tkinit; $p = $mw->Photo(-file => Tk->findINC("Xcamel.gif")); $b = $mw->Button(-image => $p, -command => sub { $p->configure(-file => Tk->findINC("openfolder.xpm")); })->pack; MainLoop;