15.1. Looking at an Example Sideways

I admit it. I like examples. They give me a starting point to come back to when I'm getting into the nitty-gritty. Since there is quite a bit of nitty-gritty with composite widgets, we'll start simple and work up from there.

If you look at the code for these composite widgets, the LabEntry has the smallest amount of code. Here is the LabEntry.pm widget code:

# Copyright (c) 1995-1997 Nick Ing-Simmons. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
# Class LabeledEntry

package Tk::LabEntry;
require Tk::Frame;
@ISA = qw(Tk::Frame);

Construct Tk::Widget 'LabEntry';

sub Populate
{
 require Tk::Entry;
 # LabeledEntry constructor.
 #
 my($cw, $args) = @_;
 $cw->SUPER::Populate($args);
 # Advertised subwidgets:  entry.
 my $e = $cw->Entry();
 $e->pack(-expand => 1, -fill => 'both');
 $cw->Advertise('entry' => $e);
 $cw->ConfigSpecs(DEFAULT => [$e]);
 $cw->Delegates(DEFAULT => $e);
 $cw->AddScrollbars($e) if (exists $args->{-scrollbars});
} 

1;

That's the complete set of code, comments and all. You can tell it's a frame-based composite widget because of the line @ISA = qw(Tk::Frame). We can look in Programming Perl (O'Reilly, 1997) to find out what the @ISA array is for: "Within each package a special array called @ISA tells Perl where else to look for a method if it can't find the method in that package." There's a lot more there about how this implements inheritance, but I ...

Get Learning 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.