I was toying around with how to make a stationary background "appear" to stay in place, as a Tk::Canvas was scrolled. It is more of an optical illusion, but it works. In the script below, the background image appears to stay stationary with scrolling. A very fine point being the ratio used in a scrolled widget. See the $div variable. It is not obvious, because you must account for the REAL canvas size, (which includes the default border).

Anyways, this will take a photo as ARGV[0] on the commandline, or will retreive an image from the net (if no image is given).

UPDATE April 8,2006 Fixed a few pixel jitter caused on first scroll use, caused by not accounting for the canvas border.

#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::JPEG; use Tk::PNG; my $mw=tkinit; my $imagin = shift; $imagin ||= ''; my $image; if(length $imagin){ $image = $mw->Photo(-file =>$imagin); }else{ $image = &get_photo; } my $c = $mw->Scrolled('Canvas', -width=>400, -height=>400, -bg=>'white', -scrollregion=>[0,0,1000,1000], -scrollbars=>'sw')->pack(-expand=>1, -fill=>'both'); my $real_can = $c->Subwidget('scrolled'); my $real_can_h = $real_can->reqheight; my $sc_h = $c->cget('-height'); my $pad = ($real_can_h - $sc_h)/2; # pad prevents a startup jitter due to border width my $bimage = $c->createImage( 0,$pad, -anchor => 'nw', -image => $image, ); for(0..500){ if( $_ % 2 == 0){ $c->createRectangle(0, 5 + $_ * 10, 30, 15 + $_ *10, -fill =>'hotpink', ); $c->createText( 3, 5 + $_ * 10, -fill =>'white', -text => $_, -anchor =>'nw', ); next; } } my $ybar = $c->Subwidget("yscrollbar"); $ybar->configure( -background => "lightgreen", -activebackground => "green", -troughcolor => "black", -command => \&yscrollcallback, ); $mw->Button(-text =>'Change Scroll Region', -command =>sub{ $c->configure(-scrollregion =>[0,0,2000,2000]); })->pack(); MainLoop; ###################################################################### #if you specify a yscrollcallback, you will override the #normal scroll behavior. sub yscrollcallback{ #restore original function $c->yview(@_); my($z,$z1) = $c->yview; my(undef,undef,undef,$sry) = $c->cget('scrollregion'); my $real_can_h = $real_can->reqheight; # print "$sry $real_can_h\n"; my $div = $sry/$real_can_h; # print "div $div\n"; $c->coords($bimage, 0, $div *$z * $real_can_h ); $c->update; } ###################################################################### +####3 sub get_photo{ use LWP::Simple; use MIME::Base64; my $URL = 'http://zentara.net/2uni2.jpg'; my $content = encode_base64(get($URL)); return $mw->Photo(-data => $content); }

In reply to Stationary background on Scrolled Tk-Canvas by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.