http://www.perlmonks.org?node_id=948784


in reply to Re^3: Perl tk - open file and print content
in thread Perl tk - open file and print content

Thank you so much, now CUPS works fine !! I am able to open a file and it printed content in the terminal. I'm not able to print that content (of the txt files) in a scrolled GUI windows. Can you suggest me the correct sintax to link print@line in $mw....???
sub open_file {my $open = $mw->getOpenFile(-filetypes => $types_OPEN, -defaultextension => '.sff'); $read_fh = IO::File->new("$open",'r'); read_text($read_fh); } sub read_text { local $read_fh = shift; my @lines; @lines = <$read_fh>; print @lines; MainLoop }

Replies are listed 'Best First'.
Re^5: Perl tk - open file and print content
by Anonymous Monk on Jan 19, 2012 at 14:30 UTC
Re^5: Perl tk - open file and print content
by zentara (Archbishop) on Jan 19, 2012 at 16:54 UTC
    Can you suggest me the correct sintax to link print@line in $mw....???

    You can't print in the $mw directly, but you can print to a Scrolled Text Widget in the $mw.

    #!/usr/bin/perl use warnings; use strict; use Tk; open (FH,"<$0") or warn "$!\n"; my @lines = <FH>; close FH; my $mw = MainWindow->new(-title=>'Colored Text'); $mw->geometry(($mw->screenwidth-10) . 'x' . ($mw->screenheight-10) . '+0+20'); $mw->fontCreate('big', -family=>'arial', -weight=>'bold', -size => 25 ); my $text = $mw->Scrolled("Text", -scrollbars => 'osw', -background => 'white', -font =>'big', -wrap => 'none', )->pack; $text->tagConfigure('greyline', -background => 'grey95'); my $toggle = -1; for (@lines) { my $greyline; if( $toggle == 1 ){ my $greyline = '' } else { $greyline = 'greyline' } $text->insert('end',"$_",$greyline); $toggle *= -1; } MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks you for your reply and herlp. Unfortunately with this script i didn't resolve my problem. Because this print out the code of the script itself. I would like to print in that scrolled txt widget what i choose to open when i push a button. ( "open" that calls the function 'Get_open_file' that show me the dir of my PC, than choosing a txt file it should be printed in that scrolled widgets. I don't know if i have been clear! I hope you understand me....please save me ! It's really important
        If you want to print multiple files, it might be best to push all file selections into an array, then send the whole array to the printer, after selections are done. There are many ways to do it.
        #!/usr/bin/perl use Tk; use strict; my $TOP = MainWindow->new; my $button = $TOP->Button(-text=> 'Print It' , -command => sub{ my $file_r = &fileDialog($TOP); print"\n selected this file $file_r\n"; # use whatever method you want to send # $file_r to the printer # like with Net::Cups print " printing now!\n"; # system( "lpr -lp0 -f $file_r"); #untested } )->pack(); MainLoop; sub fileDialog { my( $w ) = @_; my @types =( ["C files", [qw/.c++ .cpp .c/]], ["Log files", [qw/.log/]],, ["Text files", [qw/.txt/]], ["All files", '*'] ); my $file = $w->getOpenFile( -filetypes => \@types ); return($file); }

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh