Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Subroutines and Callbacks in Perl/Tk

29 views
Skip to first unread message

doni

unread,
Mar 5, 2007, 7:19:33 PM3/5/07
to
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

Paul Lalli

unread,
Mar 5, 2007, 8:28:44 PM3/5/07
to

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

Ben Morrow

unread,
Mar 5, 2007, 10:48:20 PM3/5/07
to

Quoth "Paul Lalli" <mri...@gmail.com>:

> 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::Dialog;
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. | b...@morrow.me.uk
Musica vel ipsas arbores et horridas movet feras. |

Mumia W.

unread,
Mar 5, 2007, 11:50:03 PM3/5/07
to

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__


Petr Vileta

unread,
Mar 6, 2007, 7:06:01 PM3/6/07
to
"doni" <doni....@gmail.com> píse v diskusním príspevku
news:1173140373.5...@q40g2000cwq.googlegroups.com...

You must have some widget to display text, for example Label()

#!/usr/bin/perl
use strict;
use Tk;

use Tk::Label;
use Tk::Button;

my $text_to_display = '';


my $mw = MainWindow->new;
$mw->title("Demo Program");
$mw->Button(-text => "Test",

-command => \&test)->pack;
$mw->Label(-textvariable => \$text_to_display)->pack;


$mw->Button(-text => "Exit",
-command => sub{exit})->pack;
MainLoop;

sub test {
$text_to_display = "We have reached the subroutine part";
}

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)


Michele Dondi

unread,
Mar 7, 2007, 1:49:29 PM3/7/07
to
On 5 Mar 2007 16:19:33 -0800, "doni" <doni....@gmail.com> wrote:

>I am right now learning about Perl/TK and have a basic question
>regarding subroutines and callbacks.

You may also consider asking in <news:comp.lang.perl.tk>

>$mw->Button(-text => "Test",
> -command => sub{test()})->pack;

Why not \&test?

>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.

You wanted it to, but that's not what print() does. You need some Tk
widget that displays text and have your callback update that.

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

And if there were more complex stuff than a simple MW, where would you
expect print() to "display text"?


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Michele Dondi

unread,
Mar 7, 2007, 1:53:51 PM3/7/07
to
On Tue, 6 Mar 2007 03:48:20 +0000, Ben Morrow <b...@morrow.me.uk>
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. 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? (With attribution, of course!)
There are some people with some expertise in GUI programming there,
including zentara who is also active here, and I bet most "monks"
would be glad to swee it. It would be nice to see if anyone takes it
over to roll his or her own version, possibly doing other interesting
things...

Michele Dondi

unread,
Mar 7, 2007, 2:04:16 PM3/7/07
to
On Wed, 7 Mar 2007 01:06:01 +0100, "Petr Vileta"
<sto...@practisoft.cz> 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.)

Ben Morrow

unread,
Mar 7, 2007, 3:24:07 PM3/7/07
to

Quoth Michele Dondi <bik....@tiscalinet.it>:

> On Tue, 6 Mar 2007 03:48:20 +0000, Ben Morrow <b...@morrow.me.uk>
> 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 ** b...@morrow.me.uk

Ben Morrow

unread,
Mar 7, 2007, 3:25:54 PM3/7/07
to

Quoth Michele Dondi <bik....@tiscalinet.it>:

> On Wed, 7 Mar 2007 01:06:01 +0100, "Petr Vileta"
> <sto...@practisoft.cz> 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.
b...@morrow.me.uk The Levellers, 'Believers'

Ch Lamprecht

unread,
Mar 7, 2007, 4:12:05 PM3/7/07
to
Ben Morrow wrote:
> Quoth Michele Dondi <bik....@tiscalinet.it>:
>
>>On Wed, 7 Mar 2007 01:06:01 +0100, "Petr Vileta"
>><sto...@practisoft.cz> 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(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

Michele Dondi

unread,
Mar 8, 2007, 6:57:59 AM3/8/07
to
On Wed, 7 Mar 2007 20:24:07 +0000, Ben Morrow <b...@morrow.me.uk>
wrote:

>> 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

[snip]


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

Done:

http://perlmonks.org/?node_id=603803

Please note that if anything is not of your liking, the node is
updateable. You can reach me by email at <ti tod nfni tod im tod mcl
ta razalb>.

0 new messages