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.


DWIM is Perl's answer to Gödel

In reply to Re: How do I preset values in an entry-box? by GrandFather
in thread How do I preset values in an entry-box? by tamaguchi

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.