[Tkinter-discuss] syntax highlighting

Michael Lange klappnase at web.de
Sat May 21 15:14:22 CEST 2011


Hi,

Thus spoketh Nemes Andrei <teh_sh_meister at yahoo.com> 
unto us on Sat, 21 May 2011 12:46:07 +0100 (BST):

> Hi!
> I was wondering if there is any auto-tag function in Tkinter. I want to
> make a simple syntax highlighter, and so far what I've done is bind a
> function to Key event and build a string with the registered keys,
> compare that to a list of special words and if they match, delete the
> word from the text and replace it with a tagged version on the same
> position. This did not work out very well because the key event and
> handlers are fired off on key press, therefore before the character is
> actually written in the text itself, resulting in the function deleting
> everything before the last letter is printed in the text; this results
> always in the last letter of the word remaining on screen (for example
> "doctype" = "doctypee").
> 
> How can I fix this?
> 
> 
> Is there a "KeyUp" event?

You would probably need "<Any-KeyRelease>" .

> 
> Better yet, is there a nicer way to do syntax highlighting?
> Please advise.

I would try to use some code from IDLE. If you run the demo from 
IDLE's ColorDelegator module you get a text widget that highlights python
code. Depending on what exactly you want to achieve, it might be possible
to change the keyword list and a few other things to customize it for
your needs (I never tried this myself, though).

Regards

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

You can't evaluate a man by logic alone.
		-- McCoy, "I, Mudd", stardate 4513.3


More information about the Tkinter-discuss mailing list