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.

Widget’s Children

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

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.