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.


In reply to Tk::Canvas too wide for MainWindow by benizi

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.