holandes777 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All: Not so new at perl but very new at Tk. I'm not sure if you guys handle Tk here but ... I am trying to place a background image in a Tk window and then write over it:
use Tk; use strict; my $mw = MainWindow->new( -background=>'YELLOW', # -bitmap=>'money.bmp', # not here! ); $mw->state('zoomed'); yada-yada MainLoop; yada-yada
I looked at canvas and frame and there seems to be no "background image". There is one for labels and buttons but, those are precisely the things I am trying to overlay on the bitmap. Help or re-direction is appreciated.

Replies are listed 'Best First'.
Re: perl/TK - background image
by zentara (Cardinal) on Sep 26, 2006 at 20:00 UTC
    You need to learn the difference between a window and a widget. Mainwindows do not have a background, you have to fill them with widgets that have the background capability, and according to your needs, the ability to place widgets on top. The only widget which easily does this is the canvas. As a matter of fact, I worked out a canvas based widget, with a background image at Tk::CanvasDirTree.

    A simple example is below. But there are glitches. You may not be able to acheive background transparency on your inserted widgets. A workaround would be to build your own custom canvas widgets out of geometric shapes, and use mouse bindings on them, to simulate the widget actions.

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::JPEG; use Tk::PNG; my $file = shift || die "need a bmp, gif,jpg or png as arg 1\n"; my $mw=tkinit; my $canvas = $mw->Scrolled('Canvas')->pack(-expand=>1, -fill=>'both'); my $img = $mw->Photo( -file => $file ); $canvas->createImage(0,0, -image => $img, -anchor => 'nw', -tags => ['img'], ); bunchOfBoxes(); sometext(); MainLoop; sub bunchOfBoxes{ for(0..5){ my $window = $canvas->Checkbutton(-text=> $_); $canvas->createWindow(10, 10+ $_ * 20, -window=> $window); } } sub sometext{ $canvas->createText( 50 , 50, -text => 'foobar', -anchor => 'center', -fill => 'hotpink', ); }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: perl/TK - background image
by chargrill (Parson) on Sep 26, 2006 at 19:29 UTC
Re: perl/TK - background image
by holandes777 (Scribe) on Sep 27, 2006 at 23:33 UTC
    Thanks! I got it. The only remaining issue is that the labels are not transparent so they do not show the graphic underneath. From what I have read I would have to grab a slice of the underlying graphic to apply to each labe area and then render the characters on top of it. Since these are variable AND dynamic, it sounds too much like honest labour!