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

I have a few questions regarding perl Tk listed below.

1. By default how can you have the cursor be in a text box to type?

2. How can you allow the option of hitting enter to active the a submit button?

3. I have a series of option buttons all of them are on by default except one. How can I have only one from the list selected and only allow one of the options for choosing?

Thanks in advance to all who reply I already have my Tk GUI built just have to work out the kinks I listed above.

Replies are listed 'Best First'.
Re: perl Tk ?s
by choroba (Cardinal) on May 29, 2012 at 21:28 UTC
    Something like this?
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(-title => "Tk"); my $f = $mw->Frame->pack; # 1 $f->Text()->pack(-side => 'left')->focus; my $b = $mw->Button(-text => 'Submit', -command => sub {print "Submitted\n"}, )->pack; # 2 $mw->bind('<Return>', [sub { $b->invoke; Tk->break; }]); # 3 my $g1; $f->Radiobutton(-text => 'Option1', -value => 1, -variable => \$g1)->pack; $f->Radiobutton(-text => 'Option2', -value => 2, -variable => \$g1)->pack->select; $f->Radiobutton(-text => 'Option3', -value => 3, -variable => \$g1)->pack; MainLoop();
Re: perl Tk ?s
by zentara (Cardinal) on May 29, 2012 at 22:39 UTC
    By default how can you have the cursor be in a text box to type?

    Do you mean a Text widget, or Entry widget when you say "text box". For an Entry widget, you don't need to activate the submit button. The way you do it, is to have bindings to the same callback. I assume that is what you want, and only think you want to actually activate the button, which is a useless step in most cases.

    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow; my $ent = $mw -> Entry( -bg => 'white', ) -> pack(); $ent->focus; # so cursor is there in the entry $ent->bind('<Return>', \&process_it ); my $button = $mw->Button(-text => 'Submit', -command => \&process_it )->pack(); MainLoop; sub process_it { my $var = $ent->get(); print "$var submitted\n" }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh