Reputation: 41
So I am actually creating a coding editor and obviously I needed the code to be colored when it appeared on screen. I was successfully able to do that but now, whenever the already colored code is edited, the text remains colored. (for example the user writes "print" and it gets colored. Then he erases the 't' from the end of it and it becomes 'prin' and it is still colored) I was hoping there was a way to detect the font color of a tkinter.Text index. For Example:
textArea.get_color("1.0", "1.4"))
# Returns: Black (or whatever color)
So the app can change it if it isn't the proper word and still colored. Can Anyone Help Me?
Upvotes: 0
Views: 272
Reputation: 385900
You can get the color at any text position by first getting the list of tags for that character, and then getting the foreground color of the highest priority tag. If there are no tags with a foreground color, the color is whatever is configured for the text widget.
For example, it might look something like the following:
def get_color(index):
for tag in text.tag_names(index)[::-1]:
fg = text.tag_cget(tag, "foreground")
if fg != "":
return fg
return text.cget("foreground")
Upvotes: 3