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

Hi All,
I am creating a memory-game for my Kid. I would like to change the features of the button on Click. I am Not sure how to do it. My code is as follows.
use strict; use Tk; use Tk::Button; require Tk::Photo; my $top = new MainWindow; my $myimage = $top->Photo(-format=>'bmp',-file=>"image/images2.bmp", - +height=> 50, -width => 50); for my $x (1..10) { for my $y (1..10) { my $button = $top->Button(); $button = $top->Button(-width => 5, -height=>2, -command => sub {c +heck_status($x,$y)}); $button->grid(-column=>$y,-row=>$x); } } sub check_status() { my ($myx,$myy)=@_; ######### What to Write ??? #### ################################ } MainLoop
Can some guru, please tell me what command I can write between the commented lines so that I can assign '$myimage' to the same button.
Update:
Dear All,
Thanks to all of you, for your suggestions. I have almost completed my script. However I cannot understand, How to make Tk sleep?? Here when I click on 2nd button, I want image to appear atleast for 2 secs and then disappear. However the image is disappearing when you click 2nd button. Forgive me if my explaination is Not proper.
use strict; use Tk; use Tk::Button; use Tk::Canvas; use Tk::Photo; use Tk::Splashscreen; require Tk::Splash; ##################### my @filearray; opendir( DIR, 'images' ) or die "Can't open images DIR $!"; while ( my $entry = readdir( DIR ) ) { my $type = ( -d "images\\$entry" ) ? "dir" : "file"; push @filearray, "images\\$entry" if (($type eq "file") && ($e +ntry =~ /\.gif$/)); #print "$type\t$entry\t@filearray\n"; } closedir( DIR ); @filearray = (@filearray,@filearray); #################### #fisher_yates_shuffle(\@filearray); unshift @filearray, "NIL"; #foreach (@filearray) {print $_,"\n";}; my @arranged_gifs; my $top = new MainWindow; my $loop_cntr=0; my $state_of_button=-1; my $prev_x = undef; my $prev_y = undef; my $number_of_clicks=0; my $prev_clicked_button=undef; foreach my $x (1..10) { for my $y (1..5) { $loop_cntr++; $arranged_gifs[$x][$y]= $filearray[$loop_cntr]; #print "$loop_cntr:$x:$y:-".$arranged_gifs[$x][$y]."\t".$filea +rray[$loop_cntr]."\n"; my $button = $top->Button(-width => 10, -height =>3,-command=> +sub{click_button($x,$y,\$top->gridSlaves(-row=>$x,-column=>$y), \$pre +v_clicked_button)}); $button->configure(-text=>'Hey'); $button->grid(-row =>$x , -column =>$y); } } sub click_button($$$){ my ($myx,$myy,$clicked_button,$prev_clicked_button)=@_; $number_of_clicks++; my $mywait; my $image = $top->Photo(-format => 'gif' , -file=>$arranged_gifs[$ +myx][$myy]); ${$clicked_button}->configure(-width => 50, -height =>30, -image=> +$image, -state=>'disabled'); if ($number_of_clicks == 1) { print "Hello\n"; ${$prev_clicked_button} = ${$clicked_button}; } if ($number_of_clicks == 2) { print "pair\n"; sleep(2); $number_of_clicks=0; ${$prev_clicked_button}->configure(-width => 10, -height =>3, +-state=>'normal', -image=>undef); ${$clicked_button}->configure(-width => 10, -height =>3, -stat +e=>'normal', -image=>undef); } } sub fisher_yates_shuffle { my $array = shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i,$j] = @$array[$j,$i]; } } MainLoop;

Replies are listed 'Best First'.
Re: Tk Game - Image on Button
by rinceWind (Monsignor) on Nov 17, 2006 at 11:07 UTC

    You want to use the configure method on the button. This is how you can change any attribute of your button, such as the -image.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: Tk Game - Image on Button
by zentara (Cardinal) on Nov 17, 2006 at 12:57 UTC
    You have to be careful with images on buttons, that they are the same size or smaller than the original. Also, I'll show you a little trick to include the images right into the script with base64encoding.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Why specify the image size at all??? Works fine without.
        As an added precaution to keep the grid intact when the second image is bigger than the first image. I made sure both images were sized right, so you could leave the button size options out. BUT, try loading a big image for the second image, and notice what happens when you specify button size, and not. When you do specify, the new big image will be contained and partially shown. When you don't, the whole grid gets re-arranged to accomadate the entire image.
        sub get_image2{ my $bunny = $top->Photo(-file => 'somebig.jpg'); + return $bunny; + }

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Tk Game - Image on Button
by vkon (Curate) on Nov 17, 2006 at 12:07 UTC
    If I am to write Tk game, I will look at Tcl/Tk games first, for example at http://mini.net/tcl/898 and wrap it to perl with one simple go using Tcl::Tk module from CPAN.
    Memory game is, for example, at A simple memory game

    I do this often
    (not games of course, but this approach of Perl+Tcl/Tk)
    ... and I am quite sure this is very effective approach.

Re: Tk Game - Image on Button
by strat (Canon) on Nov 18, 2006 at 09:20 UTC

    Maybe the codes from Example Codes for Tk::Photo might help you (the page is in german, but the code parts are more important)

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: Tk Game - Image on Button
by j3 (Friar) on Nov 17, 2006 at 19:31 UTC

    Off-topic from what you're asking, but there's a game called childsplay which is written in Python and uses SDL. It comes with a nice memory game (and a spelling one too). It's lib/memory.py file might provide inspiration for writing your own in Perl. :)

    Regarding Perl and SDL, looks like the SDL Perl site has some tutorial content. See also the SDL_Perl CPAN page.