Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Tk::Canvas too wide for MainWindow

by benizi (Hermit)
on Jul 23, 2005 at 15:28 UTC ( [id://477489]=perlquestion: print w/replies, xml ) Need Help??

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

I have a Tk app that basically consists of a Tk::Canvas inside a MainWindow. (It's a simple, hacked-together tool to display graphs of some data I'm using.) For simplicity, I've made the Canvas as many pixels wide as the number of data points in the graph. Some of my graphs, though, are wider than my screen's resolution (~2700 data points vs. 1600 pixels), so I tried simply constructing the Canvas with the Scrolled constructor. Unfortunately, the scrollbar doesn't seem to "realize" that the Canvas isn't fully displayed. Much-reduced version of the problem below:

Reduced version draws a sawtooth-like graph, with three peaks, first of which should be visible, next two should be scrolled-to.

Desired outcome: 1000x500 pixel MainWindow with portion of scrollable 3000-pixel-wide Canvas visible.

Current outcome: 1000x500 pixel MainWindow. Scrollbar indicates 100% of Canvas visible. (Stretching window reveals rest of Canvas.)

#!/usr/bin/perl use strict; use warnings; use Tk; my ($width, $height) = (1000, 500); my $cwidth = 3 * $width; sub func { $_ = shift; $_ / $width * $height % $height; } my $mw = MainWindow->new(-width => $width, -height => $height); $mw->packPropagate(0); # keeps MainWindow @ 1000x500 my $c = $mw->Scrolled('Canvas', -width => $cwidth + 2, -height => $height + 2, -scrollbars => 's')->pack; # draw a simple graph $c->create(line => $_, func($_), $_ + 1, func($_ + 1), -fill => 'red') for 1..$cwidth-1; $c->configure(-scrollregion => [ $c->bbox('all') ]); # THIS SOLVED THE + PROBLEM # could also add: -scrollregion => [ 0, 0, 3000, 500 ], # to the constructor MainLoop;

Update: Solved per pg's help (see: Re: Tk::Canvas too wide for MainWindow). Thanks.

Update 2: Corrected code, per chanio's suggestion.

Replies are listed 'Best First'.
Re: Tk::Canvas too wide for MainWindow
by pg (Canon) on Jul 23, 2005 at 15:55 UTC

    After read your post, I tried my code in this post Re: A Canvas and a Scrollbar with Tk, and it does not work with 5.8.4. It is unclear which version of Perl was used at that time.

      Okay, fixed my own code. This works with 5.8.4:

      use Tk; use strict; use warnings; my $main = new MainWindow(); my $canvas = $main->Scrolled('Canvas', -scrollregion => [0,0,1000,500], )->pack(-fill=>"both"); $canvas->createOval(100,100,500,500); MainLoop;
Re: Tk::Canvas too wide for MainWindow
by chanio (Priest) on Jul 24, 2005 at 20:45 UTC
    Since your code was almost correct and only needed a line, please, add it to the original source code like this (4 example)...
    ... -fill => 'red') for 1..$cwidth-1; $c->configure(-scrollregion => [ $c->bbox('all') ]); ## <--SOLVED MainLoop;
    Since the solution is correct if placed in that possition.

    Thanks!

    { \ ( ' v ' ) / }
    ( \ _ / ) _ _ _ _ ` ( ) ' _ _ _ _
    ( = ( ^ Y ^ ) = ( _ _ ^ ^ ^ ^
    _ _ _ _ \ _ ( m _ _ _ m ) _ _ _ _ _ _ _ _ _ ) c h i a n o , a l b e r t o
    Wherever I lay my KNOPPIX disk, a new FREE LINUX nation could be established

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://477489]
Approved by davidrw
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found