I have a perplexing problem which may turn out have a simple solution (hopes!).

I am writing a little GUI to allow a user to enter information which will get built into an XML file which is linked to flash-based graphic.
One of the fields I have is a Tkinter text widget. Now I am trying to format the text entered by a user so that:

  1. Each line is a specific number of characters wide and it turns to a new line at that point
  2. And it will automatically insert a <br/> tag at this point of specific length(ie every 20 characters etc)

I'm sure this should be simple but I'm struggling.
I used .get() to grab the text from the widget and have tried to manipulate this as a string but I can't work out how to count off the characters.
I tried breaking up the string into words and then recompiling it as a sentence and counting the characters as I go and then trying to calculate steps of 40 characters or so to break it again and insert the </br> tag.

This is the snippet I've been fooling around with. Please be gentle on my inelegant coding (I'm a journalist trying to produce something useful for my colleagues - not a programmer!)

description = """Ngqolomashe was hit with fists and open hands, kicked in the face and strangled by about 15 people. His head was also hit against a metal parking meter and he was seen to be bleeding profusely.  """

wordlist=description.split()
sentence=""


for word in wordlist:
    sentence=sentence+" "+word
    length=len(sentence)
  
    if  length % 40  <5:## trying to use this to calculate some sort of stepping process to determine the spot to
                                    ##insert the <br/tag>
        sentence=sentence+" "+"<br/>"+word

 
print sentence

So, in summary:
Here is a sentence. I want to break it at every 40 characters and insert a <br/> tag. but it must recognise that if the point is in the middle of a word to force the word over onto the new line.

As usual, thanks in advance

Recommended Answers

All 2 Replies

Python is a modular language, and half the difficulty is discovering these optimized little gems ...

# use Python module textwrap to break up a long text into
# columns of maximum 40 characters, words are kept whole

import textwrap

description = """\
Ngqolomashe was hit with fists and open hands, kicked in the face
and strangled by about 15 people. His head was also hit against a
metal parking meter and he was seen to be bleeding profusely.  """


# blank lines are ignored
sentence = textwrap.fill(description, 40)

print(sentence)

""" result >>>
Ngqolomashe was hit with fists and open
hands, kicked in the face and strangled
by about 15 people. His head was also
hit against a metal parking meter and he
was seen to be bleeding profusely.
"""
commented: very good comments ! +4

You're a genius! A million thank-yous. You don't know how much googling I did and never came across the suggestion of such a module. Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.