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

Is there any way to specify from a Perl/Tk script that (for instance) all Buttons should have the -background option set to 'white'?

I could subclass the Button widget, but going through all my files and changing all instances of $parent->Button to $parent->MyButton seems klugey at best.

Is it possible to tinker with the X option database programatically? If so, then I could say "myapp.Button.background" should be white, or something to that effect.

TIA, Bill

  • Comment on Setting Standard options for Tk widgets

Replies are listed 'Best First'.
Re: Setting Standard options for Tk widgets
by bobn (Chaplain) on Jun 09, 2003 at 01:15 UTC
    Tk::option::optionAdd in the Tk::option module may be what you are looking for. Described in Chapter 16 of 'Mastering Perl/Tk'.


    --Bob Niederman, http://bob-n.com
      Here's an example:
      #!/usr/bin/perl -w use Tk; use strict; my $mw = MainWindow->new; $mw->packPropagate(0); $mw->optionAdd("*Button.Background", "red"); print "ARGV=@ARGV.\n"; my $l1 = $mw->Label(-text => "Application class/name is\n'" . $mw->class . '/' . $mw->name . "' +"); $l1->pack; my $b1 = $mw->Button(-background => 'yellow')->pack; my $b2 = $mw->Button->pack; my $b3 = $mw->Button('Name' => 'B3')->pack; foreach ($mw->children) { next if ref $_ eq 'Tk::Label'; $_->configure(-text => "'" . $_->PathName . "'"); } MainLoop;


      Run this with and without the $mw->optionAdd line to see the effect.

      --Bob Niederman, http://bob-n.com
      ++ That's what I was looking for!

      I even looked throught Mastering Perl/Tk. I guess I missed it. Thanks a lot!

      Bill

Re: Setting Standard options for Tk widgets
by JamesNC (Chaplain) on Jun 09, 2003 at 00:50 UTC
    use kids and iterate through them...
    use Tk; my $mw = tkinit(-bg,'gray'); my $btn1 = $mw->Button(-text, "Hello There")->pack; my $btn1 = $mw->Button(-text, "Button 2")->pack; my $text = $mw->Text(-width, 40,-height, 5)->pack; my $super = ref $button; @kids = $mw->children; print @kids; foreach(@kids){ if(/Tk::Button/){ $_->configure(-bg, 'green'); } if(/Tk::Text/){ $_->configure(-bg, 'yellow'); } } MainLoop;
    JamesNC
      i'm looking more for a way to specify in advance what the options should be....