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

How do i read data from a file and display it onto a label in Perl TK ? My code is :
system(" egrep --color 'Mem|Cache|Swap|Buffer' /proc/meminfo > memOutp +ut.txt"); sysopen($memData,"memOutput.txt",O_RDWR); while($line = <$memData>) {print "$line"; $myLabel=$memWindow->Label(-text =>'$line'); }
This doesnt seem to work ! Thanks in advance

Replies are listed 'Best First'.
Problem creating Tk::Label widgets from file data (was: Re: Perl tk)
by kcott (Archbishop) on Mar 24, 2014 at 07:14 UTC

    G'day coder_perl,

    "This doesnt seem to work !"

    That tells us nothing useful at all. What didn't work? In what way didn't it work? What output did you get? What output were you expecting to get? Were there any error or warning messages? Please following the guidelines in "How do I post a question effectively?" when posting questions.

    Why are you using sysopen to read a text file? Why are you using a mode of O_RDWR?

    Possible options (for better ways to access the data) might include not using an intermediary file, but instead iterating the output of your egrep command by using:

    Your "$myLabel=..." is also problematic. You assign each Tk::Label widget you create (in every iteration of the while loop) to the same package global variable. The 'vars' stricture of strict would have warned you about this.

    The title of your post also needs mentioning: "Perl tk" is far from adequate. A better choice would be something like: Problem creating Tk::Label widgets from file data. See "How do I compose an effective node title?".

    -- Ken

Re: Problem creating Tk::Label widgets from file data
by zentara (Cardinal) on Mar 24, 2014 at 10:48 UTC
    This doesnt seem to work !

    The code below does what you want, but be advised dumping alot of text into a Label widget may not be what you want, a Text widget may be better. See Insert something like a hyperlink in a Tk Text widget

    #!/usr/bin/perl use Tk; use strict; my $mw = MainWindow->new; my $buf; open (FH,"< $0"); read( FH, $buf, -s FH ); close FH; my $label = $mw->Label( -textvariable => \$buf, -background => 'black', -fg=>'yellow', )->pack; MainLoop;
    The reason your code dosn't work, is you didn't define $memWindow anywhere, and you didn't pack your labels. If you don't pack them, or use another geometry manager, the labels lay hidden behind the scenes. Also see: linux memory leak monitor for a good example of what you may be looking for. Be aware, that you shouldn't use system, or sleep, or anything which blocks the eventloop in GUI's. It is better to fork or use a pipe to run commands.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Problem creating Tk::Label widgets from file data
by Anonymous Monk on Mar 24, 2014 at 06:47 UTC

    This doesnt seem to work ! Thanks in advance

    Why doesn't it work?