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

Is it possible to lay a Text widget over an image? I've tried everything I could think of and can't find an answer using super search.

Heres the snippet of code:

my $ROW1=$GUI->Frame(-relief=>'flat', -borderwidth=>0, -background=>'#c2d297' ->pack(-side=>'top', -fill=>'x'); my $photo = $ROW1->Photo(-file=>"numbers/1.bmp"); my $photo_canvas = $ROW1->Canvas(-width=>29, + -height=>32, -background=>'#c2d297', -borderwidth=>0) ->pack(-side=>'left'); $photo_canvas->createImage(16, 18, -image=>$photo);

Replies are listed 'Best First'.
Re: Text widget on Photo (Tk)
by liverpole (Monsignor) on Dec 12, 2006 at 22:38 UTC
    Hi antioch,

    Absolutely.  Just use the place geometry manager instead of (or in addition to) pack.

    For example:

    use strict; use warnings; use Tk; my $GUI = new MainWindow; my $ROW1=$GUI->Frame(-relief=>'flat', -borderwidth=>0, -background=>'#c2d297') ->pack(-side=>'top', -fill=>'x'); my $photo = $ROW1->Photo(-file=>"numbers/1.bmp"); my $photo_canvas = $ROW1->Canvas(-width=>29, -height=>32, -background=>'#c2d297', -borderwidth=>0) ->pack(-side=>'left'); my $text = $ROW1->Text()->place(-x => 16, -y => 16); $photo_canvas->createImage(16, 18, -image=>$photo); MainLoop;

    The above example overlays a Text widget on top of the photo, by overlapping it in Frame $ROW1, with (x,y) coordinates of (16, 16).  Please refer to the Perl/Tk documentation, specifically the pages about geometry management (eg. pack, grid and place) for more details.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Thank you! =)
Re: Text widget on Photo (Tk)
by zentara (Cardinal) on Dec 13, 2006 at 12:05 UTC
    You can use the createWindow method of the canvas also. This script detects and sizes the window and textbox automatically, just feed it a jpg image on the commandline.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::JPEG; use Image::Magick; my $im = Image::Magick->new; my $image = shift || '2uni2.jpg'; my ($width, $height, $size, $format) = $im->Ping($image); my $mw = MainWindow->new(); my $canv = $mw->Canvas(-bg => 'lightsteelblue', -relief => 'sunken', -width => $width, -height => $height)->pack(-expand => 1, -fill => 'both'); my $img = $mw->Photo(-file => $image ); $canv->createImage( 0, 0, -image => $img, -anchor => 'nw' ); my $text = $mw->Scrolled('Text', -bg=>'white', -scrollbars=>'osoe', ); my $textcontainer = $canv->createWindow( $width/2, $height/2, # default anchor is center -window => $text, -width => $width -200, -height => $height-200, -state => 'normal'); $text->focus; MainLoop();

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum