O'Reilly logo

Mastering Perl/Tk by Nancy Walsh, Stephen Lidie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required