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

So I have a tk label which looks something like this:
$frame->Label( -textvariable => \$value )->pack();
Then later I try to set this variable $value to a referenced hash value. But if I make the assignment directly Tk gives me an error saying it can't use a "string" as a "hash ref". So instead i use this snippet of code that works:
my $value_buffer = $$move{'value'}; $value = $value_buffer;
assuming $move is a reference to a hash with key 'value': how do i just assign $value directly in one line? thanks a lot -justin bishop

Replies are listed 'Best First'.
Re: Setting value in Tk -textvariable
by Chmrr (Vicar) on Aug 06, 2002 at 01:18 UTC

    Tk's not the one complaining at you; perl is complaining that you're not doing what you think you're doing. If it's saying that it can't use a string as a hash reference, that means that $move isn't a hash reference, it's a string. Somewhere along the ways, you've gotten confused as to what's in what variable. I'd suggest a proper application of Data::Dumper to the variables in question to figure out what's going on.

    Here's a crude and fast example of setting a label's value to something in a hash reference:

    use Tk; my $main = tkinit; my $value = "Foo!"; my $href = {bar=>baz}; $main->Label(-textvariable=>\$value)->pack; $main->after(5000,sub{$value=$$href{bar}; $main->update;}); $main->MainLoop();

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: Setting value in Tk -textvariable
by Necos (Friar) on Aug 07, 2002 at 01:49 UTC
    Let's take Chmrr's example one step further... Here's a snippet of code that I use in one of my programs:
    sub _usr_next { $window_hash->{main}{children}{label}->configure(-textvariable, $w +indow_hash->{main}{children}{text}); }
    The configure method will set the parameter (in this case, -textvariable), to a new value. You can also use the get method to retrieve the value. So, let's say you need to update a label. You could make a button that calls the sample code as a callback. In total, it would look something like this. WARNING: UNTESTED!!!

    #/usr/bin/perl -w use strict; use Tk; my $window_hash = {}; $window_hash->{main}->{main} = new MainWindow(); $window_hash->{main}->{children} = {}; $window_hash->{main}->{children}->{frame} = $window_hash->{main}{main} +->Frame()->grid(-column, 0, -row, 0, -sticky, 'news'); $window_hash->{main}->{globals}->{test1} = "hi"; $window_hash->{main}->{globals}->{test2} = "there"; $window_hash->{left_label} = $window_hash->{main}->{children}->{frame} +->Label(-textvariable, $window_hash->{main}->{globals}->{test1})->pac +k(-side, 'left'); $window_hash->{right_label} = $window_hash->{main}->{children}->{frame +}->Label(-textvariable, $window_hash->{main}->{globals}->{test2})->pa +ck(-side, 'right'); $window_hash->{test_button} = $window_hash->{main}->{children}->{frame +}->Button(-command, \&_usr_next())->pack(-side, 'bottom'); MainLoop; sub _usr_next { $window_hash->{left_label}->configure(-textvariable, $window_hash- +>{main}->{globals}->{test2}); $window_hash->{right_label}->configure(-textvariable, $window_hash +->{main}->{globals}->{test1}); }
    The above method of using hash of hash of hashes method of Tk objects was suggested to me by NULE. It's actually a very creative method of making windows in Tk. The garbage collector apparently uses a linked list to store the parent and child windows, so you can put them anywhere and they will be properly garbage collected. I hope the example code is some help...

    Theodore Charles III
    Network Administrator
    Los Angeles Senior High
    4650 W. Olympic Blvd.
    Los Angeles, CA 90019
    323-937-3210 ext. 224
    email->secon_kun@hotmail.com
    perl -e "map{print++$_}split//,Mdbnr;"