Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  TK::MEGA (3)

.ds Aq ’


Tk::mega - Perl/Tk support for writing widgets in pure Perl



Define the widget’s new class name:

<B>package Tk::B>MyNewWidget;

For composite widget classes:

<B>use base qw/ Tk::container /B>; # where <B>containerB> is Frame or Toplevel

For derived widget classes:

<B>use base qw/ Tk::Derived Tk::DerivedWidget /;B>

Install the new widget in Tk’s namespace and establish class and instance constructors.

<B>Construct Tk::B>Widget ’MyNewWidget’;

<B>sub ClassInitB> { my ($self, $args) = @_; ... }

<B>sub PopulateB> { my ($self, $args) = @_; ... }


The goal of the mega-widget support of Perl/Tk is to make it easy to write mega-widgets that obey the same protocol and interface that the Tk core widgets support. For mega-widget sample code please run the widget demonstration program and go to the section Sample Perl Mega-Widgets.

There are two kinds of mega-widgets:
o Composite Widgets

A composite widget is composed with one or more existing widgets. The composite widget looks to the user like a simple single widget. A well known example is the file selection box.

o Derived Widgets

A derived widget adds/modifies/removes properties and methods from a single widget (this widget may itself be a mega-widget).



Give a subwidget a symbolic name.



Gives a subwidget $widget of the mega-widget $self the name <B>nameB>. One can retrieve the reference of an advertised subwidget with the Subwidget method.

<B>Comment:B> Mega-Widget Writers: Please make sure to document the advertised widgets that are intended for public use. If there are none, document this fact, e.g.:




Invoke a callback specified with an option.


$self-><B>CallbackB>(-option ?,args ...?);

<B>CallbackB> executes the callback defined with $self-><B>ConfigSpecsB>(-option, [<B>CALLBACKB>, ...]); If args are given they are passed to the callback. If -option is not defined it does nothing.


Initialization of the mega-widget class.


<B>sub ClassInitB> { my ($class, $mw) = @_; ... }

<B>ClassInitB> is called once for each MainWindow just before the first widget instance of a class is created in the widget tree of <B>MainWindowB>.

<B>ClassInitB> is often used to define bindings and/or other resources shared by all instances, e.g., images.


 $mw->bind($class,"<Tab>", sub { my $w = shift; $w->Insert("\t"); $w->focus; $w->break});
 $mw->bind($class,"<Return>", [Insert,"\n"]);

Notice that $class is the class name (e.g. <B>Tk::MyTextB>) and $mw is the mainwindow.

Don’t forget to call $class-><B>SUPER::ClassInit($mw)B> in <B>ClassInitB>.


Convenience function to create subwidgets.


    $cw->Component(Whatever, AdvertisedName,
        -delegate => [method1, method2, ...],
        ... more widget options ...,

<B>ComponentB> does several things for you with one call:

o Creates the widget

o Advertises it with a given name (overridden by ’Name’ option)

o Delegates a set of methods to this widget (optional)


    $cw->Component(Button, quitButton, -command => sub{$mw->destroy});


Defines options and their treatment


        -option => [ where, dbname, dbclass, default],
        DEFAULT => [where],

Defines the options of a mega-widget and what actions are triggered by configure/cget of an option (see Tk::ConfigSpecs and Tk::Derived for details).


Make the new mega-widget known to <B>TkB>.


<B>ConstructB> baseclass <B>’Name’B>;

<B>ConstructB> declares the new widget class so that your mega-widget works like normal Perl/Tk widgets.


<B>Construct Tk::WidgetB> ’Whatever’; <B>Construct Tk::MenuB> ’MyItem’;

First example lets one use $widget-><B>WhateverB> to create new <B>WhateverB> widget.

The second example restricts the usage of the <B>MyItemB> constructor method to widgets that are derived from <B>MenuB>: $isamenu->MyItem.


Process options before any widget is created:

<B>sub CreateArgsB> { my ($package, $parent, $args) = @_; ...; return @newargs; }

$package is the package of the mega-widget (e.g., <B>Tk::MyTextB>, $parent the parent of the widget to be created and $args the hash reference to the options specified in the widget constructor call.

Don’t forget to call $package-><B>SUPER::CreateArgsB>($parent, $args) in <B>CreateArgsB>.


Redirect a method of the mega-widget to a subwidget of the composite widget


        method1 => $subwidget1,
        method2 => advertived_name,
        Construct => $subwidget2,
        DEFAULT   => $subwidget3,

The <B>’Construct’B> delegation has a special meaning. After ’Construct’ is delegated all Widget constructors are redirected. E.g. after


a $self-><B>ButtonB> does really a $subframe-><B>ButtonB> so the created button is a child of $subframe and not $self.

<B>Comment:B> Delegates works only with methods that $cw does not have itself.


Note: this method should not, in general, be used, as it has been superceeded by Populate and specifying Tk::Derived as one of the base classes.

Defines construction and interface of derived widgets.


    sub InitObject {
        my ($derived, $args) = @_;

where $derived is the widget reference of the already created baseclass widget and $args is the reference to a hash of -option-value pairs.

<B>InitObjectB> is almost identical to Populate method. <B>PopulateB> does some more ’magic’ things useful for mega-widgets with several widgets.

Don’t forget to call $derived-><B>SUPER::InitObjectB>($args) in <B>InitObjectB>.


Define a callback invoked when the mega-widget is destroyed.



<B>OnDestroyB> installs a callback that’s called when a widget is going to to be destroyed. Useful for special cleanup actions. It differs from a normal <B>destroyB> in that all the widget’s data structures are still intact.

<B>Comment:B> This method could be used with any widgets not just for mega-widgets. It’s listed here because of it’s usefulness.


Defines construction and interface of the composite widget.


    sub Populate {
        my ($self, $args) = @_;

where $self is the widget reference of the already created baseclass widget and $args is the reference to a hash of -option-value pairs.

Most the other support function are normally used inside the <B>PopulateB> subroutine.

Don’t forget to call $cw-><B>SUPER::PopulateB>($args) in <B>PopulateB>.


Set/get a private hash of a widget to storage composite internal data


$hashref = $self-><B>privateDataB>();

$another = $self-><B>privateDataB>(unique_key|package);


Get the widget reference of an advertised subwidget.

@subwidget = $cw-><B>SubwidgetB>();

$subwidget = $cw-><B>SubwidgetB>(name);

@subwidget = $cw-><B>SubwidgetB>(name ?,...?);

Returns the widget reference(s) of the subwidget known under the given name(s). Without arguments, return all known subwidgets of $cw. See Advertise method how to define name for a subwidget.

<B>Comment:B> Mega-Widget Users: Use <B>SubwidgetB> to get only documented subwidgets.


o Resource DB class name

Some of the standard options use a resource date base class that is not equal to the resource database name. E.g.,

  Switch:            Name:             Class:

  -padx              padX              Pad
  -activerelief      activeRelief      Relief
  -activebackground  activeBackground  Foreground
  -status            undef             undef

One should do the same when one defines one of these options via <B>ConfigSpecsB>.

o Method delegation

Redirecting methods to a subwidget with <B>DelegateB> can only work if the base widget itself does have a method with this name. Therefore one can’t ‘‘delegate’’ any of the methods listed in Tk::Widget. A common problematic method is <B>bindB>. In this case one as to explicitely redirect the method.

  sub bind {
      my $self = shift;
      my $to = $self->privateData->{my_bind_target};

o privateData

Graham Barr wrote: ... It is probably more private than most people think. Not all calls to privateData will return that same HASH reference. The HASH reference that is returned depends on the package it was called from, a different HASH is returned for each package. This allows a widget to hold private data, but then if it is sub-classed the sub-class will get a different HASH and so not cause duplicate name clashes.

But privateData does take an optional argument if you want to force which HASH is returned.

o Scrolled and Composite

<B>ScrolledB>(Kind,...) constructor can not be used with <B>CompositeB>. One has to use $cw-><B>CompositeB>(<B>ScrlB>Kind => <B>’name’B>, ...);


Of course Perl/Tk does not define support function for all necessities. Here’s a short list of things you have to handle yourself:
o No support to define construction-time only options.
o No support to remove an option that is known to the base widget.
o It’s hard to define <B>undefB> as fallback for an widget option that is not already <B>undefB>.
o Frame in Perl/Tk carries magic and overhead not needed for composite widget class definition.
o No support methods for bindings that are shared between all widgets of a composite widget (makes sense at all?)


mega, composite, derived, widget


Tk::composite Tk::ConfigSpecs Tk::option Tk::callbacks Tk::bind
Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 MEGA (3) 2013-11-15

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.