16.1. Building a Family Tree

The following methods deal with the ancestors or children of widgets and how they were created: children, name, parent, toplevel, manager, and class.

16.1.1. Widget's Children

To determine the children of a widget (usually a toplevel or a frame), use the children method:

@kids = $widget->children();
# i.e. Tk::Button=HASH(0x85e3a0) Tk::Button=HASH(0x85e4a8)

The list returned contains scalars that are the children of $widget. You can then use those references to perform actions such as setting a background color or font.

16.1.2. Name of a Widget

To determine what the parent calls the widget use the name method:

$name = $widget->name();

You can combine the nameand childrenmethod like this:

@kids = $widget->children();
foreach (@kids) {
  print "Name: ", $_->name(), "\n";
}

Here is example output from that code:

button
button1

16.1.3. Parent of a Widget

To get a reference to the parent of a widget, use the parentmethod:

$parent = $widget->parent();

16.1.4. The Widget's Toplevel

To get the toplevel widget that contains a widget, use toplevel:

$path = $widget->toplevel();

The $path returned is a number (that is, 8606484) that you can compare to another number that was returned from another call to toplevel to see if they are equal.

16.1.5. Widget's Manager

You can find out which geometry manager $widget used by calling manager:

$manager = $widget->manager();

It returns a string that describes the geometry manager; for instance, if it is a toplevel ...

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.