Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Subroutines and Callbacks in Perl/Tk

Reply
Thread Tools

Subroutines and Callbacks in Perl/Tk

 
 
doni
Guest
Posts: n/a
 
      03-06-2007
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      03-06-2007
On Mar 5, 7:19 pm, "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";
>
> }
> >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.


You are very confused about what Perl/Tk is. It is not a different
kind of Perl. All the functions that you learned in Perl do *exactly*
what they do when you `use Tk;`. Nothing's changed. print() still
prints to standard output.

If you want to put a message in your MainWindow, create a Label widget
in the main window, with a -textvariable attribute. Then when you
push the test button, change the contents of that variable.

Paul Lalli

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      03-06-2007

Quoth "Paul Lalli" <>:
> On Mar 5, 7:19 pm, "doni" <doni.se...@gmail.com> wrote:
> >
> > 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.
> >

[ snip code ]
> >
> > When I press the "Test" button the result is getting displayed on the
> > command line and not on the MainWindow.

>
> You are very confused about what Perl/Tk is. It is not a different
> kind of Perl. All the functions that you learned in Perl do *exactly*
> what they do when you `use Tk;`. Nothing's changed. print() still
> prints to standard output.
>
> If you want to put a message in your MainWindow, create a Label widget
> in the main window, with a -textvariable attribute. Then when you
> push the test button, change the contents of that variable.


You may find the following useful: it's what I use to give a basically
command-line program a simple GUI.

Usage is as

#!/usr/bin/perl

use BMORROW::Tie::TkWatch qw/Run/;

Run {
print "Hello world!\n";
warn "Houston, we have a problem";
} 'My Perl App';

__END__

I call the module BMORROW::Tie::TkWatch (I have a convention that
modules for my own use are prefixed with my CPAN id); you probably want
to call it something else.

Ben

package BMORROW::Tie::TkWatch;

use warnings;
use strict;

our @EXPORT_OK = qw/Run Dialog Update/;
use base qw/Exporter/;

use Tk;
use Tk:ialog;
use Scalar::Util qw/openhandle/;

my %TK;

$TK{mw} = Tk::MainWindow->new(
-title => 'Perl',
);
$TK{font} = $TK{mw}->Font(
-family => 'Bitstream Vera Sans Mono',
-size => 10,
);
$TK{out} = $TK{mw}->Scrolled(
ROText =>
-font => $TK{font},
-width => 80,
-height => 35,
-scrollbars => 'osoe',
)->pack(
-fill => 'both',
-expand => 'y',
);

$TK{out}->tag(configure => out => -foreground => 'black');
$TK{out}->tag(configure => err => -foreground => 'red');
$TK{out}->focus;

sub TIEHANDLE {
my $c = shift;
return bless \@_, $c;
}

sub PRINT {
my $s = shift;
my ($o, $t, $h) = @$s;

my $txt = do {
no warnings 'uninitialized';
(join $,, @_) . $\
};
openhandle $h and print $h $txt;
$o->insert(end => $txt, $t);
$o->see('end');
$o->update;
}

tie *STDOUT, __PACKAGE__, $TK{out}, 'out';
open my $OERR, '>&', \*STDERR;
tie *STDERR, __PACKAGE__, $TK{out}, 'err', $OERR;

sub Run (&$) {
my $sub = shift;
$TK{mw}->configure(-title => shift);
$TK{mw}->after(0, $sub);
Tk::MainLoop;
}

sub Dialog {
my $ans = $TK{mw}->Dialog(@_)->Show();
$TK{mw}->update;
return $ans;
}

sub Update {
$TK{mw}->update;
}

sub Tk::Error {
my ($mw, $err) = @_;
print STDERR $err;
}

1;

--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces mollit animos, tristesque mentes erigit. |
Musica vel ipsas arbores et horridas movet feras. |
 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      03-06-2007
On 03/05/2007 06:19 PM, doni 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";
> }
>
>>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
>


Create a label or something else to display the text and store the
reference to it. When you need to display something in that label,
change its text:

use strict;
use Tk;

my $mw = MainWindow->new;
$mw->title("Demo Program");
$mw->Button(-text => "Test",
-command => sub{testfunc()})->pack;
my $lbl = $mw->Label(-text => '')->pack;
$mw->Button(-text => "Exit",
-command => sub{exit})->pack;
MainLoop();

sub testfunc {
my $msg = $lbl->cget('-text');
$lbl->configure(-text => $msg . "We have reached the subroutine
part.\n");
}


__HTH__


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-07-2007

Quoth Michele Dondi <>:
> On Tue, 6 Mar 2007 03:48:20 +0000, Ben Morrow <>
> wrote:
>
> >I call the module BMORROW::Tie::TkWatch (I have a convention that
> >modules for my own use are prefixed with my CPAN id); you probably want

>
> Why don't you release it.


I've been half thinking about releasing it, but that means writing
tests/writing documentation/testing it cross-platform/etc. and I don't
really have time right now .

> It's cool, and certainly useful. Are you a
> PerlMonks user too? If so, then you may also post it in the snippets
> or code sections. If not, may I do so?


No, I'm not; and yes, of course you may . Could you stick a

Copyright 2007 Ben Morrow

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5
or, at your option, any later version of Perl 5 you may have
available.

section at the bottom? (I should really have done so before posting...)

Ben

--
don't get my sympathy hanging out the 15th floor. you've changed the locks 3
times, he still comes reeling though the door, and soon he'll get to you, teach
you how to get to purest hell. you do it to yourself and that's what really
hurts is you do it to yourself just you, you and noone else **
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-07-2007

Quoth Michele Dondi <>:
> On Wed, 7 Mar 2007 01:06:01 +0100, "Petr Vileta"
> <> wrote:
>
> >use Tk;
> >use Tk::Label;
> >use Tk::Button;

>
> AFAIK (not much, really) the latter two are not necessary. (But I
> checked and it seems like it's actually so.)


They aren't strictly necessary: when you create a Button Tk will
autoload Tk::Button for you. But unfortunately it gives a warning, which
would be *very* confusing for users (or, at least, for *my* users ).

Ben

--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
The Levellers, 'Believers'
 
Reply With Quote
 
Ch Lamprecht
Guest
Posts: n/a
 
      03-07-2007
Ben Morrow wrote:
> Quoth Michele Dondi <>:
>
>>On Wed, 7 Mar 2007 01:06:01 +0100, "Petr Vileta"
>><> wrote:
>>
>>
>>>use Tk;
>>>use Tk::Label;
>>>use Tk::Button;

>>
>>AFAIK (not much, really) the latter two are not necessary. (But I
>>checked and it seems like it's actually so.)

>
>
> They aren't strictly necessary: when you create a Button Tk will
> autoload Tk::Button for you. But unfortunately it gives a warning, which
> would be *very* confusing for users (or, at least, for *my* users ).


With Tk-804 this should not be the case (not with Label and Button):

use strict;
use warnings;

use Tk;
my $mw = MainWindow->new( );

$mw->$_()->pack for qw/Label Button Entry Text/;
print "but:\n";
$mw->ROText()->pack;
MainLoop;

Christoph

--
use Tk;use Tk::GraphItems;$c=tkinit->Canvas->pack;push@i,Tk::GraphItems->
TextBox(text=>$_,canvas=>$c,x=>$x+=70,y=>100)for(J ust=>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
memory and subroutines Frank Silvermann C Programming 4 06-14-2006 09:47 PM
Multiple Page_Load subroutines and User Controls =?Utf-8?B?UGF1bA==?= ASP .Net 1 10-26-2005 07:06 PM
References Subroutines and Arrays Ketema Perl Misc 2 03-06-2004 01:49 PM
References and subroutines ReaprZero Perl 1 12-04-2003 02:53 PM
Style question regarding subroutines and lexical variables Joseph Ellis Perl Misc 6 07-24-2003 11:37 PM



Advertisments