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

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

Hi
In my Tk gui application, there are three frames,namely $userframe (at the top), hlistframe (in the middle having HList object) and bottomframe (having a 'Display File' button on it). The problem is , when I contract the gui by holding and dragging the right-bottom corner, the bottomframe and also the button on it is disappearnig. Please help me how can I make both the bottomframe and the button to reduce proportionately so that they don't disappear.

Below is my code

use Tk; use Tk::HList; use Tk::ItemStyle; use Tk::BrowseEntry; my $mw = new MainWindow; $mw->title("View"); $mw->geometry("560x320"); my $userframe = $mw->Frame(-width=>5,-height=>10)->pack(-side=>'top',- +anchor=>'nw'); $userframe->Label(-text => "USER: $user")->pack(-side => 'left',-ancho +r => 'w',-padx => 0); $userframe->Label(-text => "Set time(sec)")->pack(-side => 'left',-anc +hor => 'w',-padx => 0); my $frequency = $userframe->Entry(-width=> 5,-textvariable=> \$sec)->p +ack(-side => 'left',-anchor => 'nw',-padx => 0); $userframe->Label(-text => "Select File")->pack(-side => 'left',-ancho +r => 'w',-padx => 0); $userframe->BrowseEntry( -state => 'normal', -choices => [keys %url], -variable => \$selectfile, -browsecmd => \&read_file, )->pack(-side=>'right',-anchor =>' +e'); my $hlistframe = $mw->Frame()->pack(-fill => 'both',-expand=>1); my $h +l; my $bottomframe = $mw->Frame()->pack(-fill => 'both', -expand => 1); $bottomframe->Button(-text => "Display File",-command => \&dumpOnTermi +nal)->pack(-side=>'right',-anchor => 'se',-fill=>'both'); my ($label1,$label2,$label3,$label4,$label5,$label6,$label7); $hl = $hlistframe->Scrolled('HList', -scrollbars => 'ose', -columns =>7 , -header => 1, -width => 100, -command => sub {print "AAA\n";}, )->pack(-fill => 'both',-expand =>1 ); $label1 = $hl->Label(-text => "Tool", -anchor => 'w'); $hl->headerCreate(0,-itemtype => 'window',-widget => $label1) ; $label2 = $hl->Label(-text => "Vendor", -anchor => 'w'); $hl->headerCreate(1,-itemtype => 'window',-widget => $label2); $label3 = $hl->Label(-text => "Available", -anchor => 'w'); $hl->headerCreate(2,-itemtype => 'window',-widget => $label3); $label4 = $hl->Label(-text => "num.", -anchor => 'w'); $hl->headerCreate(3,-itemtype => 'window',-widget => $label4); $label5 = $hl->Label(-text => "usage%", -anchor => 'w'); $hl->headerCreate(4,-itemtype => 'window',-widget => $label5); $label6 = $hl->Label(-text => "user", -anchor => 'w'); $hl->headerCreate(5,-itemtype => 'window',-widget => $label6); $label7 = $hl->Label(-text => "user%", -anchor => 'w'); $hl->headerCreate(6,-itemtype => 'window',-widget=> $label7); MainLoop;

Replies are listed 'Best First'.
Re: perl tk frame packing question
by stefbv (Curate) on Aug 17, 2013 at 15:41 UTC
    Here is my solution. The main differences are:
    • add minimum size for the main window
    • top and bottom frame have expand = 0 and fill = 'x'
    • Add height = -1 to HList
    use strict; use warnings; use Tk; use Tk::HList; use Tk::ItemStyle; use Tk::BrowseEntry; use Tk::FontDialog; my $mw = MainWindow->new; $mw->title("View"); $mw->minsize(560, 180); my ($user, $sec, $selectfile) = ('user', 30, 'file'); my %url; my $userframe = $mw->Frame()->pack( -side => 'top', -fill => 'both', -expand => +0 ); my $hlistframe = $mw->Frame()->pack( -side => 'top', -fill => 'both', -expand => +1 ); my $bottomframe = $mw->Frame()->pack( -fill => 'x', -expand => 0 ); my $menubutton = $userframe->Menubutton( -text => "Font", )->pack( -side => 'left +' ); $menubutton->command( -label => "Change", -command => sub { my $font = $mw->FontDialog->Show; $mw->RefontTree( -font => $font ) if defined $font; } ); $userframe->Label( -text => "USER: $user" ) ->pack( -side => 'left', -anchor => 'w', -padx => 10 ); $userframe->Label( -text => "Set time(sec)" ) ->pack( -side => 'left', -anchor => 'w', -padx => 10 ); my $frequency = $userframe->Entry( -width => 5, -textvariable => \$sec + ) ->pack( -side => 'left', -anchor => 'nw', -padx => 10 ); $userframe->Label( -text => "Select File" ) ->pack( -side => 'left', -anchor => 'w', -padx => 10 ); $userframe->BrowseEntry( -state => 'normal', -choices => [ keys %url ], -variable => \$selectfile, -browsecmd => \&read_file, )->pack( -side => 'left', -anchor => 'w' ); my $hl = $hlistframe->Scrolled( 'HList', -scrollbars => 'ose', -columns => 7, -header => 1, -height => -1, -command => sub { print "AAA\n"; }, )->pack( -fill => 'both', -expand => 1 ); my $label1 = $hl->Label( -text => "Tool", -anchor => 'w' ); $hl->headerCreate( 0, -itemtype => 'window', -widget => $label1 ); my $label2 = $hl->Label( -text => "Vendor", -anchor => 'w' ); $hl->headerCreate( 1, -itemtype => 'window', -widget => $label2 ); my $label3 = $hl->Label( -text => "Available", -anchor => 'w' ); $hl->headerCreate( 2, -itemtype => 'window', -widget => $label3 ); my $label4 = $hl->Label( -text => "num.", -anchor => 'w' ); $hl->headerCreate( 3, -itemtype => 'window', -widget => $label4 ); my $label5 = $hl->Label( -text => "usage%", -anchor => 'w' ); $hl->headerCreate( 4, -itemtype => 'window', -widget => $label5 ); my $label6 = $hl->Label( -text => "user", -anchor => 'w' ); $hl->headerCreate( 5, -itemtype => 'window', -widget => $label6 ); my $label7 = $hl->Label( -text => "user%", -anchor => 'w' ); $hl->headerCreate( 6, -itemtype => 'window', -widget => $label7 ); $bottomframe->Button( -text => "Display File", -command => \&dumpOnTer +minal ) ->pack( -side => 'right', -anchor => 'ne', -fill => 'x' ); MainLoop;

    This answers also to your other question regarding the font and window dimensions change.

    Update: Changed the intro paragraph. The dimension of the window doesn't always change, only when it accommodates to bigger contents.

    Update2: Updated the update :-)

      Hi
      One problem that I am getting is , the font is always set to Helvetica and size as 9. Even if I change it to something else it is again getting resetted to that Helvetica,9.
      Also since my gui is getting refeshed after few seconds, that is resulting in giving the old font back. The ItemStyles inside the hlistframe is changing back to the old font.

        One problem that I am getting is , the font is always set to Helvetica and size as 9.

        That happens when you close and restart the application, right? The "old font" is the default font, in this case probably Helvetica,9. If you restart the application the default font will be used. Use a configuration file to save the desired font and apply it at the application start.

        The same for the HList, if you destroy and recreate it you have to reapply the desired font.

Re: perl tk frame packing question
by choroba (Cardinal) on Aug 17, 2013 at 15:47 UTC
    That is how Tk works. You can get a slightly better behaviour if you do not set the geometry and height and width of various widgets, but sometimes the window is simply too small to show everything.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ