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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Newbie TK question
by hebes_99 (Acolyte) on Jun 30, 2006 at 19:05 UTC | |
by GrandFather (Saint) on Jul 21, 2006 at 23:09 UTC |