Derived Mega-Widgets

A derived widget is one directly descended from another widget rather than being comprised of two or more widgets. A classic example of OO subclassing is Dialog, a widget derived from DialogBox. Looking at Dialog’s @ISA array:

use base qw(Tk::DialogBox);

you might wonder why Tk::Derived isn’t included. The answer is Tk::DialogBox is itself a mega-widget and Tk::Derived is already part if its @ISA array (see Figure 14-5).

Tk::NavListbox

If you see limitations in the standard Listbox and want to make it more useful, perhaps the best thing to do is create your own derived widget. Let’s start with a Listbox widget and add some features that allow a user to edit the items without need for additional controls.

We’ll add these enhancements to our version of Listbox:

  • Display one widget only, not a combination of widgets. We want to keep the widget simple for future programmers.

  • Still be able to use scrollbars by using the built-in Scrollbars method, or let users attach scrollbars themselves.

  • Provide a popup menu to give the user a mechanism to manipulate Listbox entries.

  • Allow the programmer to define a validation routine for new/renamed entries; e.g., end user can enter only numbers.

Figure 14-10 shows what the NavListbox widget looks like while the user is editing an entry.

NavListbox, a derived mega-widget

Figure 14-10.  NavListbox, a derived mega-widget

The code to use the NavListbox might look like ...

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.