http://qs321.pair.com?node_id=228948

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

Fellow monks,

A while back I wrote an image map program in Perl for a project in a Perl class I took. Well I went to use it on a large graphic the other day and discovered a minor flaw, so needless to say I'm in the process of fixing it now. :)

My problem is with the scrollbars. They are showing up as advertised, but they're filling the entire canvas space and I can't figure out exactly how to set it to a percentage of the canvas width.

My frame is set to 800 x 600, and the canvas is based on the size of the GIF file that it's reading. If it's larger than 300 pixels either way, the value is set to 300 pixels.

The code I'm working with is this:

my $tcanvas=$top->Scrolled("Canvas", -scrollbars => 'ow', -scrollbars => 's', -bg=>'white', -height=>$height, -width=>$width, -border=>0, -cursor=>'crosshair', )->place( -x=>325, -y=>30, ); my $canvas = $tcanvas->Subwidget("canvas");

My graphic is about 200 x 700 and when I try it with the program, I get the bottom scroll bar, but not the side bar, which is what I would expect as I've set it that way. But the bottom scrollbar fills the entire area on the bottom. Using the arrow bits on either side does move the graphic along fine.

Is there something that I haven't set in my scrollbar? I looked in the Perl/Tk book but that wasn't much help unless I overlooked something obvious.

Thanks in advance!

There is no emoticon for what I'm feeling now.

Replies are listed 'Best First'.
Re: TK Scrollbars not working correctly
by pg (Canon) on Jan 22, 2003 at 07:39 UTC

    If you want both horizontal and vertical scrollbars, you have to say: -scollbars es, not -scrollbars e -scrollbars s

    Those named parameters are indeed hash elements, to reassign them, simply overwrites the existing value.

    One example:

    use Tk; use strict; use warnings; sub something { my $box = shift; $box->delete("1.0", "end"); $box->insert("end", $_) for (1 .. 100); } my $mw = MainWindow->new(title=>"Scrollbar Demo"); my $box = $mw->Scrolled(qw/Text -width 100 -height 15 -scrollbars se/) +->pack; $mw->Button(text=>"click", command=>[\&something, $box])->pack; MainLoop;
Re: TK Scrollbars not working correctly
by graff (Chancellor) on Jan 22, 2003 at 06:32 UTC
    For starters, your description is a little confusing: you have a 800 (wide) by 600 (tall) frame, and you place a canvas inside this, with a max size of 300 x 300 (possibly smaller on one or both axes, depending on the GIF to be loaded), you have a mandatory scrollbar at the bottom of the canvas, and an optional one along the right side. Then, you say that you load a 200 x 700 image, which should mean 200 wide, 700 tall, and you say that you don't get the side scroll bar, which is what you expected. (?? -- Did you really mean that the image is 700 x 200? What happens when you load an image that needs the vertical scrollbar?)

    Anyway, I gather that the bottom scrollbar is spanning the full width of the frame that contains the canvas (800 wide), which certainly seems wrong -- although you say it actually does scroll the image as intended when you click on its various elements.

    I don't have a direct answer to the sizing problem, but I would point out that you could (probably should) combine the two "-scrollbars" parameters into one:  -scrollbars => 'sow', -- sorry if that turns out to be off the mark... Apart from that, have you confirmed that the "$height" and "$width" variables actually have the expected values (as opposed to, say, zero or undef)?

      Provided the way Tk processes passed arguments, one *must* combine same parameters into a single one, otherwise only latest will be in effect.

      Most probably you pointed exactly at the problem place.

      Courage, the Cowardly Dog

      Sorry if it was a bit confusing. I do have an 800 x 600 Tk window with a canvas that defaults to 300 x 300 if the image is larger than that, or is sized at the graphic's actual size if it's smaller than 300 x 300. (Yes I did mean it was 700 x 200)

      I do know that the height and width are coming in correctly as I'm pulling those values right from the gif file itself.

      You're on the mark with the -scrollbars => 'sow' I know that, however when I tried to use -scrollbars => 'osow' both scrollbars disappeared. That's why I thought to put them as seperate items.

      Thanks for the idea though. Back to the books I guess. :)

      There is no emoticon for what I'm feeling now.

Re: TK Scrollbars not working correctly
by hiseldl (Priest) on Jan 22, 2003 at 15:20 UTC

    If you want the canvas to be the size of the image AND the scrollbars to fit in that canvas, then you will have to

    1. create another frame the size of your image, and
    2. create the canvas and place it in the frame
    It should look something like this:

    my $main_frame = tkinit(); # make this frame the size of your image my $canvas_frame = $main_frame->Frame( ...options... ); my $tcanvas = $canvas_frame->Scrolled('Canvas', ...options...); my $canvas = $tcanvas->Subwidget('canvas');

    Try making the frame smaller than the image to see that the scrollbars appear as you want them. Also, you need to combine the '-scrollbar' options as mentioned before.

    --
    hiseldl
    What time is it? It's Camel Time!

      Actually the only time I want to see the scrollbars is when the image is larger than 300 x 300. That's why I tried to do the optional bit. My thinking is, after reading the replies, that I should probably test the size of my graphic in the subroutine that gets the size, see if it's over my defined limit and build a string of what I want to see as scrollbars rather than use the optional bit. That may be a bit of a kludge, but using -scrollbars => 'osow' makes them both disappear.

      There is no emoticon for what I'm feeling now.

          the only time I want to see the scrollbars is when the image is larger than 300 x 300

        Aha! I think you want to set the -scrollregion. Set the -scrollregion after you have populated the canvas so that the scrollbars can reset to where they need to be. I loaded a 300x300 image and the scrollbars were not there, then I loaded a 400x400 image and the scrollbars both appeared.

        use Tk 800.000; use Tk::Canvas; use strict; my $main = Tk::MainWindow->new(); my $canvas=$main->Scrolled('Canvas', -scrollbars => 'osoe', -bg=>'white', -width=>300, -height=>300, )->pack(); my $image300x300 = $canvas->Photo(-file=>'300x300.gif'); my $image400x400 = $canvas->Photo(-file=>'400x400.gif'); my $id = $canvas->createImage(200,200,-image => $image400x400); # configure the scrollregion AFTER you populate the # canvas widget to reset the scrollbars $canvas->configure( -scrollregion => [ $canvas->bbox("all") ]); MainLoop;

        Is this what you're looking for?

        --
        hiseldl
        What time is it? It's Camel Time!