The reason the window expands to such a large size when you add the label is that the width and height you are passing to the Label constructor are interpreted as the width in characters and the height in lines. Thus, you're requesting that the label be 100 lines tall, and 100 characters wide. The window expands to fit it.
Unfortunately, there doesn't really seem to be a way to specify the size of the label in pixels, unless you are displaying a bitmap. Judging from how you specify the desired new background color of the window as the label background color, however, you really want the label to fill up the window anyway. Thus, you should tell pack to fill and expand the label, and not give it a size.
This prevents the problem with the window expanding when you pack the label but uncovers the problem that the window now shrinks to the minimum possible size to accommodate all of its widgets (in this case, the menu and label). This is due to the way geometry management is done with Toplevel widgets by the Tk Window Manager interface (Tk::Wm). To get around this, you should use the geometry method for the MainWindow (it's documented under Tk::Wm).
Well, here's your code, updated with these changes.
#!/usr/local/bin/perl -w use 5.005; use Tk; use Tk::widgets qw/Dialog/; use subs qw/build_menubar fini/; use vars qw/$MW $VERSION/; use strict; $MW = MainWindow->new(); $MW->geometry('640x100'); my $menubar = build_menubar; my $Body = $MW->Frame(-background => 'cyan')->pack( -expand => 1, -fill => 'both', ); MainLoop; sub build_menubar { my $menubar = $MW->Menu; my $file = $menubar->cascade(-label => '~File'); $MW->configure(-menu => $menubar); $file->command(-label => "Print Text", -command => \&PrintText); $file->command(-label => "Quit", -command => \&fini); return $menubar; } sub PrintText{ my $lbl = $Body->Label( -text => "This is new text", -background => 'red', #-width => '100', # This would be 100 _characters_ #-height => '100', # And this 100 _lines_ )->pack( -fill => 'both', -expand => 1, ); } sub fini { exit; }
bbfu
Black flowers blossum
Fearless on my breath
In reply to (bbfu) Re: Perl/Tk menu item
by bbfu
in thread Perl/Tk menu item
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |