in reply to Tk and optionAdd's scope

Try to run the following and it proves that optionAdd does deliver the expected "scope":

use warnings; use strict; use Tk; use Tk::LabFrame; my $mw = new MainWindow ( ) ; $mw->optionAdd('*foo*font' => '-adobe-courier-plain-r-normal-*-30-*-*- +*-*-*-*'); $mw->optionAdd('*bar*font' => '-adobe-courier-bold-r-normal-*-30-*-*-* +-*-*-*'); $mw->optionAdd('*foo*foreground' => 'red'); $mw->optionAdd('*foo*background' => 'green'); $mw->optionAdd('*bar*foreground' => 'white'); $mw->optionAdd('*bar*background' => 'yellow'); my $frame = $mw->LabFrame(-label => 'Frame', Name => "foo")->pack; $frame->Label ( -text => 'foo', Name => "bar")->pack(-padx=>5, -pady=> +5) ; MainLoop();

Peter (Guo) Pei

Replies are listed 'Best First'.
Re^2: Tk and optionAdd's scope
by eff_i_g (Curate) on Apr 16, 2010 at 14:16 UTC
    Peter,

    This works, but the reason I turned to optionAdd was to avoid extra code within the widgets (laziness). Luckily, I've only a few LabFrames, but say I had 25. I would have to add 25 lines of Name = ... rather than (what I had hoped) one line of $mw->optionAdd('the font of every LabFrame', 'the font');. I thought "LabFrame" was already an identifier in itself, but apparently not.

    Thanks.

      optionAdd looks buggy, and I think you should just avoid it entirely. For example, it supposed to support -class, but apparently does not. If it supports -class as expected, you can simply put all 25 in the same class.

      Peter (Guo) Pei

        Eureka!

        The trick is in the path. LabFrame does not have a .font property, but its subwidget of Label does:
        use warnings; use strict; use Tk; my $mw = MainWindow->new(); $mw->geometry('200x200'); $mw->optionAdd('*font', 'helvr12'); $mw->optionAdd('*LabFrame.label.font', 'helvb12'); my $lab_frm = $mw->LabFrame(-label => 'LabFrame Test')->pack; $lab_frm->Label(-text => 'Label Test')->pack; print STDERR $lab_frm->Subwidget('label')->PathName, "\n"; MainLoop;
        Update: Another interesting finding. This does not work with LabEntry because, from what I can tell, it does not advertise its Label subwidget. This can, however, be worked around by changing both subwidgets, then reverting Entry:
        $mw->optionAdd('*LabEntry*font', 'helvb12'); $mw->optionAdd('*LabEntry.entry.font', 'helvr12');