in reply to How do I preset values in an entry-box?
You already had the answer in your Label and Button widgets. Use -text => value for the Entry widget:
my $nmbr = $mw->Entry(-width => 10, -text => 10);
The -textvariable option bobf mentioned binds a variable to the widget's value. You could do it that way and should if you want to access the value without using get. Consider:
#!/usr/bin/perl -w use Tk; use strict; my $mw = MainWindow->new; my $value = 10; $mw->Label(-text => 'Number')->pack; my $nmbr = $mw->Entry(-width => 10, -textvariable => \$value); $nmbr->pack; $mw->Button( -text => 'Print', -command => sub{do_print($value);} )->pack; MainLoop; sub do_print { (my $value) = @_; print "Printing number $value\n"; }
Update: You might also like to try adding
$mw->Button( -text => 'Randomise', -command => sub{do_randomise(\$value);} )->pack;
before MainLoop and
sub do_randomise { (my $value) = @_; $$value = int rand 100; }
to the end of your program. Note that this time we pass a reference to the variable into the sub and can now update the value of the variable and thus the value shown in the Entry widget.
|
|---|