In Tk Hell

TorgoX on 2003-04-10T03:50:52

Dear All,

I am bewildered by Tk -- in general, but also with this specific problem:

So I make some widgets. If I change that if(1) to if(0), and just throw them into the mainwindow, all is well and the window magically appears sized to the right size.

But since I want to be able to deal with cases where there are too many widgets for the window to even fit the screen, I use a Pane object (so make the if(0) an if(1)), I have a new problem: the window starts too small. Sure, I can resize it manually. But how do I make its initial size be the right size, if possible?

Tk is making me sad!


It's not trigonometry

djberg96 on 2003-04-10T05:05:57

See the geometry() method.

Re:It's not trigonometry

TorgoX on 2003-04-10T05:33:49

Yeah, I've seen the geometry() method -- I don't see how to make it be "just the right size" the way it normally would be without that Pack thingamajigger.

Re:It's not trigonometry

jordan on 2003-04-10T06:13:34

Wow! What a funny happenstance of your comment and the random (as in I Ching) Larry Wall quote that I was just served up:


What about WRITING it first and rationalizing it afterwords? :-)

--Larry Wall in <8162@jpl-devvax.JPL.NASA.GOV>

Re:It's not trigonometry

djberg96 on 2003-04-11T01:07:24

Oh, I see what you mean. Here's your code, revamped by me (plus an "exit" button). This work like you want?
use strict;
use Tk;
use Tk::Pane;

my $main = MainWindow->new;

my($container, $pane);

$pane = $main->Scrolled('Pane',
   -scrollbars => 'osoe',
   -sticky     => 'nsew',
   -gridded    => 'y'
);
$pane->pack(-fill => 'both', -expand => 1,);

my $width = 0;
my $height = 0;

for(0 .. 10) {
   my $label  = $pane->Label(-text => "Sed ut perspiciatis" );
   my $b2 = $pane->Button(-text => "Hooboy");
   my $b1 = $pane->Button(-text => "Yow!")->grid($label,$b2);

   my $twidth = $b1->reqwidth + $b2->reqwidth + $label->reqwidth;
   $width = $twidth if $twidth > $width;

   $height += $b1->reqheight;
}

$pane->pack if $pane;

my $eb = $main->Button(-text=>"Exit",-command=>sub{exit})->pack;
$width += $eb->reqwidth;
$height += $eb->reqheight;

$pane->configure(-width=>$width,-height=>$height);

MainLoo p();

Re:It's not trigonometry

djberg96 on 2003-04-11T01:08:42

oops - get rid of that second pane pack line.

Re:It's not trigonometry

TorgoX on 2003-04-11T02:11:03

Wow, I thought something like this might be necessary, but 1) I didn't know how to do it, and 2) I kept thinking "wait, shouldn't the geometry manager already be doing this for me, via some switch I can't find?".

But I like your way. It works!