tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:

Im new to -tk and I have written the following fascinating program.
#!/usr/bin/perl -w use Tk; use strict; my $mw = MainWindow->new; $mw->Label(-text => 'Number')->pack; my $nmbr = $mw->Entry(-width => 10); $nmbr->pack; $mw->Button( -text => 'Print', -command => sub{do_print($nmbr);} )->pack; MainLoop; sub do_print { (my $nmbr) = @_; my $nmbr_val = $nmbr->get; print "Printing number $nmbr_val\n"; }
The program open a window with a entry box in which it is possible to put in values. The value in the box is printed in the console when the button "print" is clicked. Now I wonder if it is possible to preset a default value in the entry box so that there alredy is a value in the window when the program is started. (Which could could then be changed to something else.) Thank you for any help.

Replies are listed 'Best First'.
Re: How do I preset values in an entry-box?
by bobf (Monsignor) on Jul 23, 2006 at 20:28 UTC

    I have very little experience with Tk, but the docs (and the test I ran using your example code) indicate that defaults can be provided by simply adding -textvariable => \$defaultvalue to the list of options when you create your Entry object. Note that the -textvariable parameter must take a reference.

    my $default = 5; my $nmbr = $mw->Entry(-width => 10, -textvariable => \$default);

Re: How do I preset values in an entry-box?
by GrandFather (Saint) on Jul 23, 2006 at 21:30 UTC

    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
Re: How do I preset values in an entry-box?
by CountZero (Bishop) on Jul 23, 2006 at 20:36 UTC
    I'm no Tk-specialist either but I think you will need a Text or a TraceText widget for that. TraceText looks especially promising as you can bind a Perl-variable to the contents of this widget, so if you set the content of that variable prior to calling Tk, it should show your "pre-set" value.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law