Discussion:
[Tkinter-discuss] syntax highlighting
Nemes Andrei
2011-05-21 11:46:07 UTC
Permalink
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?

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


Here is the bit of code:

self.edit.bind("<Key>", self.parent.key)
...
??? def key(self,event):
??????? if event.char == " ":
??????????? self.word=""
??????? if event.char == event.keysym:
??????????? self.word=self.word+event.char
??????????? self.match(self.word,"HTML")
???????????????????????
???????
??? def match(self,keyword,lang):
??????? match=False
??????? print keyword
??????? if lang=="HTML":
??????????? for item in self.html:
??????????????? if item==keyword:
?????????????????? match=True
?????????????????? break
??????? if match:
??????????? self.tab.editlist[self.tab.nav.index("current")].delete("insert-%s c" %len(keyword), "insert")
??????????? self.tab.editlist[self.tab.nav.index("current")].insert("insert", keyword, "blue")

Thanks in advance!
Regards,
Andrei Nemes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20110521/59917492/attachment.html>
Michael Lange
2011-05-21 13:14:22 UTC
Permalink
Hi,

Thus spoketh Nemes Andrei <teh_sh_meister at yahoo.com>
Post by Nemes Andrei
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>" .
Post by Nemes Andrei
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
Alan Gauld
2011-05-23 08:30:18 UTC
Permalink
Post by Nemes Andrei
Better yet, is there a nicer way to do syntax highlighting?
Take a look at the IDLE source - it is all in Python/Tkinter
and available in the standard distribution.

It seems to handle syntax highlighting OK...

HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
Loading...