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

How would I print the text in a .txt file onto a Label Widget in Tk?

If I have several files that I may want to print, but only on command, how would I go about doing that?

Replies are listed 'Best First'.
Re: Printing File Onto Tk::Label
by zentara (Cardinal) on Aug 26, 2011 at 09:35 UTC
    Just in case you have a legitimate need to put a file onto a label, here are a few tips. You need to watch your -wraplength, -anchor, and -justify.

    I suppose for small files, that you want to display readonly, and don't want the overhead of the full Text widget, a Label might be a good choice for display.

    #!/usr/bin/perl use strict; use Tk; my $mw = Tk::MainWindow->new(); #open this script as an example open( my $fh,"< $0" ) or die "$!\n"; my $buf; read( $fh, $buf, -s $fh ); close $fh; my $lab1=$mw->Label(-text => 'abc abc abc abc abc abc abc abc abc abc +abc abc', -bg=>'white', -width => 40, -wraplength => 200, )->pack(-expand=> 0, -fill=> 'x'); my $lab2=$mw->Label(-text => 'abc abc abc abc abc abc abc abc abc abc +abc abc', -bg=>'lightblue', -width => 40, -wraplength => 100, )->pack(-expand=> 0, -fill=> 'x'); my $lab3=$mw->Label(-text => $buf, -bg=>'hotpink', -width => 40, -wraplength => 200, -justify => 'left', -anchor => 'w', )->pack(-expand=> 0, -fill=> 'x'); MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Printing File Onto Tk::Label
by Anonymous Monk on Aug 26, 2011 at 06:19 UTC

    You don't want to do that, you want to use a Text widget

    You can see several examples if you run the Tk demonstration program "widget", or see the program "gedi"

    While you're at it, you might as well read Tk Tutorial