Creating a Frame

There is nothing special about creating a Frame widget, except you will usually save a reference to the widget so that you can put other things inside it.

# a Frame you'll never see
$frame1 = $mw->Frame; 
$frame1->Button(-text => 'button1')->pack;
$frame1->Button(-text => 'button2')->pack;

# a more visible Frame
$frame2 = $mw->Frame(-borderwidth => 2, -relief => 'groove');
$frame2->Button(-text => 'button1, frame2')->pack;
$frame2->Button(-text => 'button2, frame2')->pack;

# a Frame in a Frame
$frame3 = $frame2->Frame(-borderwidth => 3, -relief => 'raised');
$frame3->Button(-text => 'button1, frame3')->pack;

$frame3->pack;  # not visible yet
$frame2->pack;  # still nothing visible...
$frame1->pack;  # now we can see all the Frames and Buttons

The Frame’s parent can be a MainWindow, a Toplevel, or another Frame widget.[3] After the Frame is created, it can become a parent to other widgets. You must have created the Frame but not necessarily packed it on the screen for it to be the parent of other widgets. Keep in mind that even if you pack other widgets inside your Frame, if you don’t pack the Frame as well, the other widgets won’t show on the screen.

[3] Technically, any widget can be a parent of another widget, but life is easier when widgets are children of a Frame, or else we have to use the -in option with pack, confusing everybody. Keep it simple, and you’ll be much happier.

Get Mastering Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.