Home > Archive > PerlTk > March 2007 > Callbacks and Subroutines
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Callbacks and Subroutines
|
|
|
| Hi,
I am right now learning about Perl/TK and have a basic question
regarding subroutines and callbacks.
I was wondering how can I have the subroutine output to display in the
MainWindow if the subroutine is called using a button widget from the
MainWindow.
Here is the code that I wrote to test my above question.
#!/usr/bin/perl
use strict;
use Tk;
my $mw = MainWindow->new;
$mw->title("Demo Program");
$mw->Button(-text => "Test",
-command => sub{test()})->pack;
$mw->Button(-text => "Exit",
-command => sub{exit})->pack;
MainLoop;
sub test {
print "We have reached the subroutine part \n";
}
>From the above code, I wanted the print statement "We have reached the
subroutine part" to display in the MainWindow when I press the "test"
button.
When I press the "Test" button the result is getting displayed on the
command line and not on the MainWindow.
Thanks,
doni
| |
| felix.da.ru 2007-03-06, 4:18 am |
| On Mar 6, 2:18 am, "doni" <doni.se...@gmail.com> wrote:
> Hi,
>
> I am right now learning about Perl/TK and have a basic question
> regarding subroutines and callbacks.
>
> I was wondering how can I have the subroutine output to display in the
> MainWindow if the subroutine is called using a button widget from the
> MainWindow.
>
> Here is the code that I wrote to test my above question.
>
> #!/usr/bin/perl
>
> use strict;
> use Tk;
>
> my $mw = MainWindow->new;
> $mw->title("Demo Program");
> $mw->Button(-text => "Test",
> -command => sub{test()})->pack;
> $mw->Button(-text => "Exit",
> -command => sub{exit})->pack;
> MainLoop;
>
> sub test {
> print "We have reached the subroutine part \n";
>
> }
>
> subroutine part" to display in the MainWindow when I press the "test"
> button.
>
> When I press the "Test" button the result is getting displayed on the
> command line and not on the MainWindow.
>
> Thanks,
> doni
Hi,
Looks like you've to learn more about Perl/Tk and GUI development in
general.
You are trying to use graphic widget as text terminal, and it for sure
wouldn't work.
To solve your task you've to create respective text widget (Label/
Message/ROText) and insert your text message there.
Example:
#!/usr/bin/perl -w
# test
use strict;
use Tk;
use Tk::Button;
use Tk::Frame;
use Tk::ROText;
my $mw=MainWindow->new(-title=>'test');
#===vptk widgets definition===< DO NOT WRITE UNDER THIS LINE >===
my $w_ROText_003 = $mw -> Scrolled ( 'ROText', -height=>10, -
scrollbars=>'osoe', -width=>30 ) -> pack(-fill=>'both', -expand=>1);
my $w_Frame_002 = $mw -> Frame ( -relief=>'flat' ) -> pack();
my $w_Button_004 = $w_Frame_002 -> Button ( -text=>'Test', -command=>
\&test ) -> pack(-anchor=>'nw', -side=>'left');
my $w_Button_005 = $w_Frame_002 -> Button ( -text=>'Exit', -
command=>sub {exit} ) -> pack(-anchor=>'nw', -side=>'left');
MainLoop;
#===vptk end===< DO NOT CODE ABOVE THIS LINE >===
sub test
{
$w_ROText_003->insert('end',"your text to indicate event\n");
}
| |
| Ch Lamprecht 2007-03-06, 4:18 am |
| doni schrieb:
> Hi,
>
> I am right now learning about Perl/TK and have a basic question
> regarding subroutines and callbacks.
>
> I was wondering how can I have the subroutine output to display in the
> MainWindow if the subroutine is called using a button widget from the
> MainWindow.
You need a widget to display the message. The Labels -textvariable option takes
a reference to a Scalar variable. The widget will automagically update on
changes of that variable.
Christoph
use warnings;
use strict;
use Tk;
my $mw = MainWindow->new;
$mw->title("Demo Program");
my $message = '';
$mw->Label(-textvariable => \$message)->pack;
$mw->Button(-text => "Test",
-command => sub{test(\$message)})->pack;
$mw->Button(-text => "Exit",
-command => sub{exit})->pack;
MainLoop;
sub test {
my $msg = shift;
$$msg = "We have reached the subroutine part \n";
}
--
use Tk;use Tk::GraphItems;$c=tkinit->Canvas->pack;push@i,Tk::GraphItems->
TextBox(text=>$_,canvas=>$c,x=>$x+=70,y=>100)for(Just=>another=>Perl=>Hacker);
Tk::GraphItems->Connector(source=>$i[$_],target=>$i[$_+1])for(0..2);
$c->repeat(30,sub{$_->move(0,4*cos($d+=3.16))for(@i)});MainLoop
|
|
|
|
|