Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Tk autosizing Scrolled widgets

by tybalt89 (Monsignor)
on Sep 14, 2021 at 08:41 UTC ( [id://11136728]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Tk autosizing Scrolled widgets
in thread Tk autosizing Scrolled widgets

The mixing of ->pack and ->grid only applies within ONE parent, different parents can have different geometry managers within one application. No Problem. I do it all the time. Example follows.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11136705 use warnings; use Tk; use Tk::Pane; my $mw = MainWindow->new(); $mw->geometry("+0+0"); $mw->maxsize(1000,1200); $mw->minsize(400,300); my $scroll = $mw->Scrolled('Pane', -scrollbars => 'osoe', -sticky => 'news', )->pack(-expand => 1, -fill => 'both'); my $f = $scroll->Frame->pack(-expand => 1, -fill => 'both'); foreach my $row (0..10) { foreach my $col (0..12) { $f->Entry( -text => "rc $row $col", -width => 0, )->grid(-row => $row, -column => $col, -sticky => 'news'); $row or $f->gridColumnconfigure($col, -weight => 1); } $f->gridRowconfigure($row, -weight => 1); } # example of ->pack in a different Frame my $buttonbar = $mw->Frame(-bg => 'blue', )->pack(-fill => 'x'); $buttonbar->Button(-text => 'Exit', -command => sub{$mw->destroy}, )->pack(-side => 'right', -fill => 'x', -expand => 1); $buttonbar->Button(-text => ' Another Noop', )->pack(-side => 'right', -fill => 'x', -expand => 1); $buttonbar->Button(-text => 'Noop', )->pack(-side => 'right', -fill => 'x', -expand => 1); MainLoop; -M $0 < 0 and exec $0; # FIXME for testing

Applications resizing themselves without user input is a BAD IDEA. Just imagine your user trying to click on something and having it move out of the way just before the click. BAD IDEA!

See the "widget" demo program for an example of what an HList looks like. It should have come with the Perl/Tk package.
"I do not think it means what you think it means."

Replies are listed 'Best First'.
Re^4: Tk autosizing Scrolled widgets
by olgo (Acolyte) on Sep 14, 2021 at 13:43 UTC

    tybalt89:contrary to your strong recommendation, my application DOES NOT resize itself without user input. It reacts on user input and thus gets more or less data to present. Maybe I was unclear about this.
    By the way, the widget demo, specifically the HList one was what made me choose this Grid mechanism in the first place. But, as I also wrote before, I will give your ->grid packer a go, now that I now that it is an alternative. (The HList does do the job though, but there might be overhead)
    Thanks!

      Maybe closer, maybe not...
      Fine tuning left to the OP.

      #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11136705 use warnings; use Tk; use Tk::Pane; my ($rows, $columns) = (11, 13); my $sbsize = 22; my $mw = MainWindow->new(); # example of ->pack in a different Frame my $buttonbar = $mw->Frame(-bg => 'blue', )->pack(-fill => 'x'); $buttonbar->Button(-text => 'Exit', -command => sub{$mw->destroy}, )->pack(-side => 'right', -fill => 'x', -expand => 1); $buttonbar->Button(-text => 'Set Size', -command => \&setsize, )->pack(-side => 'right', -fill => 'x', -expand => 1); $buttonbar->Button(-text => 'Remove Column', -command => sub{$columns--; changegrid(); setsize(); }, )->pack(-side => 'right', -fill => 'x', -expand => 1); $buttonbar->Button(-text => 'Add Column', -command => sub{$columns++; changegrid(); setsize(); }, )->pack(-side => 'right', -fill => 'x', -expand => 1); $buttonbar->Button(-text => 'Remove Row', -command => sub{$rows--; changegrid(); setsize(); }, )->pack(-side => 'right', -fill => 'x', -expand => 1); $buttonbar->Button(-text => 'Add Row', -command => sub{$rows++; changegrid(); setsize(); }, )->pack(-side => 'right', -fill => 'x', -expand => 1); my ($screenw, $screenh) = ($mw->screenwidth, $mw->screenheight); print "w $screenw h $screenh\n"; $mw->maxsize($screenw, $screenh); #$mw->maxsize(1000,1200); #$mw->minsize(400,300); my $scroll = $mw->Scrolled('Pane', -scrollbars => 'osoe', -sticky => 'news', )->pack(-expand => 1, -fill => 'both'); my $f = $scroll->Frame->pack(-expand => 1, -fill => 'both'); changegrid(); $mw->update; setsize(); MainLoop; -M $0 < 0 and exec $0; # FIXME for testing sub setsize { $mw->update; my( $mwh, $sh, $fh) = map $_->height, $mw, $scroll, $f; my( $mww, $sw, $fw) = map $_->width, $mw, $scroll, $f; my( $mwrh, $srh, $frh) = map $_->reqheight, $mw, $scroll, $f; my( $mwrw, $srw, $frw) = map $_->reqwidth, $mw, $scroll, $f; my $deltaheight = $sh - $frh; my $deltawidth = $sw - $frw; print "height $mwh $mwrh $sh $srh $fh $frh\n"; print " width $mww $mwrw $sw $srw $fw $frw\n"; print "delta h $deltaheight w $deltawidth\n"; my $geo = $mw->geometry; my ($w, $h) = split /\D/, $geo; my $newgeo = ($w - $deltawidth + $sbsize + 10) . 'x' . ($h - $deltaheight + $sbsize); print "old $geo new $newgeo\n"; $mw->geometry( $newgeo ); } sub changegrid { $_->destroy for $f->children; foreach my $row (0..$rows - 1) { foreach my $col (0..$columns - 1) { $f->Entry( -text => "rc $row $col", -width => 0, )->grid(-row => $row, -column => $col, -sticky => 'news'); $row or $f->gridColumnconfigure($col, -weight => 1); } $f->gridRowconfigure($row, -weight => 1); } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found