Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

TK Problem (me again)

by mr_leisure (Beadle)
on Jan 04, 2001 at 22:54 UTC ( [id://49814]=perlquestion: print w/replies, xml ) Need Help??

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

Hi folks.

I have spent half the afternoon looking on websites and trying to figure out this problem, but to no avail.

I have written a chunk of code, that, using Tk, should read my logfile, and then whack it into the window. This code works fine, see below.

#!/sbcimp/run/pkgs/gsbl/bin/perl -w #!/sbclocal/bin/perl -w #use strict; use Tk; # open filehandle open (LOG, "commandpost.log") || die "Could not open file : $!\n"; ##### # # Main Window # ##### my $mw = MainWindow->new(); # add label $mw->Label(-text => "CMD_POST Error Catcher")->pack; ##### # # Menubar # ##### my $menubar = $mw->Frame(-relief => "ridge", -borderwidth => 2)->pack (-anchor => "nw", -fill => "x"); # add file option to menubar my $filemenu = $menubar->Menubutton(-text => "File", -underline => 1)->pack (-side => "left"); # add separator $filemenu->separator(); # add help menu my $helpmenu = $menubar->Menubutton(-text => "Help", -underline => 1)->pack (-side => "left"); # add separator $helpmenu->separator(); # add about option $helpmenu->command(-label => "About...", -underline =>1, -command => \&help_about); ##### # # Main Text Box # ##### my $listbox = $mw->Listbox(-relief => "sunken", -width => 170, -height + =>30, -background => "white"); # scrollbar my $scrollbar = $mw->Scrollbar(-command => ["yview", $listbox]); $listbox->configure(-yscrollcommand => ["set", $scrollbar]); # set listbox internal colour $listbox->configure(-background => "LightBlue2"); # set main window colour $listbox->setPalette("LightBlue1"); $listbox->pack(-side => "left", -fill => "both", -expand => "yes"); $scrollbar->pack(-side => "right", -fill => "y"); # loop and subroutine to enter text from logfile into listbox $mw->fileevent(LOG, 'readable', [\&insert_lines]); MainLoop; ##### # # Subroutines # ##### sub insert_lines { my $logline; if ($logline =<LOG>) { $listbox->insert('end', $logline); $listbox->yview('moveto' +,100); #format_text(); } else { $mw->fileevent(LOG, 'readable +', ""); } }


Okay, so now I have tried to get clever. I want to put in a little chunk that goes, okay, look at each line, and if one contains the word CRITICAL, print it to the listbox in red, and if one contains the word MAJOR, print it to the listbox in orange.

my $textline; while ($textline = <LOG>) { if ($textline =~ /CRITICAL/) { $listbox->configure(-foreground => "red2"); } elsif ($textline =~ /MAJOR/) { $listbox->configure(-foreground => "DarkOrange2"); } else { } } $mw->fileevent(LOG, 'readable', [\&insert_lines]);
So i figure this bit of code will do the trick. Alas, but no. All that happens is now I get a blank listbox. I thought by putting it before $mw->fileevent(LOG, 'readable', [\&insert_lines]); I wouldn't be screwing up the loop that reads the text file.

HEEEEEEEEEELP! If anyone knows anything about Tk, I would really appreciate it. And I did search here and on CPAN. A lot.

Thanks, ML
if ($mr_leisure) { bow; }
this is still not finished

Replies are listed 'Best First'.
Re: TK Problem (me again)
by danger (Priest) on Jan 05, 2001 at 01:38 UTC

    You can't have multiple colored lines (different colored lines) in a listbox (afaik), you should use the text widget and configure some tags for different colors. Below is a replacement for everything in your script starting from the text box comment (now we use text widget). This doesn't use fileevent because you are just reading from the log file itself rather than a continuous process:

    ##### # # Main Text Box # ##### my $text = $mw->Scrolled('Text', -relief => 'sunken', -width => 100, -height => 30, -background => 'white'); $text->pack(-side => 'left', -fill => 'both', -expand => 'yes'); $text->tagConfigure('CRITICAL', -foreground => 'red'); $text->tagConfigure('MAJOR', -foreground => 'pink'); # loop and subroutine to enter text from logfile into listbox #$mw->fileevent(LOG, 'readable', [\&insert_lines]); insert_lines(); MainLoop(); ##### # # Subroutines # ##### sub insert_lines { while(<LOG>){ if(/(CRITICAL|MAJOR)/){ $text->insert('end',$_,$1); }else{ $text->insert('end',$_); } } }

    hope it helps some.

Re: TK Problem (me again)
by OeufMayo (Curate) on Jan 05, 2001 at 01:36 UTC

    Mmmh, I don't want to spoil the fun of discovering what's wrong or not in your loop, but once I fixed that, I ran into another serious problem: Each Tk listbox entry does not have any attribute -foreground (or any other attribute for that matter, except of course its value).

    Now, I might be wrong, but if you can't make each line in a different color from the Listbox widget, maybe you'll want to look at Tk::Text, to insert a Text widget in each entry (I really don't know if it's possible though...)

    UPDATE: After having checked my @INC to see what kind of modules were provided with Tk, and there's one, Tk::Textlist, which does exactly what you want: display a list box with individual properties available for each entries!

    <kbd>--
    PerlMonger::Paris(http => 'paris.pm.org');</kbd>
Re: TK Problem (me again)
by ichimunki (Priest) on Jan 04, 2001 at 23:46 UTC
    Thoughts: this probably isn't a Tk specific issue. You are probably wiping $logline or $textline before the insert method is called. Which means your loop structure is suspect. Make sure your loop is actually the base level over which the process should be iterated.

    I'm loathe to say the following because there is room in the world for personal style, but I am having a hard time reading the code and would love to be more helpful than I have been on this one. So here are some style tips that have helped me follow my own code and that of others more easily.

    Not so many comments. If a reasonable Perl coder can't follow your code from the plain code then #comment-- or if you want to make a note. Use these sparingly like you would margin notes or footnotes. Major comment blocks work best in POD form, since this means never having to worry about the comment breaking over a line.

    Single spacing for logical blocks, with extra space to break up groupings-- same as you would do for paragraphs. I've seen the C style for if-else, but prefer this:
    if ( $condition ) { this_foo(); that_bar(); } else { #or "elsif ( $condition ) {" a_new_foo(); another_bar(); }
    This keeps with the idea that else is a lead-in to a distinct BLOCK in your code.

    I also tend to use something similar for Tk methods with more than two arguments.
    $object->method( ARG1 => $this, ARG2 => $that, ARG3 => $else ) #if not packing put ";" here ->pack( ARGX => $whyzee );
Re: TK Problem (me again)
by chipmunk (Parson) on Jan 05, 2001 at 01:04 UTC
    The reason that you end up with a blank listbox is that your while loop reads in all the input from LOG, so that by the time &insert_lines tries to read from LOG, there's nothing left. It looks like the code to change the foreground color should be inside &insert_lines.
(tye)Re: TK Problem (me again)
by tye (Sage) on Jan 04, 2001 at 23:05 UTC

    No, I'm pretty sure that won't work.

    If you move the color setting stuff into your insert_lines routine, then it might work. If not, I can do some more digging.

            - tye (but my friends call me "Tye")
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://49814]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found