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

hi monks, I've programmed a chat system. client interface code like below:
$left_frame->Label(-text => "Perl Chat Client v$VERSION") ->pack(-side => 'top', -anchor => 'nw'); $status = $left_frame->Text (-width => 40, -height => 30, -wrap => 'word')->pack(); $status->tagConfigure(section, -font => '-adobe-helvetica-bold-r-normal--14-140-75-75-p-82-iso8 +859-1'); $status->bind('<Double-1>', \&pick_word); $left_frame->Label(-text => 'Global Message:')->pack(-side => 'left +', -anchor => 's'); $gm_quick = $left_frame->Entry (-width => 26)->pack(-side => 'left' +, -anchor => 's'); $gm_quick->bind('<KeyPress-Return>', sub { send_msg_all($gm_quick->get +()); $gm_quick->delete(0,'end');
user complained text widget would occupy all screen of the window when they shrink the window. they hope Entry widget is the last widget they can see in the screen.describing my problem clearly seems a little difficult.If you confuse or misunderstand on my problem, please reply or msg me to let me know.


I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: treat widget when resizing window
by shmem (Chancellor) on May 29, 2007 at 10:14 UTC
    Your code is incomplete, the anonymous sub lacks curlies, there's no MainWindow definition and the MainLoop is missing. Please provide a running example next time.

    That said, you get what you want if you pack the Label and Entry widgets into a frame of their own and anchor that at the bottom of $left_frame before adding the other widgets (see also the -after and -before arguments to pack() for an alternative):

    #!/usr/bin/perl use Tk; my $mw = MainWindow->new(); $left_frame = $mw->Frame->pack(qw(-fill both -expand 1)); $bottom_frame = $left_frame->Frame->pack(qw(-side bottom -expand 1 -fi +ll x)); $left_frame->Label(-text => "Perl Chat Client v$VERSION") ->pack(-side => 'top', -anchor => 'nw', ); $status = $left_frame->Text (-width => 40, -height => 30, -wrap => 'word')->pack(qw(-expand 1 -f +ill both)); $status->tagConfigure(section, -font => '-adobe-helvetica-bold-r-normal--14-140-75-75-p-82-iso8 +859-1'); $status->bind('<Double-1>', \&pick_word); $bottom_frame->Label(-text => 'Global Message:')->pack(qw(-side left - +anchor s)); $gm_quick = $bottom_frame->Entry (-width => 26)->pack(qw(-side left -a +nchor s -fill x -expand 1)); $gm_quick->bind('<KeyPress-Return>', sub { send_msg_all($gm_quick->get +()); $gm_quick->delete(0,'end'); } ); MainLoop;

    The qw(-fill x -expand 1) arguments to pack() are provided for the text and entry widgets to resize nicely.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Thanks for your help.
      It really does work. But I'd like to know why also. In your code, -expand and fill options seem to be key to resize automatically. According to Tk manual, expand option causes the allocation rectangle to fill the remaining space available in the window or Frame, and fill option causes the widget to fill the allocation rectangle in the specified direction. I've tried some piece of code as below:
      # your code, absolutly works. #!/usr/bin/perl use Tk; my $mw = MainWindow->new(); $left_frame = $mw->Frame->pack(qw(-fill both -expand 1)); $bottom_frame = $left_frame->Frame->pack(qw(-side bottom -expand 1 -fi +ll x)); $left_frame->Label(-text => "Perl Chat Client v$VERSION") ->pack(-side => 'top', -anchor => 'nw', ); $status = $left_frame->Text (-width => 40, -height => 30, -wrap => 'word')->pack(qw(-expand 1 -f +ill both)); #$status->tagConfigure(section, # -font => '-adobe-helvetica-bold-r-normal--14-140-75-75-p-82-iso +8859-1'); $status->bind('<Double-1>', \&pick_word); $bottom_frame->Label(-text => 'Global Message:')->pack(qw(-side left - +anchor s)); $gm_quick = $bottom_frame->Entry (-width => 26)->pack(qw(-side left -a +nchor s -fill x -expand 1)); $gm_quick->bind('<KeyPress-Return>', sub { send_msg_all($gm_quick->get +()); $gm_quick->delete(0,'end'); } ); MainLoop; # Entry disappear ! use Tk; my $mw = MainWindow->new(); my $fm = $mw->Frame->pack(-fill => 'both', -expand => 1); $fm->Label(-text => "Perl chat") ->pack; $fm->Text->pack(-side => 'top'); my $sub_fm = $fm->Frame->pack(-fill => 'both', -expand => 1 , -side => + 'bottom'); $sub_fm->Entry->pack(-fill => 'x' , -expand => 1); MainLoop; # titto! use Tk; my $mw = MainWindow->new(); my $fm = $mw->Frame->pack(-fill => 'both', -expand => 1); $fm->Label(-text => "Perl chat") ->pack; $fm->Text->pack(-side => 'top'); #my $sub_fm = $fm->Frame->pack(-fill => 'both', -expand => 1 , -side = +> 'bottom'); $fm->Entry->pack(-fill => 'x' , -expand => 1); MainLoop;
      I'm totally confused! I was wondering whether I don't really understand your code.
      Please the gurus could explain it more clearly since I'm a beginner.
      Any reply would be appreciated.

      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction