Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

tk widget options stored in hash

by glenn (Scribe)
on Nov 13, 2013 at 18:52 UTC ( #1062429=perlquestion: print w/replies, xml ) Need Help??

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

So i'm trying to make my GUI look better and although i could put into every widget "-background => $color{lbl}[0], -foreground => $color{lbl}[0]" using an hash of arrays i would like to store all these in a hash so that if some have an active fg/bg colors as well it's all the same and alot cleaner, ultimately i could put all options in the hash. Has anybody tried to do this? I keep getting 'odd number of parameters' error. Thanks for the wisdom.
my %colors = ( but_quit => { '-background' => 'red', '-foreground' => 'black', }, lbl => { '-background' => 'tan', '-foreground' => 'black', }, ent => { '-background' => 'white', '-foreground' => 'black', }, ); $frame->LabEntry(-label=>"IP Address", -labelWidth=>$lblwidth, -labelP +ack=>[-side=>'left', -anchor=>'w'], -textvariable => \$ServiceSystem, + $colors{lbl} )
PS: i have also tried:
lbl => { '-background => tan, -foreground => black', },

Replies are listed 'Best First'.
Re: tk widget options stored in hash
by kcott (Bishop) on Nov 13, 2013 at 19:27 UTC

    G'day glenn,

    Predefining default options is a good idea. Your problem here is that your using a hashref instead of a hash: you want %{$colors{lbl}}, not $colors{lbl}.

    Also, if you put your defaults first, you can override them if you want to. E.g.

    $frame->LabEntry( %{$colors{lbl}}, -other_opt => 'other_value', -background => 'non_default_colour' );

    By the way, your "PS: i have also tried: ..." is wrong for all sorts of reasons. Use strict and warnings in all your scripts to pick up errors like this:

    $ perl -Mstrict -Mwarnings -e ' my %colours = ( lbl => { "-background => tan, -foreground => black +", } ); ' Odd number of elements in anonymous hash at -e line 2.

    -- Ken

      Thank you, i knew i was just missing something... of course wrap the d@#n thing as a hash.... worked perfectly. I appreciate the additional tip on declaring it first so that it could be overridden.
      Is there a way to get info about the tk widget your currently in? I have not found anything as such, but this would be very helpful.
      $frame->LabEntry(%{$colors{$_->class()}},....);

        If you're just interested in options, then cget() and configure() are probably the methods you want: see Tk::options.

        A lot of other methods providing information about widgets are documented in Tk::Widget.

        There's also information that's specific to particular widgets, geometry managers and so on. Spend some time getting to know where this is documented from the links provided in the Tk Distribution Documentation. See also equivalent documentation for User Contributed Widgets.

        -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2022-01-17 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (51 votes). Check out past polls.

    Notices?