in reply to Updating concatenated text on a Menubutton

Thanks for the help thus far, but I think that I failed to fully explain my problem. I want the text on the menubutton to change WHENEVER bit 9 or bit 10 changes. Not only when bit 9 or bit 10 is changed by the menubutton itself, but also when I change the value of bit 9 or bit 10 at other points in my code.

Do I need to change
-textvariable => \$bit_10_9,

to something else?

Everything works fine (i.e. the text changes WHENEVER bit 9 is changed) when I use only
-textvariable => \$bit[9],
or
-textvariable => \$bit[10],

If it works fine for one bit....how can I make it work for a concatenation of two bits?
I don't want to have to update another variable like $bit_10_9 everytime I change $bit[10] or $bit[9].

Replies are listed 'Best First'.
(ichimunki) Re (2): Updating concatenated text on a Menubutton
by ichimunki (Priest) on Nov 27, 2001 at 05:26 UTC
    (Updated) I certainly agree with ar0n that tie is the way to go-- unless that looks too complicated for the case at hand.I still think the best way would be to build something similar to an If you don't want to do that, you might try an accessor method. That way you can trap the calls that set the bits and update the display variable whenever the bits get set. But I tried to do something like making the -textvariable an anonymous sub to no avail. I ended up with debugging code in my label.
    #!/usr/bin/perl -w use strict; use Tk; my $Foo = 'x'; my $Bar = 'y'; my $foobar = setfoobar(); my $mw = Tk::MainWindow->new(); my $entry = $mw->Label( -textvariable => \$foobar, )-> pack( -expand => 'y', ); my $foo_button = $mw->Button( -text => "FOO", -command => sub { flip( $Foo );} )->pack(); my $bar_button = $mw->Button( -text => "BAR", -command => sub { flip( $Bar );} )->pack(); MainLoop(); sub flip { $_[0] = ( $_[0] eq 'x' ) ? 'y' : 'x'; setfoobar(); } sub setfoobar { $foobar = $Foo . $Bar; }
(ichimunki) Re: Updating concatenated text on a Menubutton
by ichimunki (Priest) on Nov 27, 2001 at 22:51 UTC
    Here is my sample code reworked to use a tied array (not all the necessary features of a tied array are implemented here FETCHSIZE and STORESIZE may not be very useful as written-- I just wanted to try the basics of tie and get this example to work a little less clumsily). Thanks to ar0n for suggesting tie in the first place, to ar0n and broquaint for helping me fix a bug in this code. Note that you can also export the $Concat variable in the package to avoid having to package qualify it in Main, but I think I prefer it how it is.
    #!/usr/bin/perl -w use strict; use Tk; my @bit; tie @bit, 'Bitters'; my $mw = Tk::MainWindow->new(); my $entry = $mw->Label( -textvariable => \$Bitters::Concat, )-> pack( -expand => 'y', ); my $foo_button = $mw->Button( -text => "Bit One", -command => sub { flip( $bit[1] );} )->pack(); my $bar_button = $mw->Button( -text => "Bit Zero", -command => sub { flip( $bit[0] );} )->pack(); print "MainLoop\n"; MainLoop(); sub flip { $_[0] = ( $_[0] eq 'x' ) ? 'y' : 'x'; } { package Bitters; use vars qw( $Concat ); sub TIEARRAY { my $class = shift; my $self = { ARRAY => ['x','x'] }; bless $self, $class; $self->update_concatenation(); return $self; } sub FETCH { my($self,$idx) = @_; return $self->{ARRAY}[$idx]; } sub FETCHSIZE { my $self = shift; return $self->{ARRAY}+0; } sub STORESIZE { print "STORESIZE ignored\n"; } sub STORE { my($self, $idx, $value) = @_; print "[STORE $value at $idx]\n"; $self->{ARRAY}[$idx] = $value; $self->update_concatenation(); return $self->{ARRAY}[$idx]; } sub update_concatenation { my $self = shift; $Concat = $self->{ARRAY}[0] . $self->{ARRAY}[1]; print "$Concat\n"; } }#end_package Bitters;