Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Perl/TK: how to set defaults for the whole script?

by perltux (Scribe)
on Oct 24, 2012 at 07:14 UTC ( #1000558=perlquestion: print w/ replies, xml ) Need Help??
perltux has asked for the wisdom of the Perl Monks concerning the following question:

With Perl/TK how do i change the inbuilt defaults for the whole script?

For example I want all Scale widgets to have the following settings:

-width => '10', -length => '200', -sliderlength => '16', -orient => 'horizontal', -borderwidth => '1', -cursor => 'hand2', -showvalue => '0'

How can I define that once for the whole script, as opposed to defining it each time I build a new Scale widget?

Comment on Perl/TK: how to set defaults for the whole script?
Download Code
Re: Perl/TK: how to set defaults for the whole script?
by choroba (Canon) on Oct 24, 2012 at 07:43 UTC
    You do not have to type the whole thing every time, just keep it in a variable:
    my %Scale_defaults = ( -width => 10, -length => 200, -sliderlength => 16, -orient => 'horizontal', -borderwidth => 1, -cursor => 'hand2', -showvalue => 0 ); # ... my $s = $mainwindow->Scale(%Scale_defaults, -showvalue => 1); # override a default
    If even this is not enough, you can subclass Scale and instantiate objects of the new class instead.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks, that sounds like a good solution to me!
Re: Perl/TK: how to set defaults for the whole script?
by stefbv (Deacon) on Oct 24, 2012 at 07:59 UTC

    Use a function.

    sub build_scale { my $frame = shift; my $scale = $frame->Scale( -width => '10', -length => '200', -sliderlength => '16', -orient => 'horizontal', -borderwidth => '1', -cursor => 'hand2', -showvalue => '0' ); return $scale; }
Re: Perl/TK: how to set defaults for the whole script?
by kcott (Abbot) on Oct 24, 2012 at 08:24 UTC

    G'day perltux,

    You can put all your default options in a hash.

    my %default_scale_options = ( -width => '10', -length => '200', -sliderlength => '16', -orient => 'horizontal', -borderwidth => '1', -cursor => 'hand2', -showvalue => '0' ); ... my $scale1 = $parent->Scale(%default_scale_options); ... my $scaleN = $parent->Scale(%default_scale_options);

    An alternative to this is to use an option database. You can set this up external to your script (e.g. with a .Xdefaults file) allowing access by multiple scripts; you can also use optionAdd(), and related methods, to handle options within a single script. See option for details.

    Here's an example of usage extracted from one of my own scripts. configure_option_database() is called after MainWindow->new() but before any widgets are created.

    sub configure_option_database { ... my $priority = 'startupFile'; # 40 ... # Button $mw->optionAdd('*Button*borderWidth', 1, $priority); $mw->optionAdd('*Button*activeBorderWidth', 1, $priority); ... # Entry $mw->optionAdd('*Entry*background', '#ffffff', $priority); $mw->optionAdd('*Entry*borderWidth', 1, $priority); $mw->optionAdd('*Entry*cursor', 'xterm', $priority); ... }

    -- Ken

      Thanks, in this case I will use the hash solution, the options database solution looks interesting too, but is overkill in this case for my specific script.
Re: Perl/TK: how to set defaults for the whole script?
by AnomalousMonk (Abbot) on Oct 24, 2012 at 21:04 UTC
    ... keep it in a variable ...

    Not being quite as adventurous as choroba or kcott, I will often use a constant for something like this. The other alternative is a Readonly hash, but  Readonly imposes execution overhead that may be undesirable. The functional approach of stefbv is also nice.

    use constant SCALE_DEFAULTS => qw(-width 10 -length 200 -etc 42); my $s = $MW->Scale(SCALE_DEFAULTS, -override => 1, etc...);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1000558]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2015-07-03 23:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls