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
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
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. |
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__
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.)
>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,
>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...
>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.)
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
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'
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
>> 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>.