I'm trying to read from two Entry widgets and pass their values to a sub. It's not doing what I would expect. Here's the relevant code:
#!perl
use Tk;
use strict;
use warnings;
my $original_x = 3;
my $original_y = 4;
my $mw = MainWindow -> new;
my $f1 = $mw -> Frame;
my $user_x = $f1 -> Entry (
-textvariable => $original_x,
-width => 3,
) -> pack;
my $user_y = $f1 -> Entry (
-textvariable => $original_y,
-width => 3,
) -> pack;
$f1 -> pack;
$mw -> Button (
-text => "Print Entries",
-command => [\&print_entries, $user_x -> get(), $original_y],
) -> pack;
MainLoop;
sub print_entries {
print "@_\n";
}
I would expect the shell output to be the entry in $user_x, a space, and the entry in $user_y. Instead the output is consistantly the hard-coded default value of $original_x, a space, and the hard-coded default value of $original_y (please don't respond and ask if I changed the values in the Entry widgets ;~) Of course I did!).
You can see in the -command option I tried two different methods for getting the values of Entry widgets. Neither seem to work, which puzzels me. Quoth the Emu, "Nevermore!" Wait, that was a different bird. Mastering Perl/Tk says:
5.2.13. Getting the Contents of an Entry Widget
There are two ways to determine the content of the Entry widget: the get method or the variable associated with the
-textvariable option. Using the get method,
$entry_text = $entry->get() will assign the entire content of the Entry widget into
$entry_text.
The code tries both, but doesn't get what I ask for. Any suggestions as to why? After looking a little deeper I changed
-command => [\&print_entries, $user_x -> get(), $y_size], to
-command => [\&print_entries, $user_x -> cget(-textvariable), $y_size], an still get the same results.
Update: I made one other change and the results changed, but still to something unexpected. I turned
-command => [\&print_entries, $user_x -> get(), $y_size], into
-command => [\&print_entries, \$user_x -> get(), \$y_size], (note the back-slashes before the $ in the variable names). Now, the shell output is SCALAR(some memory location), a space, and SCALAR(some other memory location). How do I extract the user entry from an Entry widget? Is it relevant that the Entry widget is in a Frame that is a child of the widget that the Button widget is in?
Thanks for your help!
Cheers!
-P
Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
-Howard Aiken
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.