syntax highlighting

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

syntax highlighting

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?

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


_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: syntax highlighting

Michael Lange
Hi,

Thus spoketh Nemes Andrei <[hidden email]>
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
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: syntax highlighting

Alan Gauld
In reply to this post by Nemes Andrei

"Nemes Andrei" <[hidden email]> wrote

> 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/


_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss