On Wed, 27 Aug 2008 19:35:46 +0100, [EMAIL PROTECTED] (Aruna Goke)
wrote:
however, I wanted to be able to scroll
both horizontally and vertically so that i can see all the numbers
printed. and which one is selected as winner on click.
and ability to click and randomly selected a single winner as long as i
click will be appreciated.
Ok, there are some things to do to get that extra functionality.
First, when you move to a scrolled canvas, you need to get
the subwidget 'scrolled' to do some things.
Also, things are done with tags on canvases. I added another
tag to make the left click guess work. I also showed how you can
change color on a bad guess.....you may not want that.
Finally, you will notice, that the greying out depends on whether you
click on the rect or the number.... that is the tag layering..... you
can fix that with some work on the tags.... but I leave that to you.
Have fun,
zentara
#!/usr/bin/perl -w
use warnings;
use strict;
use Tk;
#generate the size of an my array.
my $filename = "julypromo.csv";
#Open the file
open FH, '<', $filename || die "Couldn't open $filename : $!";
my (@carprice, @array);
while(<FH>){
@array = split/,/;
push @carprice, $array[1] if $array[0] =~ /Car/;
}
# put in your row and columns size
my ($x, $y) = ($#carprice-1, 10);
#my($x,$y) = (int(($#carprice-1)/2), int(($#carprice-1)/2));
#my $winner = int rand($x * $y);
my $winner = int rand($#carprice);
print "$winner\n";
my $mw = MainWindow->new;
my $vh = $mw->vrootheight-50;# -100; #get window full size
my $vw = $mw->vrootwidth-50;# -100; #with some margin maybe
$mw->geometry($vw.'x'.$vh);
#my $xsize = $vw/$x; #cell sizes
#my $ysize = $vh/$y;
my $xsize = int $vw/10;
my $ysize = int $vh/10;
$mw->fontCreate('big',
-family=>'arial',
-weight=>'bold',
-size=> 38
);
# this is what grabs all virtual desktops
#$mw->overrideredirect(1);
# Note that the 'virtual window' height and width are $vh and $vw
# respectively, so we use those dimensions for our Canvas height
# and width, and let the Canvas expand and fill in both x and y
# directions.
#
my $scrolled_c = $mw->Scrolled('Canvas',
-width => 600, #actual size on screen
-height => 600,
-scrollregion =>[0,0,$vw,$vh], #scrollregion
-background =>'black',
-scrollbars => 'osoe',
#-takefocus =>0
)->pack(-expand => 1, -fill => 'both');
my $c = $scrolled_c->Subwidget('scrolled');
my %cell;
my $count = 0;
for my $xc(0..$x-1) {
for my $yc(0..$y-1){
$count++;
my $wintag = 0; #added for left click guesses
if($count == $winner){
$wintag = 1;
$cell{$xc}{$yc}{'win'} = $c->createText(
$xc * $xsize + $xsize/2,
$yc* $ysize + $ysize/2,
-anchor=>'center',
-font => 'big',
-fill => 'red',
-text => 'WIN',
-tags => ['winner']
);
$c->lower( 'winner' ,'rect' );
}
my @tags;
if($wintag){ @tags = ('rect','wintag') }
else{ @tags = ('rect') }
$cell{$xc}{$yc}{'rect'} =
$c->createRectangle($xc * $xsize, $yc* $ysize ,
$xc * $xsize + $xsize ,$yc* $ysize + $ysize ,
-fill=> 'lightyellow',
-outline=>'black',
-tags => [EMAIL PROTECTED],
);
$cell{$xc}{$yc}{'text'} = $c->createText(
$xc * $xsize + $xsize/2, $yc* $ysize + $ysize/2,
-anchor=>'center',
-font => 'small',
-fill => 'black',
-text => $carprice[$count],
-tags => [EMAIL PROTECTED]
);
}
}
#use this to set scrollbars to encompass all squares
$c->configure(-scrollregion => [$c->bbox('all')]);
# left click shows if individual square is a winner
$scrolled_c->CanvasBind("<Button-1>", sub{
#get current cell
my @tags = $c->gettags("current");
if( grep{/wintag/} @tags){
$c->raise( 'winner' ,'rect' );
}
else{
$c->itemconfigure('current',-fill=>'grey');
}
});
# right click shows winner
$scrolled_c->CanvasBind("<Button-3>", sub{
$c->raise( 'winner' ,'rect' );
});
MainLoop;
__END__