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

Hi Monks,I have written a small application to look good , i have applied a background image to Mainwindow , but the image is moving up and down(place the cursor on it and scroll it down and up),how can i fix this problem can you please suggest .

#!/usr/bin/perl -w use Tk; use strict; use warnings; use Tk::JPEG; use Tk::PNG; my $mw = MainWindow->new; $mw->geometry("400x400"); if ( -e "2bgimg.png") { #if bgimage graphic exists , use it $mw->update(); my $canvas1 = $mw->Canvas->pack(-expand=>1,-fill=>'both'); my $bg_img = $mw -> Photo(-file=>'2bgimg.png'); $canvas1->createImage(0,0, -image => $bg_img, -anchor => 'nw', -tags => ['img'], ); } MainLoop;

Replies are listed 'Best First'.
Re: Background Image is moving in perl tk
by zentara (Cardinal) on Dec 17, 2010 at 12:49 UTC
    It can get tricky, and you need to set the canvas's scrollregion, even though it is not a scrolled canvas.
    #!/usr/bin/perl -w use Tk; use strict; use warnings; use Tk::JPEG; use Tk::PNG; my $mw = MainWindow->new; # I have a photo that is 500w x 430h # so make the $mw a bit bigger # This step can be automated to accept any size image # but I hardcode the numbers for this simple example $mw->geometry("510x440"); # gave $mw extra 10 pixels h and w to hold c +anvas # you need to be careful about specifying all sizes, and # borderwidths, so that no scroll occurs # you also probably need to set the scrollregion to the # exact photo size. #you also will have problems if someone resizes the mainwindow # you may need PDF, or SVG images, which Tk dosn't support yet # So you may want to make your window non-resizable # $mw->resizable(0,0); my $canvas1 = $mw->Canvas(-bg=>'lightgreen', -width=> 500, -height=>430, -scrollregion=>[0,0,500,430], )->pack(-expand=>0,-fill=>'none'); my $bg_img = $mw -> Photo(-file=>'1Zen16.jpg'); $canvas1->createImage(0,0, -image => $bg_img, -anchor => 'nw', -tags => ['img'], ); MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Background Image is moving in perl tk
by vr786 (Sexton) on Dec 18, 2010 at 07:27 UTC

    Thank you zentara, I was struggling to fix it . once again thank you very much