in reply to Re: Specifying the -class of Tk subwidgets
in thread Specifying the -class of Tk subwidgets

use Tk; use lib '/genesis/oscripts/misc_scr/lib'; use OwegoCAM::GUI::GUIBuilder::LabelOption; my($mw) = MainWindow->new; my($fileloc) = "/genesis/oscripts/misc_scr/lib/OwegoCAM/GUI/GUIBuilder +/.Xdefaults.ocd"; $mw->optionReadfile($fileloc) if (-e $fileloc); my(%foo) = ('bar' => [qw/yes no maybe/]); my($bah) = $mw->LabelOption(-class => 'Lab2', -options => $foo{'bar'}, -label => 'Is it safe?', -labelPack => [-side => 'left'] )->pack(); MainLoop;
And here's an excerpt from the .Xdefaults:
*Lab2*optionmenu*activeForeground:#000000 *Lab2*optionmenu*activeBackground:tan3 *Lab2*optionmenu*background:tan3 *Lab2*optionmenu*font:-adobe-helvetica-bold-r-normal--*-120-*-*-*-*-*- +* *Lab2*optionmenu*foreground:#000000 *Lab2*optionmenu*relief:raised *Lab2*optionmenu*cursor:right_ptr *Lab2*optionmenu*width:15 *Lab2*optionmenu*anchor:w *Lab2*label*background:wheat1 *Lab2*label*font:-adobe-helvetica-bold-r-normal--*-120-*-*-*-*-*-* *Lab2*label*foreground:black *Lab2*label*relief:flat *Lab2*label*width:15 *Lab2*label*anchor:w
The only thing that actually gets a color set is the option menu background when the mouse is over it, and the optionmenu's width. Thanks, -Peter Oh, sorry. Rereading the original post, I've since changed the original module that I was using. The Tk widget code now looks like this (mostly lifted from Mastering Perl/Tk):
package LabelOption; use Tk::widgets qw /Optionmenu/; use base qw/Tk::Frame/; use strict; Construct Tk::Widget 'LabelOption'; sub ClassInit { my($class, $mw) = @_; $class->SUPER::ClassInit($mw); } sub Populate { my($self, $args) = @_; $self->SUPER::Populate($args); my($o) = $self->Optionmenu->pack(); $self->Advertise('optionmenu' => $o); $self->ConfigSpecs('DEFAULT' => [$o]); $self->Delegates('DEFAULT' => $o); } 1;

Replies are listed 'Best First'.
Re^3: Specifying the -class of Tk subwidgets
by lamprecht (Friar) on Jul 10, 2009 at 14:05 UTC
    Hi monksp,

    Here is a possible solution: First you need to direct -bg/-fg to 'SELF' otherwise these options propagate down the Descendants hierarchy of your composite overriding anything else. Then there is a problem with the Label created by Frames -labelPack option which you inherit in your Composite. I didn't dig into it further, but you can work around by creating your own Label (see below).

    use strict; use warnings; package LabelOption; use Tk::widgets qw /Optionmenu/; use base qw/Tk::Frame/; use strict; Construct Tk::Widget 'LabelOption'; sub ClassInit { my($class, $mw) = @_; $class->SUPER::ClassInit($mw); } sub Populate { my($self, $args) = @_; $self->SUPER::Populate($args); my $l = $self->Label()->pack(-side => 'left'); my $o = $self->Optionmenu->pack(-side => 'left'); $self->Advertise('optionmenu' => $o); $self->ConfigSpecs(-options => [$o], -class => [$self], -background => 'SELF', -foreground => 'SELF', -label => [{-text=> $l}], 'DEFAULT' => [$self], ); $self->Delegates('DEFAULT' => $self); } package main; use Tk; use Tk::Optionmenu; my $mw = tkinit; my $fileloc = "Xdefaults.ocd"; $mw->optionReadfile($fileloc) if -e ($fileloc); my(%foo) = ('bar' => [qw/yes no maybe/]); my $bah = $mw->LabelOption(-class => 'Lab2', -options => $foo{'bar'}, -label => 'Is it safe?', # -labelPack => [-side => 'left'], )->pack(); print join("\n" ,map {$_->PathName} $mw->Descendants); MainLoop;
    Cheers, Christoph
      Spectacular! Thank you so much. (=