Dynamic text color

Dave McCormick mackrackit at gmail.com
Wed Dec 30 12:58:06 EST 2009


Hi All,

I am new to Python and the list so I hope  I am posting this correctly...

I am working on a way to have text automatically formated in a Tkiniter 
Text widget and would like some input on my code.
Currently I am using Python 2.5 because the server I use has that 
installed. Tkinter is tk8.4.

Most of the time when I type red, blue, or green the code works as 
expected.  When I copy paste text into the widget the last line is 
parsed with part of the previous lines
So I guess the problem is in the "looping"?

Here is my code:
from Tkinter import *
root = Tk()
def get_position(event):
    start = 1.0
    while 1:       
        pos = Tbox.search("red",END,backwards=TRUE)
        if not pos:
            break
        red = pos + "-1c"
        Tbox.tag_add("red", pos, float(pos)+.03)
        Tbox.tag_config("red", foreground="red")
     
        pos = Tbox.search("blue",END,backwards=TRUE)
        if not pos:
            break
        blue = pos + "-1c"
        Tbox.tag_add("blue", pos, float(pos)+.04)
        Tbox.tag_config("blue", foreground="blue")

        pos = Tbox.search("green",END,backwards=TRUE)
        if not pos:
            break
        green = pos + "-1c"
        Tbox.tag_add("green", pos, float(pos)+.05)
        Tbox.tag_config("green", foreground="green")
        break

Tbox = Text(root,width=40, height=15, wrap=CHAR)
Tbox.grid(column=0, row=0, sticky=(N+W+E+S))
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
Tbox.bind("<KeyRelease>", get_position)
Tbox.focus()
root.mainloop()

Thank you,
Dave



More information about the Python-list mailing list