Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Printing text from scrolled window?

by jdtoronto (Prior)
on Jul 28, 2006 at 17:34 UTC ( [id://564402]=note: print w/replies, xml ) Need Help??


in reply to Re: Printing text from scrolled window?
in thread Printing text from scrolled window?

Grandfather,

Like Fletch asked, why separate the lines from the widget? I combined the code and ran it, and it worked fine without splitting the lines.

#!/usr/bin/perl -w use strict; use Tk; my $maincolor='red'; my $mw = MainWindow->new; $mw->minsize(qw(250 200)); #x y $mw->configure(-title=>'PROGRAM', -background=>$maincolor); my $frame_1=$mw->Frame(-borderwidth => 3, -background => $maincolor)-> +pack( -side=>'top', -fill=>'x'); $frame_1->Label(-text => 'Scroll', -background=>'yellow',)->pack(-side + =>'left'); my $scroll=$mw->Scrolled("Text", -scrollbars => 'e', -width => 10, -he +ight => 10,)->pack; my $frame_2=$mw->Frame(-borderwidth => 3, -background => $maincolor)-> +pack( -side=>'top', -fill=>'x'); $frame_2->Button( -text => 'Print', -width=>7, -command=>\&doPrint )->pack(-side =>'bottom'); MainLoop; sub doPrint { #my @lines = split /\n/, $scroll->Contents (); #print join "\n", @lines; my $lines = $scroll->Contents(); print $lines; }

jdtoronto

Replies are listed 'Best First'.
Re^3: Printing text from scrolled window?
by GrandFather (Saint) on Jul 28, 2006 at 18:55 UTC

    See the reply to Fletch.

    BTW, why introduce a variable, or even the named sub come to that?

    $frame_2->Button( -text => 'Print', -width=>7, -command=>sub {print $scroll->Contents ()}, )->pack(-side =>'bottom');

    does what is required without the sub or variable, but doesn't show OP how to get the lines into an array and doesn't illustrate the technique required to use a named sub for the command handler.

    It may well be that OP doesn't require instruction in such things and possibly no-one who comes after will benefit, but it wasn't much effort to illustrate those techniques and someone may learn from it.


    DWIM is Perl's answer to Gödel

      The worry is that they might get the wrong idea from it: I need to print out these lines, so I'll split things into an array and then use join when I print because I saw that code on PerlMonks so it must be right.</cargocult>

      I actually think your above answer is better because it does directly answer the question. You could always supplement it with asides about "to get an array of the lines use split like ...", or "if you're going to do complex processing before printing the contents you might want to use \&process_and_print and move the actual work to sub process_and_print { ... }".

      But that's just me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://564402]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found