原文:
10.9. How do I display a bitmap?
You can display X bitmaps on your widgets with the -bitmap configuration option. Typically -bitmaps are configured into Label, Frame, Button, etc. widgets (Canvas widgets are another story however see question [11.1] below). In order to emphasize the bitmap option itself let us assume we were specifying a bitmap for a Label with a call like: $main->Label(-bitmap => 'bitmap-name')->pack;
Where bitmap-name could be any of the built in Tk bitmaps: error, gray25, gray50, hourglass, info, question, questhead, warning (see the widget demo for a full list).
In order to use some of the bitmaps in the perl5/Tk/demos/images/ directory you would specify a fuller path name like: $main->Label(-bitmap => "\@$tk_library/demos/images/face")->pack;
Note the escaped "\@" on the directory specification (as well as the use of the $tk_library variable imported by use Tk;). If you wanted to specify a file called foobar.xbm in the directory where you were running the script then either: $main->Label(-bitmap => '@foobar.xbm')->pack;#or $main->Label(-bitmap => "\@foobar.xbm")->pack;
should work just fine. In another directory however that would be a problem. So something like: $main->Label(-bitmap => "\@$ENV{'HOME'}/img/foobar.xbm")->pack;
will help someone who has an img/foobar.xbm file in their $HOME directory. If you don't mind the non-portability then hard-wiring in the full path name will help as well. (Or if you have write access then put your files in Tk/demos/images/ e.g.)
译文:
10.9. 如何显示一个位图(bitmap)?
我们可以通过在组件中使用-bitmap选项来设置显示位图。一般来说,-bitmap选项可以使用在Label、Frame和Button等组件中(Canvas组件虽然也可以但是比较特殊,我们将在后面的11.1.中详细介绍)。为了突出bitmap选项,我们假设用下面的代码为一个Label设置了一个位图:
$main -> Label(-bitmap => ‘bitmap-name’) -> pack;
这里的bitmap-name可以是任何的Tk内置位图:error、gray25、gray50、hourglass、info、question、questhead、 warning等等(完整的列表,请参阅widget演示程序中的Miscellaneous里的1. The built-in bitmaps.)
但是,要想使用perl5/Tk/demos/images/目录中的一下位图,我们就必须指定完全的路径,例如:
$mw->Label(-bitmap => "\@$Tk::library/demos/images/face")->pack;
这里,请特别注意在指定的目录名前面使用的转义操作符“\@”(同时,这里的变量$Tk::library是由use Tk;语句导入的)。如果你指定一个当前目录中的名为foobar.xbm的文件,那么使用:
$main->Label(-bitmap => '@foobar.xbm')->pack;
或者
$main->Label(-bitmap => "\@foobar.xbm")->pack;
两种语句都可以。但是,如果在其它的目录中就会有问题了。所以,使用如下的语句:
$main->Label(-bitmap => "\@$ENV{'HOME'}/img/foobar.xbm")->pack;
可以直接指定在用户主目录中的img/foobar.xbm文件。当然,如果你不在乎代码的冗长,那么书写出完整的路径也是一样的。(或者,如果你有写权限的话,还可以使用把你要用的文件放入Tk/demos/images目录中等办法)
(译者注:提醒大家注意,这里所谓的“位图”和我们在Windows系统中用“画图”程序所生成的后缀为bmp的图片不是一个概念。这里所指的这种文件后缀通常是xbm,但是也有很多根本就没有后缀——因为在Unix系统中并不需要。它们本质上是文本格式的文件,具体的定义,可以参阅手册perldoc Tk::Bitmap)