Syntax Highlighting in a tkinter Text widget

Christian Gollwitzer auriocus at gmx.de
Tue Oct 7 01:49:44 EDT 2014


Am 07.10.14 07:35, schrieb Nicholas Cannon:
> Hey guys Im working on an open source text
> editor(https://github.com/nicodasiko/Text-Config-2) and I would like
> to add syntax highlighting(mainly for python code). I have built the
> editor in python and the text input is a Text tkinter widget. I know
> how to add tags and highlight things  but Im not sure on how to
> constantly update the highlighting.

How long do you expect the input to be? If it will be short pieces, just 
run your regexps at every keystroke.

You can also cheat and use an existing tool for syntax highlighting. One 
possibility is the ctext widget, which is based on the text widget and 
is part of tcllib:

	http://tcllib.sourceforge.net/doc/ctext.html

All you need to do is to wrap it for the use in Tkinter.

Another alternative would be ScintillaTk

	http://sourceforge.net/projects/scintillatk/

Scintilla is a full-blown editor component, which is used in many code 
editors as the basic building block. Since this is a compiled extension, 
I have no idea how hard it will be to interface it with Python, however.

	Christian



More information about the Python-list mailing list