原文:
10.10. How do I display an image?
You will want to get a "Photo" handle on the file as in the following example where 'imggif' is the Photo handle for a gif file that is distributed with perl/Tk:
#!/usr/bin/perl -w use strict; use Tk; my $main = new MainWindow; $main ->Label(-text => 'Main')->pack; $main -> Photo('imggif', -file => "$Tk::tk_library/demos/images/earth.gif"); my $l = $main->Label('-image' => 'imggif')->pack; $main->Button(-text => 'close', -command => sub{destroy $main} )->pack(-side => 'left'); $main->Button(-text => 'exit', -command => [sub{exit}] )->pack(-side => 'right'); MainLoop;
(Canvas widgets are another story however see question a later question within this FAQ).
译文:
10.10. 如何显示一个图像?
要想显示一个图像文件(译者注:目前所能显示的图片格式,除了Bitmap和Pixmap外,好像只有:GIF和PPM/PGM),我们需要为这个文件创建一个Photo句柄。例如,在下面的例子中,’imggif’就是我们为一个GIF图像文件创建的Photo句柄(这个图片是Perl/Tk包中自带的)。
#!/usr/bin/perl -w
use strict;
use Tk;
my $main = new MainWindow;
$main ->Label(-text => 'Main')->pack;
$main -> Photo('imggif',
-file => "$Tk::library/demos/images/earth.gif");
my $l = $main->Label('-image' => 'imggif')->pack;
$main->Button(-text => 'close',
-command => sub{destroy $main}
)->pack(-side => 'left');
$main->Button(-text => 'exit',
-command => [sub{exit}]
)->pack(-side => 'right');
MainLoop;
(Canvas组件是另一种显示图片的方式,但是我们将在后面专门讨论。)