http://qs321.pair.com?node_id=564313

tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:

I have written the following:
#!/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=>sub{})->pack(- +side =>'bottom'); MainLoop;
The point is that there should be a scrolled window in which it is possible to directly write things. In my code the window it is of "Text"-type (but I´m not sure if others could be better). Now I would like to make it possible to print the text entered in the scolled window by clicking the "print" button, but I dont know how to do this, could you help me? Preferably (if it is possible) the text could be extracted as an array so that each line in the scrolled window is an ellement in the array. Thank you for any help

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

    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

      /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
      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
Re: Printing text from scrolled window?
by reneeb (Chaplain) on Jul 28, 2006 at 10:30 UTC
    You can use the Printer module. You have to get the elements from the Text widget and pass them to the printer.
      The printing is not very importent the importent thing for the application is to get the text out from the frame since I will use the frame in a larger context without any printing.