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

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

I have a simple issue that I need some help with. Can someone give me a nudge in the right direction?

I am trying to define how many characters can be in a line in a Text Widget. I only want 30 characters on each line.

I do not have a piece of code that even comes close...I have looked and tried Tags and defining -rmargin, but that doesn't seem to do it....help!

#!c:\perl\bin\perl.exe -w use Tk; my $char= "&"; my $mw=MainWindow->new; my $t=$mw->Scrolled("Text",-height=>'45',-width=>'80')->pack(); $t->tagConfigure('tag',-font => "Courier 24 bold",-rmargin=>50); my $tm=500; my $try=Tk::After->new($mw,$tm,'repeat',\&loop); stop(); MainLoop; sub stop { $t->delete('1.0','end'); foreach (1..500) { $t->insert('1.0', ' '); } } sub loop { $num++; if($num==1){first(); } if($num == 3){ second(); } if($num == 4){ third(); } if($num == 5){stop(); $num=0;} } sub first { $t->insert('1.5',$char,'tag'); } sub second { $t->insert('3.6',$char); } sub third { $t->insert('3.7',$char); }

Thanks in advance,

witandhumor

Replies are listed 'Best First'.
Re: Line size in a text widget
by zentara (Archbishop) on Mar 10, 2004 at 17:32 UTC
    There is probably a way to keep track of the line length from the text widget's methods, but here is a brute approach.
    #!/usr/bin/perl use Tk; use strict; my $mw=tkinit; my $textarea = $mw->Scrolled("Text", -scrollbars => 'ose', -width =>80, -wrap =>'word', )->pack; $mw->after(1000,\&filltext); MainLoop; sub filltext { my $length = 0; for (1..200){ if($length > 30){$textarea->insert('end', "\n");; $length = 0} $textarea->insert('end', "$_ "); $length += length("$_ "); $textarea->see('end'); $textarea->idletasks; $mw->after(10); } }

    I'm not really a human, but I play one on earth. flash japh
Re: Line size in a text widget
by Daruma (Curate) on Mar 10, 2004 at 04:43 UTC
    Greetings, witandhumor!

    I'm having a hard time visualizing what you are trying to do here.

    Is this to be a text box where the text wraps to the next line at 30 characters?

    or

    Is this to be a text box that limits the information to 30 characters?

    Daruma
      I should have clarified...sorry about that. This is a text box where the text wraps to the next line at 30 characters.
        not tested, just an idea...
        use Text::Autoformat; my ($allTheText) = shift; ## all your text to format my (@lins); ## your resulting lines my ($widthTxt) = 30; ## your line width... { my ($formated)=autoformat($allTheText, { left=>5, right=>$widthTxt +, all=>1}) if ($allTheText); foreach (split(/\n+/,$formated)) { push(@lins,$_); ## array w/lines of max.30chrs.wth. } }
A reply falls below the community's threshold of quality. You may see it by logging in.