Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Printing text from scrolled window?

by GrandFather (Saint)
on Jul 28, 2006 at 10:44 UTC ( [id://564317]=note: print w/replies, xml ) Need Help??


in reply to Printing text from scrolled window?

Contents returns the contents of a Text widget as a string. \n is used to seperate lines so the following should get you what you want:

... $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; }

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Printing text from scrolled window?
by Fletch (Bishop) on Jul 28, 2006 at 13:00 UTC

    /boggle

    Erm, why split to immediately rejoin with the same character? print $scroll->Contents(), "\n"; (not being familiar with Tk that may require a scalar after the print, but at any rate it should accomplish the same thing without the extra round trip).

      OP: "Preferably (if it is possible) the text could be extracted as an array ..."

      DWIM is Perl's answer to Gödel

        Oop, quite right. I missed that part of the original question.

        If the OP's trying to post-process the contents line-by-line before printing that's something completely different of course, but I didn't get that from the overall question (and it only would seem that way when you actually read that one un-parenthetical parenthetical aside which I missed).

        Otherwise the objection still remains: if you're just trying to print the contents out somewhere, it's unnecessary work to split to lines just to add back newlines.

        Update: And even if you are post-processing things from an array I'd tend to use something along the lines of for( @lines ) { my $line = munge($_); print $line, "\n"; } rather than joining for one big print just in the interest of efficiency.

        Useless observation: Posting #2000. Break out the crepe paper and confetti.

Re^2: Printing text from scrolled window?
by jdtoronto (Prior) on Jul 28, 2006 at 17:34 UTC
    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

      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://564317]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found