in reply to Images in Canvas
To load any standard image type( bmp gif jpg png) you first load the image into a Photo object, then specify the Photo object in your widget's image option. Don't forget that gif and bmp are the only ones included by default, so you need use Tk::JPEG and use Tk::Png. Here is your basic image on a canvas.#!/usr/bin/perl use Tk; my $mw = MainWindow->new; $mw->geometry('-5-0'); #$mw->overrideredirect(1); my @color = qw/red green/; my $bits = pack("b8"x8, "...11...", "..1111..", ".111111.", "11111111", "11111111", ".111111.", "..1111..", "...11...",); $mw->DefineBitmap('indicator' => 8,8, $bits); my $label = $mw->Label( -bitmap=>'indicator', -bg=>'black', -fg=>'red', )->pack; $mw->repeat(500,sub{$label->configure( -fg=>$color[0]); @color=reverse(@color); }); MainLoop;
#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::JPEG; my $file = "zen16.jpg"; my $mw = Tk::MainWindow->new; my $can = $mw->Canvas( -width => 320, -height => 240 )->pack(); # if you are pulling your image from data (inline base64encoded string # you may need to specify -format #my $img = $mw->Photo( -data => $data, -format => 'jpeg' ); my $img = $mw->Photo( -file => $file); $can->createImage( 0, 0, -image => $img, -anchor => 'nw' ); my $red = $can->createRectangle(0, 20, 50, 75, -fill => 'red'); $can->Tk::bind("<Button-1>", [ \&print_xy, Ev('x'), Ev('y') ]); MainLoop(); sub print_xy { # print "@_\n"; my ($canv, $x, $y) = @_; print "(x,y) = ", $canv->canvasx($x), ", ", $canv->canvasy($y), "\n" +; printf "%6.6X\n", $img->get($canv->canvasx($x), $canv->canvasy($y) ) +; }
|
|---|