10.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 'imggif' is the Photo handle for a gif file that is distributed with Tk-b#:

    #!/usr/bin/perl -w
    use strict;
    use Tk;
    my $main = new MainWindow;
    my $canvar = $main ->Canvas;

    $main->Photo('imggif', 
                  -file => "$Tk::tk_library/demos/images/earth.gif");
    $canvar->create(qw(image 5c 5c), '-image' => 'imggif');

    $canvar->pack;
    MainLoop;

Previous | Return to table of contents | Next