Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 7th, 2004, 12:21 AM
Loki34622 Loki34622 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 1 Loki34622 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Syntax Highlighting

I've recently picked up on learning Python (because i have since been too lazy to continue my ventures in C/C++). As an exercise, i decided to create a small Notepad - like program using the Tkinter module (once i get Fedora set up on my new hard drive i'll probably try out some stuff with GTK). Well, then i figured i'd go the extra mile and add a syntax highlighter (you know, just to see if i could). Well, that's about where i've been for the past hour or two. I was wondering if any of you had any ideas on how to do syntax highlighting. As i said before, i'm using the Tkinter module. I'm guessing that i have to use tags (like text.tag_config("n", background="yellow", foreground="red")) but i'm not sure how to nsert the tags at the given place automatically. Any help is appreciated.

Reply With Quote
  #2  
Old August 8th, 2004, 01:28 AM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m
Reputation Power: 5
I saw your post yesterday, but didn't have time to give you an answer then. seeing as you still haven't received help, I hacked up this little example of a syntax highlighting Text widget. You are correct in assuming you need to use tags. My code binds any key press to a function that analyses the current line of code and highlights python keywords and integers. You could expand on it to do class/function names and strings. It also needs to be fixed up so that if you were to load a script in an editor environment (this is just a text window without any other features) it would highlight the whole thing. Just a few ideas to play with

Code:
from Tkinter import *
import keyword
from string import ascii_letters, digits, punctuation, join

class SyntaxHighlightingText(Text):

    tags = {'kw': 'orange',
            'int': 'red'}

    def __init__(self, root):
        Text.__init__(self, root)
        self.config_tags()
        self.characters = ascii_letters + digits + punctuation

        self.bind('<Key>', self.key_press)

    def config_tags(self):
        for tag, val in self.tags.items():
            self.tag_config(tag, foreground=val)

    def remove_tags(self, start, end):
        for tag in self.tags.keys():
            self.tag_remove(tag, start, end)

    def key_press(self, key):
        cline = self.index(INSERT).split('.')[0]
        lastcol = 0
        char = self.get('%s.%d'%(cline, lastcol))
        while char != '\n':
            lastcol += 1
            char = self.get('%s.%d'%(cline, lastcol))

        buffer = self.get('%s.%d'%(cline,0),'%s.%d'%(cline,lastcol))
        tokenized = buffer.split(' ')

        self.remove_tags('%s.%d'%(cline, 0), '%s.%d'%(cline, lastcol))

        start, end = 0, 0
        for token in tokenized:
            end = start + len(token)
            if token in keyword.kwlist:
                self.tag_add('kw', '%s.%d'%(cline, start), '%s.%d'%(cline, end))
            else:
                for index in range(len(token)):
                    try:
                        int(token[index])
                    except ValueError:
                        pass
                    else:
                        self.tag_add('int', '%s.%d'%(cline, start+index))

            start += len(token)+1

if __name__ == '__main__':
    root = Tk()
    sht = SyntaxHighlightingText(root)
    sht.pack()
    root.mainloop()

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Syntax Highlighting


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway