Label widget -textvariable option 
Author Message
 Label widget -textvariable option

How do I de-reference a hash of arrays as the object for the
-textvariable of a Label widget?
(which should allow the text in the Label widget to change when the
string changes)

I have a string "foo" in $hash{$key}[0], and would like for the
displayed text in my Label widget to change
when the contents of $hash{$key}[0] changes.     $hash{$key}[0] = "bar";

$mw->Label(-textvariable => \$hash{$key}[0]) creates a label widget with
the correct text, but when the
string in the hash is changed, the Label widget text field does not
update.

Mike Berry



Wed, 26 May 2004 06:56:31 GMT  
 Label widget -textvariable option
use Tk;

my %hash = ( key => ["foo"] );

my $mw = MainWindow->new();

my $label = $mw->Label(
    -textvariable => \$hash{key}->[0]
)->pack;

my $button = $mw->Button(
    -text => "Change to bar",
    -command => [\&changeLabel, \%hash]
)->pack;

MainLoop;

sub changeLabel {
    my $HR = shift;
    $HR->{key}->[0] = "bar";

Quote:
}



Wed, 26 May 2004 08:34:49 GMT  
 Label widget -textvariable option

Quote:

> How do I de-reference a hash of arrays as the object for the
> -textvariable of a Label widget?
> (which should allow the text in the Label widget to change when the
> string changes)
> I have a string "foo" in $hash{$key}[0], and would like for the
> displayed text in my Label widget to change
> when the contents of $hash{$key}[0] changes.     $hash{$key}[0] = "bar";
> $mw->Label(-textvariable => \$hash{$key}[0]) creates a label widget with
> the correct text, but when the
> string in the hash is changed, the Label widget text field does not
> update.
> Mike Berry


Hmm, looks reasonable - does this help?

#!/usr/local/bin/perl -w
use strict;

my %h;

$h{'key1'} = [3,9,27];
print $h{'key1'}, "!\n";      # array ref
print $h{'key1'}[1], "!\n";   # 2nd element of array

use Tk;
my $mw = MainWindow->new;
my $l = $mw->Label(-textvariable => \$h{'key1'}[1] )->pack;

$mw->after(2000 => sub {$h{'key1'}[1] = 876});

MainLoop;

Steve

'other perl hacker';$z='createText';$c=$m->Canvas(-wi,$_[1],-he,25)->grid;$c->$
En'.
'ter>',sub{$y=int(rand($m->screenheight));$m->geometry("+$y+$y")});MainLoop;



Wed, 26 May 2004 08:39:38 GMT  
 Label widget -textvariable option

Quote:

> How do I de-reference a hash of arrays as the object for the
> -textvariable of a Label widget?
> (which should allow the text in the Label widget to change when the
> string changes)

> I have a string "foo" in $hash{$key}[0], and would like for the
> displayed text in my Label widget to change
> when the contents of $hash{$key}[0] changes.     $hash{$key}[0] = "bar";

> $mw->Label(-textvariable => \$hash{$key}[0]) creates a label widget with
> the correct text, but when the
> string in the hash is changed, the Label widget text field does not
> update.

You have to be careful to use always the same reference. For example
the following will not work:

    $hash{$key} = ["foo"];

You have another $hash{$key}[0], but the Label is still pointing to
the same reference. However

    $hash{$key}[0] = "foo";

should work.

Regards,
        Slaven

--

    tksm - Perl/Tk program for searching and replacing in multiple files
    http://ptktools.sourceforge.net/#tksm



Wed, 26 May 2004 08:07:40 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Entry widget and -textvariable configuration option..

2. Entry widget -textvariable option

3. problem trying to update label -textvariable from

4. problem trying to update label -textvariable from subroutine

5. problem trying to update label -textvariable from subroutine -- \-\ e \_ p

6. Win32 BUG in child Label with -textvariable (?)

7. -textvariable option in NumEntryPlain.pm

8. why dereference and then pass reference again on -textvariable option

9. change background color on a widget when the textvariable changes

10. Implementing a -textvariable in a custom widget

11. Bug (?) in Tk labels, foreground option

12. Changing font in Label widget

 

 
Powered by phpBB® Forum Software