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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |