Note that there are some explanatory texts on larger screens.

plurals
  1. POTk::Stderr empty text subwidget
    primarykey
    data
    text
    <p>I've written a perl/tk gui. The stderr &amp; stdout write to a popup window, but I cannot clear the text when the popup window is destroyed (it is actually withdrawn). I could not install Tk::Stderr so I appended the module to the end of my script. Below is a working example.</p> <p>I have added the following line to the Print subroutine, but it is overkill:</p> <pre><code>$text-&gt;delete('0.0', 'end'); </code></pre> <p>My suspicion is that something could be added to the following line in the Populate subroutine:</p> <pre><code>$mw-&gt;protocol(WM_DELETE_WINDOW =&gt; [ $mw =&gt; 'withdraw']); </code></pre> <p>but I don't know what. I would appreciate any assistance.</p> <pre><code>#!/usr/bin/perl use warnings; use strict; use Tk; # use Tk::Stderr; &lt;&lt; ** pasted module after main ** - honyok # create main window my $mw = MainWindow-&gt;new; $mw-&gt;InitStderr; $mw-&gt;optionAdd("*font", "-*-calibri-normal-r-*-*-*-120-*-*-*-*-*-*"); $mw-&gt;protocol('WM_DELETE_WINDOW'=&gt; sub{exit}); $mw-&gt;geometry( "100x100"); $mw-&gt;resizable(0,0);# not resizable # create buttons my $button1=$mw-&gt;Button(-text=&gt;'STDERR',-command=&gt;[sub{print STDERR "Writing to STDERR\n";}])-&gt;pack; my $button2=$mw-&gt;Button(-text=&gt;'STDOUT',-command=&gt;[sub{print STDOUT "Writing to STDOUT\n";}])-&gt;pack; MainLoop; # =========================== end main ================================== ##============================================================================== ## Tk::Stderr - capture program standard error output ##============================================================================== ## $Id: Stderr.pm,v 1.2 2003/04/01 03:58:42 kevin Exp $ ##============================================================================== #require 5.006; package Tk::Stderr; use strict; use warnings; use vars qw($VERSION @ISA); ($VERSION) = q$Revision: 1.2 $ =~ /Revision:\s+(\S+)/ or $VERSION = "0.0"; use base qw(Tk::Derived Tk::MainWindow); use Tk::ROText; use Tk::Frame; ##============================================================================== ## Populate ##============================================================================== sub Populate { my ($mw, $args) = @_; my $private = $mw-&gt;privateData; $private-&gt;{ReferenceCount} = 0; $private-&gt;{Enabled} = 0; $mw-&gt;SUPER::Populate($args); $mw-&gt;withdraw; $mw-&gt;protocol(WM_DELETE_WINDOW =&gt; [ $mw =&gt; 'withdraw']); my $f = $mw-&gt;Frame( Name =&gt; 'stderr_frame', )-&gt;pack(-fill =&gt; 'both', -expand =&gt; 1); my $text = $f-&gt;Scrolled( 'ROText', Name =&gt; 'stderr_text', -scrollbars =&gt; 'se', -label=&gt;'Output/Errors', -wrap =&gt; 'none' #-background=&gt;'slate grey' )-&gt;pack(-fill =&gt; 'both', -expand =&gt; 1); $mw-&gt;Advertise('text' =&gt; $text); $mw-&gt;ConfigSpecs( '-title' =&gt; [ qw/METHOD title Title/, "truGrid" ], ); $mw-&gt;Redirect(1); return $mw; } ##============================================================================== ## Redirect ##============================================================================== sub Redirect { my ($mw, $boolean) = @_; my $private = $mw-&gt;privateData; my $old = $private-&gt;{Enabled}; if ($old &amp;&amp; !$boolean) { untie *STDOUT;# ** hacked this line ** - honyok untie *STDERR; $SIG{__WARN__} = 'DEFAULT'; } elsif (!$old &amp;&amp; $boolean) { tie *STDOUT, 'Tk::Stderr::Handle', $mw;# ** hacked this line ** - honyok tie *STDERR, 'Tk::Stderr::Handle', $mw; $SIG{__WARN__} = sub { print STDOUT @_ };# ** hacked this line ** - honyok $SIG{__WARN__} = sub { print STDERR @_ }; } $private-&gt;{Enabled} = $boolean; return $old; } ##============================================================================== ## DecrementReferenceCount ##============================================================================== sub DecrementReferenceCount { my ($mw) = @_; my $private = $mw-&gt;privateData; if (--$private-&gt;{ReferenceCount} &lt;= 0) { $mw-&gt;destroy; } } ##============================================================================== ## IncrementReferenceCount ##============================================================================== sub IncrementReferenceCount { my ($mw) = @_; my $private = $mw-&gt;privateData; ++$private-&gt;{ReferenceCount}; } package MainWindow; use strict; use warnings; my $error_window; ##============================================================================== ## InitStderr ##============================================================================== sub InitStderr { my ($mw, $title) = @_; unless (defined $error_window) { $error_window = Tk::Stderr-&gt;new; $error_window-&gt;title($title) if defined $title; } $error_window-&gt;IncrementReferenceCount; $mw-&gt;OnDestroy([ 'DecrementReferenceCount' =&gt; $error_window ]); return $mw; } ##============================================================================== ## StderrWindow ##============================================================================== sub StderrWindow { return $error_window; } ##============================================================================== ## RedirectStderr ##============================================================================== sub RedirectStderr { my ($mw, $boolean) = @_; unless (defined $error_window) { $mw-&gt;InitStderr if $boolean; return; } return $error_window-&gt;Redirect($boolean); } ##============================================================================== ## Define the handle that actually implements things. ##============================================================================== BEGIN { package Tk::Stderr::Handle; use strict; use warnings; ##========================================================================== ## TIEHANDLE ##========================================================================== sub TIEHANDLE { my ($class, $window) = @_; bless \$window, $class; } ##========================================================================== ## PRINT ##========================================================================== sub PRINT { my $window = shift; my $text = $$window-&gt;Subwidget('text'); $text-&gt;insert('end', $_) foreach (@_); $text-&gt;see('end'); $$window-&gt;deiconify; $$window-&gt;raise; $$window-&gt;focus; $$window-&gt;update;# ** hacked this line ** - honyok } ##========================================================================== ## PRINTF ##========================================================================== sub PRINTF { my ($window, $format) = splice @_, 0, 2; $window-&gt;PRINT(sprintf $format, @_); } } 1; ##============================================================================== ## $Log: Stderr.pm,v $ ## Revision 1.2 2003/04/01 03:58:42 kevin ## Add RedirectStderr method to allow redirection to be switched on and off. ## ## Revision 1.1 2003/03/26 21:48:43 kevin ## Fix dependencies in Makefile.PL ## ## Revision 1.0 2003/03/26 19:11:32 kevin ## Initial revision ##============================================================================== </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload