in reply to Newbie TK question

liverpole's technique is well worth knowing about, but is not the only way of achieving what you want with Tk's bind. Note the array ref passed in place of the subroutine ref in the bind here:

use strict; use warnings; use Tk; sub subbie_1 { my ($widget, $txt, $entry_var) = @_; $txt->insert('end', "$$entry_var\n"); } # Main Window my $mw = new MainWindow; my $entry_var = 'Some text'; #GUI Building Area my $frm_name = $mw -> Frame()->pack(); my $lab = $frm_name -> Label(-text=>"Name:")->pack(-side=>'left'); my $ent = $frm_name -> Entry(-textvariable=>\$entry_var)->pack(); #Text Area my $textarea = $mw -> Frame()->pack(); my $txt = $textarea -> Text(-width=>40, -height=>10)->pack(); $ent->bind('<Return>' => [\&subbie_1, $txt, \$entry_var]); MainLoop();

Update: note that because subbie_1 gets the widget ($ent in this case) there is no need to pass $entry_var so the following modified lines can be used:

... sub subbie_1 { my ($widget, $txt) = @_; $txt->insert('end', $widget->get () . "\n"); } ... $ent->bind('<Return>' => [\&subbie_1, $txt]); MainLoop();

In fact you may not need $entry_var at all now.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Newbie TK question
by hebes_99 (Acolyte) on Jun 30, 2006 at 19:05 UTC
    OK...that worked great for the entry bind. Now, how would I add text without using bind? In other words, I just want to put some sort of text in (not dependent on the user entering test and not using bind). Does that make sense?

      Sorry for the long delay before replying. This is possibly what you want:

      use strict; use warnings; use Tk; # Main Window my $mw = new MainWindow; #GUI Building Area my $frm_name = $mw -> Frame()->pack(); my $lab = $frm_name -> Label(-text=>"Name:")->pack(-side=>'left'); my $ent = $frm_name -> Entry(-text=>'Some text')->pack(); #Text Area my $textarea = $mw -> Frame()->pack(); my $txt = $textarea -> Text(-width=>40, -height=>10)->pack(); MainLoop();

      DWIM is Perl's answer to Gödel