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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.