Hi,

I've been working a lot with text widgets lately, and I have posted several questions here. Again, thanks for all your help.

This time I'd like to post something I found out, so you can help me improve it.


My program consists of two textboxes. The texts inserted in both are expected to have the same amount of paragraphs, if not, users are expected to modify them until this condition is met.

Now, to make things better for them, I decided to place a special character every time they press enter, in other words to have the program mark the end of a paragraph by inserting a particular character.

This is the code:

txt = open(file, "r") #don't forget to replace 'file' with the appropriate name
for line in txt.readlines():
    t1.insert(END, line.replace("\n","¶\n"))#t1 is the textbox

Now:

1. Is there a way to insert this thing = "¶" EVERY time the user presses enter (that would be replacing "\n" with ¶\n, but how do I tell the program to keep reading the widget and replacing?)

2. How do I give that character a red color. I know the fg="red" parameter, but it only works when I create the text widget.


thank you guys.

Hi,

First of all, what are you using, TKinter, PyQt...?I use PyQt so I'm not sure what sort of text box it is you're using.

I haven't tried it because I'm a bit in a hurry, but here goes:
For 1.
- just like A = 0x65 prints out as str(A)='A', the paragraph sign is 0x182. An idea to add it to your text in the text box is either to replace as you say '\n' by '\x182\n' or by struct.pack('B', 0x182)+'\n'.
- your text box should have a returnPressed() SIGNAL (if it is PyQt you're using) and by connecting this signal to a slot (method) that would read present text, change the carriage return, and set the new text, you should get what you want.

2. For the partial color, I believe (in PyQt again) that there are some text browsers that allow large formatting options, HTML options etc.

I'm sorry if I can't help more...Good luck anyways.

T

Oh, of course, I forgot to tell you which toolkit I use. It's tkinter.

I managed to make the characters appear, but I need some way of making them appear as the user writes.

This article has been dead for over six months. Start a new discussion instead.