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

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

Hi everyone ,

I urgently require some suggestions regarding formatting of text in perl tk's text widget. Can we create a table within the tk widget to display data ..???? I know this may sound silly but my actual problem is displaying text in the widget in a very organized manner.
My problem can be broadly viewed as :
1. Display text in a table format ( multiple rows, 4 columns ). I use the command  $txt->insert('end', "ABC\t\tBCN\t\tNN\t\tARCFN\n"); However, the major limitation for this format is the string size. As soon as the string length for for any of the first three strings exceeds a certain length, the tab spacing is increased making the entry look weird. Is there a better way to do this ??? Thanks ..

I want to have something like :

ABC   NACA   AFASFA  AAA
ABC   NACA   AFASFA   AAA


As soon as the string size increases, the pattern looks something like this

ABC   NACA   AFASFA   AAA
ABCASDA     NACA     AFASFA     AAA
AS   NACA   AFASFA   AAA


Please help ,...1!!

Replies are listed 'Best First'.
Re: Formatting text-Create table -perl Tk
by toolic (Bishop) on Dec 03, 2012 at 20:16 UTC
Re: Formatting text-Create table -perl Tk
by kcott (Archbishop) on Dec 04, 2012 at 08:42 UTC
Re: Formatting text-Create table -perl Tk
by zentara (Archbishop) on Dec 04, 2012 at 10:40 UTC
    You can try the format method. See perldoc format.
    #!/usr/bin/perl my $foo = ''; open FH, '+>', \$foo or die $!; format FH_TOP = WHO COMMAND OUTPUT USER TERMINAL DATE & TIME LOCATION ----------------------------------------------------------- . my @who_output= `who`; foreach my $line (@who_output) { my ($userid,$terminal,$month,$day,$time,$location) = split(/\s+/,$line +); chomp $location; format FH = @<<<<<<< @<<<<<<<<< @<<< @<< @<<<<< @<<<<<<<<<<<< $userid $terminal $month $day $time $location . write FH; } #print "$foo\n"; use Tk; my $top = new MainWindow; my $txt = $top->Scrolled("Text")->pack; $txt->insert('end', "$foo\n") ; MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Formatting text-Create table -perl Tk
by reaper9187 (Scribe) on Dec 05, 2012 at 19:10 UTC
    I cannot thank you enough..!!! It did the trick .. Been breaking my head for some time now ... gosh ..1!!!! Thanku ..!!!!!!!!!!! Didnt think something so basic could ruin the entire display ... Get to learn something new everyday ,.. :)

      Hi reaper9187,

      Are you replying to me? If so, I'm glad I could offer some assistance.

      For what it's worth, there should be a [reply] link to the right of each message. Using that will cause your message to appear indented beneath the one you're replying to, and may (depending on the user's settings, I believe) notify the user that you've replied. Otherwise, it kind of looks like you're talking to yourself.

      And, hey, a day where you've learned something is always a good day.

        The post and thank you note was for you ... Thank you very much .. !! :)
Re: Formatting text-Create table -perl Tk
by reaper9187 (Scribe) on Dec 05, 2012 at 18:14 UTC
    I was just trying to use the Text::Table module and it seemed to work perfectly well in the command window. But its acting up in the Tk text widget .. As far as i understand ,the column width is adaptable to string size( or is it ??) being entered.. When i try to output the result in the text widget , its all messy again .. Kindly Help ,...!!!!

      Hiya reaper9187,

      There's a chance this isn't your problem, but I wonder if you're using a monospaced font in your text widget. I'm not terribly familiar with Tk myself, so maybe it does so by default. If not, maybe something in a nice Courier will do (at least you'll be able to see if that's the issue).

      Good luck...