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:
- Each line is a specific number of characters wide and it turns to a new line at that point
- 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