Book HomeMastering Perl/TkSearch this book

13.2. Building a Family Tree

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

These methods tend to return either a widget reference or a string. A string is the Tcl-like name of a widget. Tcl uses string pathnames to reference widgets, with periods as pathname separators, and is how Tcl's widget hierarchy is defined. This is analogous to how Unix uses a forward slash as its pathname separator.

In Tcl, the MainWindow is always ".". From that point on, it's the programmer's job to name widgets that reflect the application's widget hierarchy. So, .frame might refer to the first Frame widget created as a child of the MainWindow, .frame1 might refer to the second, and so on. The programmer might call the first Button under the first Frame .frame.quit_button; this is the fully qualified pathname. The name its parent knows this Button by is simply quit_button. This name is the leaf part of the pathname, or, in Unix terminology, the basename.

As Perl/Tk programmers, we seldom explicitly assign a pathname to our widgets, but pTk does so on our behalf. Now you know what's happening when you see string pathnames. Ideally, we like to see real Perl object references, but sometimes Perl/Tk's Tcl underpinnings peek through.

13.2.1. Widget's Children

To determine the children of a widget (usually a Toplevel or a Frame), use the children method, which returns a list of widget references:

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

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

13.2.2. Name of a Widget

To determine what the parent calls the widget (the widget's leaf or basename portion of its pathname), use the name method:

$name = $widget->name;

You can combine the name and children methods like this:

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

Here is example output from that code; notice you get strings:

button
button1

To see what string pathname Tk generated for a widget, use the PathName method. This method does not exist in Tcl/Tk, because you always know a widget's pathname. Do not confuse this method with pathname, described later.

print "\$button=", $button->PathName, "\n";
#i.e. .frame.button

13.2.3. Parent of a Widget

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

$parent = $widget->parent;

13.2.4. The Widget's Toplevel

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

$path = $widget->toplevel;

13.2.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 widget, it will return "grid", "pack", "place", or "wm". The manager method doesn't seem to work correctly on Windows 95, but it works on Unix and Windows NT.

13.2.6. The Widget's class

The class method returns a string that indicates the class to which it belongs. For example, $listbox->class returns "Listbox", and $menu->class returns "Menu".



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.