jpream has asked for the wisdom of the Perl Monks concerning the following question:

First attempt at creating a mega widget and I can't seem to get the components to take on the default settings. The widget is a frame with two scrolled Listbox and a couple Buttons. I think I have the ConfigSpecs setup correctly, or at least how I've seen it in the books. And the configure method works correctly (only targets the Listboxes with -bg). Can somebody please clue me in to what I'm missing..? Thanks. jpream
package Tk::SelectBoxes; use Tk::widgets qw/Listbox Button/; use base qw(Tk::Frame); Construct Tk::Widget 'SelectBoxes'; use strict; sub ClassInit { my($class, $mw) = @_; $class->SUPER::ClassInit($mw); } sub Populate { my ($self,$args) = @_; $self->SUPER::Populate($args); ########################################### # Build SBox components ########################################### my $left_lb = $self->Scrolled("Listbox", -scrollbars => "osoe", -selectmode => "extended"); my $right_lb = $self->Scrolled("Listbox", -scrollbars => "osoe", -selectmode => "extended"); my $left_btn = $self->Button(-text => '<-', -command => [$self => 'Box2Box', $right_lb, $left_lb]); my $right_btn = $self->Button(-text => '->', -command => [$self => 'Box2Box', $left_lb, $right_lb]); $left_lb->pack(-side => "left", -expand => 1, -fill => "both"); $right_lb->pack(-side => "right", -expand => 1, -fill => "both"); $right_btn->pack(-anchor => "s", -expand => 1); $left_btn->pack(-anchor => "n", -expand => 1); ######################################### $self->ConfigSpecs( '-background' => [[$left_lb, $right_lb], 'background', 'Background' , 'black'], 'DEFAULT' => ['DESCENDANTS', 'SELF']); }

janitored by ybiC: Retitle from "ConfigDefault not working?"

Replies are listed 'Best First'.
Re: Tk::widgets ConfigDefault not working?
by zentara (Cardinal) on Nov 14, 2003 at 17:44 UTC
    Hi, I just came accross this by Stephen Lidie about creating a mega-widget, hope it helps. It uses Tk::Derived .
    #Stephen Lidie wrote: #There are at least two ways of doing it: ferreting out the actual #superclass callback and invoking it manually (not recommended), or #creating a proper mega-widget. The mega-widget method is cleaner and + #seems to do what you want. #Here is a modified version of the Emu example. First, note that I #added Tk::Derived to the module's base class because we need to turn + #this into a first class mega-widget - it's no longer a simple example +. #Second, the <Enter> binding has been deleted from the class #initialization method ClassInit(). ------------------------------------------------------------- use Tk; use strict; package MyButton; use base qw/Tk::Derived Tk::Button/; Construct Tk::Widget 'MyButton'; sub ClassInit { my ($class, $mw) = @_; $class->SUPER::ClassInit($mw); # $mw->bind($class, '<Enter>', sub{print "Entered a MyButton\n"}); } sub Populate { my ($self, $args) = @_; $self->SUPER::Populate($args); $self->bind('<Enter>', sub{print "Entered a MyButton\n"}); # my (@bt) = $self->bindtags; # $self->bindtags( [ @bt[ 1, 0, 2, 3 ] ] ); } 1; package main; my $mw = MainWindow->new; $mw->Button(-text => 'NormalButton')->pack; $mw->MyButton(-text => 'MyButton')->pack; MainLoop; #-------------------------------------------------------------- #The real change is the addition of Populate(), the class instance #constructor. It's responsible for creating the <Enter> binding. Now + #when you <Enter> a MyButton mega-widget it behaves exactly as a norma +l #Button does, plus, it prints a message.