Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Tk and optionAdd's scope

by eff_i_g (Curate)
on Apr 15, 2010 at 18:47 UTC ( [id://834946]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to understand Tk's customization with optionAdd. I can get it to work on a global level, but not on a more specific level (without undesired side effects). The comments in the code below explain what I'm after and what I get:
use warnings; use strict; use Tk; my $mw = MainWindow->new(); $mw->geometry('200x200'); ### Globally, I want the font to be this... $mw->optionAdd('*font', 'helvr12'); ### ...but I want my LabFrame's to be bold. ### This does not make the label for the frame bold, but ### oddly enough the print statement below returns 'helvb12'! $mw->optionAdd('*LabFrame.font', 'helvb12'); ### The line below will make the label for the frame bold, but ### it also changes its child (the Label), which I don't want. #$mw->optionAdd('*LabFrame*font', 'helvb12'); my $lab_frm = $mw->LabFrame(-label => 'LabFrame Test')->pack; $lab_frm->Label(-text => 'Label Test')->pack; print STDERR $lab_frm->optionGet('font', ref $lab_frm), "\n"; MainLoop;
Questions:
  1. Why does the LabFrame report its font as 'helvb12' when it doesn't appear bold?
  2. How can I get the LabFrame to appear bold without changing its children?

Replies are listed 'Best First'.
Re: Tk and optionAdd's scope
by PeterPeiGuo (Hermit) on Apr 16, 2010 at 02:17 UTC

    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

      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

Re: Tk and optionAdd's scope
by Khen1950fx (Canon) on Apr 16, 2010 at 07:01 UTC
    Here's my stab at it:
    #!/usr/bin/perl use strict; use warnings; use Tk; require Tk::LabFrame; require Tk::LabEntry; my $mw = MainWindow->new; $mw->geometry("150x80"); #$mw->optionAdd( "LabFrame.font", "helvb12" ); my $canvas_total = "Label Test"; my $le_t_text = "LabFrame Test"; my $le = $mw->LabEntry( -textvariable => \$canvas_total, -labelVariable => \$le_t_text, -labelPack => [ -anchor => "center" ] ); $le->pack( -expand => 1, -fill => "y" ); #print STDERR $le->optionGet( "font", ref $le ), "\n"; MainLoop;
    I used the labelpack example from the source as a starting point. Note that I commented out the optionAdd because it didn't make any difference. Also the print statement.
      This works because LabFrames are bold by default. In my example I'm changing the default font at a global level:
      $mw->optionAdd('*font', 'helvr12');
      As a result the LabFrame's are no longer bold and I'm trying to revert them.
        I understand. I went as far as I could without having to modify my own option database setup. You should probably try Tk::Xrm.
Re: Tk and optionAdd's scope
by stefbv (Curate) on Apr 17, 2010 at 17:18 UTC
    I can get it to work on a global level, but not on a more specific level ...

    I believe that "optionAdd" is meant to be used only on a global level, for "more specific level" you have to use the widget options.

    Regards, Stefan

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://834946]
Approved by ikegami
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-18 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found