原文:
11.3. Display an Image?
Just as for the other widget types there is a two step process of first getting a "Photo" handle on the file of interest. For the Canvas (unlike the other widgets) one then makes a call to create an image as in the following example where 'IMG' is the Photo handle for a GIF file that comes distributed with the Tk kit (it just happens to be handled in this example via the scalar variable $img): #!/usr/bin/perl -w use strict; use Tk; my $main = new MainWindow; my $canvar = $main ->Canvas; $canvar->pack; my $file = 'demos/images/earth.gif'; my $img = $canvar->Photo( 'IMG', -file => Tk->findINC($file) ); $canvar->create( 'image',0,0, '-anchor' => 'nw', '-image' => $img ); MainLoop; __END__
译文:
11.3. 如何在画布中显示图片?
就像在其它的组件中显示图片一样,需要两个步骤。首先是要产生一个指向需要显示的图片的Photo句柄;对于画布组件(不同于其它的组件),然后,就可以像下面的例子中那样,调用create方法来创建图像了。(其中的IMG是指向我们显示的GIF文件的Photo句柄名称,这里的GIF文件是Tk包自带的 ——在这个例子中,我们使用了一个标量$img来控制它):
#!/usr/bin/perl -w
use strict;
use Tk;
my $main = new MainWindow;
my $canvar = $main ->Canvas;
$canvar->pack;
my $file = 'demos/images/earth.gif';
my $img =
$canvar->Photo( 'IMG',
-file => Tk->findINC($file) );
$canvar->create( 'image',0,0,
'-anchor' => 'nw',
'-image' => $img );
MainLoop;
__END__
(译者注:其实这里的’IMG’相当于是这个Photo句柄的标签——是可以任意选择的,所以在使用时他可以等价于$img——也就是说在倒数第二行中,我们也可以使用-image=>’IMG’来代替。)