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