Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Line size in a text widget

by witandhumor (Pilgrim)
on Mar 10, 2004 at 04:21 UTC ( #335344=perlquestion: print w/replies, xml ) Need Help??
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!

#!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,


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?


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

      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. } }
What is a text widget?
by Anonymous Monk on Mar 10, 2004 at 04:39 UTC
    What is a text widget? My computer only has a monitor
      Oh great, now you've updated Line size in a text widget with some Tk code and you didn't note that you've updated it. Thanks for making me a liar.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://335344]
Approved by arden
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2017-07-25 17:02 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (376 votes). Check out past polls.