Hello,

I'm trying to fill in a textvariable in a Tk grid, and it only seems to work if I assign the array elements to scalar variables first.

So it works when I use:
->Label( -textvariable => \$some_scalar )
but not when I do this:
->Label( -textvariable => \$some_array[1] )

In any perl documentation I've seen, they should both be treated as references to scalars, but the text never shows up in the second case. Here's my stripped down code that demonstrates the problem (at least on all the systems I've tried!). It should make a 2x2 grid with "abc" and "def" on the top row, and "1" and "2" on the bottom row. "abc" doesn't show up when I use the array element reference.

What am I overlooking?

Peter

#!/usr/bin/perl use Tk; use strict; use warnings; my (@status_label, @status_value); my ($entry11, $entry12, $entry21, $entry22); #--- my $mainwindow = MainWindow->new(); $mainwindow -> title('testtk'); my $frame_top = $mainwindow ->Frame() ->pack(-side=>'top'); my $label1 = $frame_top ->Label( -textvariable => \$status_label[0] ) ->grid(-row=>1,-column=>1); my $label2 = $frame_top ->Label( -textvariable => \$entry12 ) ->grid(-row=>1,-column=>2); my $value1 = $frame_top ->Label( -textvariable => \$entry21 ) ->grid(-row=>2,-column=>1); my $value2 = $frame_top ->Label( -textvariable => \$entry22 ) ->grid(-row=>2,-column=>2); #--- @status_label = ("abc","def"); @status_value = (1,2); &update_interface(); MainLoop; #--- sub update_interface { $entry11 = $status_label[0]; $entry12 = $status_label[1]; $entry21 = $status_value[0]; $entry22 = $status_value[1]; }

In reply to why won't Tk textvariable take a reference to an array element? by peter.mao

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.